Skip to content

Commit 4ff42be

Browse files
Merge pull request #206 from cloudinary/fix-placeholder-plugin-token
fix: fix placeholder plugin token
2 parents cf58917 + fbdf2b8 commit 4ff42be

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

packages/html/__tests__/HtmlImageLayer.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const sdkAnalyticsTokens: BaseAnalyticsOptions = {
1616
}
1717

1818
const flushPromises = async () => {
19-
jest.advanceTimersByTime(100);
19+
jest.advanceTimersByTime(10);
2020
await Promise.resolve();
2121
}
2222

@@ -95,4 +95,36 @@ describe('HtmlImageLayer tests', function () {
9595
await flushPromises();
9696
expect(spy).toHaveBeenCalledTimes(1);
9797
});
98+
99+
it('should verfiy no unneeded request with placeholder plugin', async function () {
100+
const OriginalImage = Image;
101+
// mocking Image constructor in order to simulate firing 'load' event
102+
jest.spyOn(global, "Image").mockImplementation(() => {
103+
const img = new OriginalImage();
104+
setTimeout(() => {
105+
img.dispatchEvent(new Event("load"));
106+
}, 10)
107+
return img;
108+
109+
})
110+
const img = document.createElement('img');
111+
const imgSrcSpy = jest.spyOn(img, 'src', 'set');
112+
const imgSetAttributeSpy = jest.spyOn(img, 'setAttribute');
113+
114+
new HtmlImageLayer(img, cldImage, [placeholder()], sdkAnalyticsTokens);
115+
expect(imgSrcSpy).toHaveBeenCalledTimes(1);
116+
// test that the initial src is set to a token contains last character "B" which is the character of placeholder plugin
117+
const imgSrcSpyAnalyticsToken = imgSrcSpy.mock.calls[0][0];
118+
expect(imgSrcSpyAnalyticsToken).toEqualAnalyticsToken('AXAABABB');
119+
// trigger load event in order to resolve the 1st promise of the placeholder plugin
120+
img.dispatchEvent(new Event('load'));
121+
await flushPromises();
122+
// resolve the 2nd promise of the placeholder plugin, which cause the placeholder plugin to create the pixelated Image
123+
await flushPromises();
124+
// resolve the 3rd promise of the placeholder plugin, which cause HtmlLayer to set the image attribute
125+
await flushPromises();
126+
expect(imgSetAttributeSpy).toHaveBeenCalledTimes(1);
127+
// test that the src which set by HtmlImageLayer contains last character "B" which is the character of placeholder plugin
128+
expect(imgSetAttributeSpy.mock.calls[0][1]).toEqualAnalyticsToken('AXAABABB');
129+
});
98130
});

packages/html/src/plugins/placeholder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function placeholder({mode='vectorize'}:{mode?: string}={}): Plugin{
2626
* @param element {HTMLImageElement} The image element.
2727
* @param pluginCloudinaryImage {CloudinaryImage}
2828
* @param htmlPluginState {htmlPluginState} Holds cleanup callbacks and event subscriptions.
29-
* @param analyticsOptions {BaseAnalyticsOptions} analytics options for the url to be created
29+
* @param baseAnalyticsOptions {BaseAnalyticsOptions} analytics options for the url to be created
3030
*/
3131
function placeholderPlugin(mode: PlaceholderMode, element: HTMLImageElement, pluginCloudinaryImage: CloudinaryImage, htmlPluginState: HtmlPluginState, baseAnalyticsOptions?: BaseAnalyticsOptions): Promise<PluginResponse> | boolean {
3232
// @ts-ignore
@@ -104,12 +104,12 @@ function placeholderPlugin(mode: PlaceholderMode, element: HTMLImageElement, plu
104104
const largeImage = new Image();
105105
largeImage.src = pluginCloudinaryImage.toURL(analyticsOptions);
106106
largeImage.onload = () => {
107-
resolve();
107+
resolve({placeholder: true});
108108
};
109109

110110
// image does not load, resolve
111111
largeImage.onerror = () => {
112-
resolve();
112+
resolve({placeholder: true});
113113
};
114114
});
115115
});

0 commit comments

Comments
 (0)