Skip to content

Commit 1a33886

Browse files
test: add tests for HtmlImageLayer destroy
1 parent 8132bcb commit 1a33886

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

packages/html/__tests__/HtmlImageLayer.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {HtmlImageLayer} from "../src";
33
import {BaseAnalyticsOptions} from "../src/types";
44
import {responsive} from "../src/plugins/responsive";
55
import {placeholder} from "../src/plugins/placeholder";
6-
import {lazyload} from "../src/plugins/lazyload";
76
import {accessibility} from "../src/plugins/accessibility";
7+
import {PluginResponse} from "../types";
8+
import {cancelCurrentlyRunningPlugins} from "../src/utils/cancelCurrentlyRunningPlugins";
9+
import * as process from "process";
810

911
jest.useFakeTimers();
1012

@@ -51,4 +53,55 @@ describe('HtmlImageLayer tests', function () {
5153
await flushPromises();
5254
expect(img.src).toEqualAnalyticsToken('AXAABABD');
5355
});
56+
57+
it('should set image src twice if HtmlImageLayer instance won\'t be destroyed', async function () {
58+
const img = document.createElement('img');
59+
const pluginsState: any = {
60+
cleanupCallbacks: []
61+
};
62+
const spy = jest.spyOn(img, 'setAttribute');
63+
const dummyFailingPlugin = (): Promise<PluginResponse> => {
64+
return new Promise((resolve) => {
65+
pluginsState.cleanupCallbacks.push(() => {
66+
resolve('canceled')
67+
})
68+
})
69+
}
70+
const dummyLazyLoadPlugin = (): Promise<PluginResponse> => {
71+
return new Promise((resolve) => {
72+
resolve({lazyload: true});
73+
})
74+
}
75+
new HtmlImageLayer(img, cldImage, [dummyFailingPlugin]);
76+
new HtmlImageLayer(img, cldImage, [dummyLazyLoadPlugin]);
77+
cancelCurrentlyRunningPlugins(pluginsState);
78+
await flushPromises();
79+
expect(spy).toHaveBeenCalledTimes(2);
80+
});
81+
82+
it('should set image src only once if HtmlImageLayer instance will be destroyed', async function () {
83+
const img = document.createElement('img');
84+
const pluginsState: any = {
85+
cleanupCallbacks: []
86+
};
87+
const spy = jest.spyOn(img, 'setAttribute');
88+
const dummyFailingPlugin = (): Promise<PluginResponse> => {
89+
return new Promise((resolve) => {
90+
pluginsState.cleanupCallbacks.push(() => {
91+
resolve('canceled')
92+
})
93+
})
94+
}
95+
const dummyLazyLoadPlugin = (): Promise<PluginResponse> => {
96+
return new Promise((resolve) => {
97+
resolve({lazyload: true});
98+
})
99+
}
100+
const instance1 = new HtmlImageLayer(img, cldImage, [dummyFailingPlugin]);
101+
new HtmlImageLayer(img, cldImage, [dummyLazyLoadPlugin]);
102+
cancelCurrentlyRunningPlugins(pluginsState);
103+
instance1.destroy();
104+
await flushPromises();
105+
expect(spy).toHaveBeenCalledTimes(1);
106+
});
54107
});

0 commit comments

Comments
 (0)