Skip to content

Commit c6bbe97

Browse files
test(unit): refactor buildSrcSet tests and test width tolerance
1 parent 809f5e7 commit c6bbe97

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

tests/unit/build-src-set.spec.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,48 @@ describe('buildSrcSet', () => {
2121
expect(firstSrcSet[1]).toMatch(/^[0-9]+w$/);
2222
});
2323

24-
it('custom widths are passed to imgix-core-js', () => {
25-
jest.resetModules();
26-
jest.mock('imgix-core-js');
27-
const { buildImgixClient } = require('@/plugins/vue-imgix/');
28-
const ImgixClient = require('imgix-core-js');
29-
const ImgixMock = {
30-
settings: {},
31-
buildSrcSet: jest.fn(),
32-
buildURL: jest.fn(),
33-
};
34-
ImgixClient.mockImplementation(() => ImgixMock);
35-
36-
const client = buildImgixClient({
37-
domain: 'testing.imgix.net',
24+
describe('srcset generation', () => {
25+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
let mockImgixClient: any;
27+
let vueImgixClient: IVueImgixClient;
28+
beforeEach(() => {
29+
jest.resetModules();
30+
jest.mock('imgix-core-js');
31+
const { buildImgixClient } = require('@/plugins/vue-imgix/');
32+
const ImgixClient = require('imgix-core-js');
33+
mockImgixClient = {
34+
settings: {},
35+
buildSrcSet: jest.fn(),
36+
buildURL: jest.fn(),
37+
};
38+
ImgixClient.mockImplementation(() => mockImgixClient);
39+
vueImgixClient = buildImgixClient({
40+
domain: 'testing.imgix.net',
41+
});
3842
});
43+
afterAll(() => {
44+
jest.resetAllMocks();
45+
jest.resetModules();
46+
});
47+
it('custom widths are passed to imgix-core-js', () => {
48+
vueImgixClient.buildSrcSet('image.jpg', {}, { widths: [100, 200] });
3949

40-
client.buildSrcSet('image.jpg', {}, { widths: [100, 200] });
41-
42-
expect(ImgixMock.buildSrcSet).toHaveBeenCalledWith(
43-
expect.anything(),
44-
expect.anything(),
45-
expect.objectContaining({
46-
widths: [100, 200],
47-
}),
48-
);
50+
expect(mockImgixClient.buildSrcSet).toHaveBeenCalledWith(
51+
expect.anything(),
52+
expect.anything(),
53+
expect.objectContaining({
54+
widths: [100, 200],
55+
}),
56+
);
57+
});
58+
it('a custom width tolerance is passed to imgix-core-js', () => {
59+
vueImgixClient.buildSrcSet('image.jpg', {}, { widthTolerance: 0.2 });
4960

50-
jest.resetAllMocks();
51-
jest.resetModules();
61+
expect(mockImgixClient.buildSrcSet).toHaveBeenCalledWith(
62+
expect.anything(),
63+
expect.anything(),
64+
expect.objectContaining({ widthTolerance: 0.2 }),
65+
);
66+
});
5267
});
5368
});

0 commit comments

Comments
 (0)