|
1 | 1 | import {CloudinaryImage} from "@cloudinary/url-gen"; |
2 | 2 | import {HtmlImageLayer} from "../src"; |
3 | | -import {AnalyticsOptions} from "../src/types"; |
| 3 | +import {BaseAnalyticsOptions} from "../src/types"; |
| 4 | +import {responsive} from "../src/plugins/responsive"; |
| 5 | +import {placeholder} from "../src/plugins/placeholder"; |
| 6 | +import {lazyload} from "../src/plugins/lazyload"; |
| 7 | +import {accessibility} from "../src/plugins/accessibility"; |
4 | 8 |
|
5 | | -const sdkAnalyticsTokens:AnalyticsOptions = { |
| 9 | +jest.useFakeTimers(); |
| 10 | + |
| 11 | +const sdkAnalyticsTokens: BaseAnalyticsOptions = { |
6 | 12 | sdkSemver: '1.0.0', |
7 | 13 | techVersion: '1.0.0', |
8 | 14 | sdkCode: 'X' |
9 | 15 | } |
10 | 16 |
|
| 17 | +const flushPromises = async () => { |
| 18 | + jest.advanceTimersByTime(100); |
| 19 | + await Promise.resolve(); |
| 20 | +} |
| 21 | + |
11 | 22 | describe('HtmlImageLayer tests', function () { |
12 | 23 | const cldImage = new CloudinaryImage('sample', {cloudName: 'demo'}); |
13 | 24 |
|
14 | | - it('should set image src', function () { |
| 25 | + it('should set image src', async function () { |
15 | 26 | const img = document.createElement('img'); |
16 | 27 | new HtmlImageLayer(img, cldImage, [], sdkAnalyticsTokens); |
17 | | - process.nextTick(() => { |
18 | | - expect(img.src).toEqual('https://res.cloudinary.com/demo/image/upload/sample?_a=AXAABAB0') |
19 | | - }) |
| 28 | + await flushPromises(); |
| 29 | + expect(img.src).toEqualAnalyticsToken('AXAABAB0'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should use feature flag for responsive plugin', async function () { |
| 33 | + const parentElement = document.createElement('div'); |
| 34 | + const img = document.createElement('img'); |
| 35 | + parentElement.append(img); |
| 36 | + new HtmlImageLayer(img, cldImage, [responsive({steps: [100]})], sdkAnalyticsTokens); |
| 37 | + await flushPromises(); |
| 38 | + expect(img.src).toEqualAnalyticsToken('AXAABABA'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should use feature flag for placeholder plugin', async function () { |
| 42 | + const img = document.createElement('img'); |
| 43 | + new HtmlImageLayer(img, cldImage, [placeholder()], sdkAnalyticsTokens); |
| 44 | + await flushPromises(); |
| 45 | + expect(img.src).toEqualAnalyticsToken('AXAABABB'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should use feature flag for accessibility plugin', async function () { |
| 49 | + const img = document.createElement('img'); |
| 50 | + new HtmlImageLayer(img, cldImage, [accessibility()], sdkAnalyticsTokens); |
| 51 | + await flushPromises(); |
| 52 | + expect(img.src).toEqualAnalyticsToken('AXAABABD'); |
20 | 53 | }); |
21 | 54 | }); |
0 commit comments