Skip to content

Commit 0a9f5ec

Browse files
test: add placeholder and responsive plugin test
1 parent 0429162 commit 0a9f5ec

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

packages/html/__tests__/HtmlImageLayer.test.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('HtmlImageLayer tests', function () {
9696
expect(spy).toHaveBeenCalledTimes(1);
9797
});
9898

99-
it('should verfiy no unneeded request with placeholder plugin', async function () {
99+
it('should verifiy no unneeded request with placeholder plugin', async function () {
100100
const OriginalImage = Image;
101101
// mocking Image constructor in order to simulate firing 'load' event
102102
jest.spyOn(global, "Image").mockImplementation(() => {
@@ -128,31 +128,32 @@ describe('HtmlImageLayer tests', function () {
128128
expect(imgSetAttributeSpy.mock.calls[0][1]).toEqualAnalyticsToken('BAXAABABB');
129129
});
130130

131-
it('should verfiy no responsive image request is fired with placeholder plugin', async function () {
131+
it('should verifiy no responsive image request is fired with placeholder plugin', async function () {
132+
// Set mock screen width
133+
Object.defineProperty(HTMLElement.prototype, 'clientWidth', { configurable: true, value: 640 })
134+
132135
const OriginalImage = Image;
133-
// mocking Image constructor in order to simulate firing 'load' event
134-
jest.spyOn(global, "Image").mockImplementation(() => {
135-
const img = new OriginalImage();
136-
setTimeout(() => {
137-
img.dispatchEvent(new Event("load"));
138-
}, 10)
136+
// mock Image constructor in order to simulate firing 'load' event
137+
jest.spyOn(global, "Image").mockImplementationOnce(() => {
138+
img = new OriginalImage();
139139
return img;
140-
141140
})
142141
const parentElement = document.createElement('div');
143142
const img = document.createElement('img');
144143
parentElement.append(img);
145-
const imgSrcSpy = jest.spyOn(img, 'src', 'set');
146-
const imgSetAttributeSpy = jest.spyOn(img, 'setAttribute');
147144
new HtmlImageLayer(img, cldImage, [responsive({steps: 200}),placeholder()], sdkAnalyticsTokens);
145+
148146
await flushPromises();
149-
expect(imgSrcSpy).toHaveBeenCalledTimes(1);
150-
// test that the initial src is set to a token contains last character "B" which is the character of placeholder plugin
151-
const imgSrcSpyAnalyticsToken = imgSrcSpy.mock.calls[0][0];
152-
expect(imgSrcSpyAnalyticsToken).toEqualAnalyticsToken('BAXAABABB');
147+
148+
// the initial src is set to a placeholder image
149+
expect(img.src).toBe("https://res.cloudinary.com/demo/image/upload/e_vectorize/q_auto/f_svg/sample?_a=BAXAABABB");
153150
await flushPromises();
151+
152+
// simulate placeholder image loaded
153+
img.dispatchEvent(new Event("load"));
154154
await flushPromises();
155-
//TODO we want to check the second image that is loaded for the presence of a w_ paramter
156-
expect(img.src).toBe("abc");
155+
156+
// the second image that is loaded should have the responsive width set
157+
expect(img.src).toBe("https://res.cloudinary.com/demo/image/upload/c_scale,w_800/sample?_a=BAXAABABB");
157158
});
158159
});

0 commit comments

Comments
 (0)