Skip to content

Commit

Permalink
feat: enforce 0.01 lower bound for widthTolerance (#211)
Browse files Browse the repository at this point in the history
* feat: enforce 0.01 lower bound for widthTolerance

* test: assert min/max values for large widthTolerance
  • Loading branch information
ericdeansanchez authored Jan 21, 2021
1 parent f02cfd5 commit 8079e75
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/validators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export function validateRange(min, max) {
}

export function validateWidthTolerance(widthTolerance) {
if (typeof widthTolerance != 'number' || widthTolerance <= 0) {
if (typeof widthTolerance != 'number' || widthTolerance < 0.01) {
throw new Error(
'The srcset widthTolerance argument can only be passed a positive scalar number',
'The srcset widthTolerance must be a number greater than or equal to 0.01',
);
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/test-buildSrcSet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,27 @@ describe('SrcSet Builder:', function describeSuite() {
}, Error);
});

it('produces srcset with min and max widths when widthTolerance is large', function testSpec() {
const srcset = new ImgixClient({
domain: 'testing.imgix.net',
}).buildSrcSet('image.jpg', {}, { widthTolerance: 999999.999 });

const srcsetSplit = srcset.split(',');
const actualLength = srcsetSplit.length;

const srcsetMin = Number.parseFloat(
srcsetSplit[0].split(' ')[1].slice(0, -1),
);

const srcsetMax = Number.parseFloat(
srcsetSplit[srcsetSplit.length - 1].split(' ')[1].slice(0, -1),
);

assert.strictEqual(actualLength, 2);
assert.strictEqual(srcsetMin, 100);
assert.strictEqual(srcsetMax, 8192);
});

it('memoizes generated srcset width pairs', function testSpec() {
let DEFAULT_MIN_WIDTH = 100;
let DEFAULT_MAX_WIDTH = 8192;
Expand Down
17 changes: 13 additions & 4 deletions test/test-validators.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ describe('Validators:', function () {
});
});

it('throws if widthTolerance is <= 0', () => {
it('throws if widthTolerance is < 0.01', () => {
assert.throws(() => {
validateWidthTolerance(0);
validateWidthTolerance(0.00999999999);
});
});

Expand All @@ -103,8 +103,17 @@ describe('Validators:', function () {
});
});

// TODO: should fail.
it('widthTolerance === 0.001', () => {});
it('does not throw on valid lower bound of 0.01', () => {
assert.doesNotThrow(() => {
validateWidthTolerance(0.01);
});
});

it('does not throw when passed a large value', () => {
assert.doesNotThrow(() => {
validateWidthTolerance(99999999.99);
});
});
});

describe('Testing validateVariableQuality', function () {
Expand Down

0 comments on commit 8079e75

Please sign in to comment.