Skip to content

Commit 47283b5

Browse files
author
Nir Maoz
authored
test/vue3 lazyload (#158)
1 parent d7f1ede commit 47283b5

File tree

5 files changed

+101
-11
lines changed

5 files changed

+101
-11
lines changed

packages/react/__tests__/lazyload.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AdvancedImage, lazyload } from '../src'
22
import { CloudinaryImage } from '@cloudinary/url-gen/assets/CloudinaryImage';
33
import { mount } from 'enzyme';
44
import React from 'react';
5-
import testWithMockedIntersectionObserver from './testUtils/setupIntersectionObserverMock';
5+
import { testWithMockedIntersectionObserver } from '../../../testUtils/testWithMockedIntersectionObserver'
66

77
const cloudinaryImage = new CloudinaryImage('sample', { cloudName: 'demo' }, { analytics: false });
88

packages/vue3/tests/unit/AdvancedImage.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mount } from "@vue/test-utils";
22
import { AdvancedImage } from "../../src/index";
33
import { CloudinaryImage } from "@cloudinary/url-gen/assets/CloudinaryImage";
4-
import { nextTick } from "vue";
4+
import { waitTicks } from "./utils";
55

66
const cloudinaryImage = new CloudinaryImage(
77
"sample",
@@ -20,7 +20,7 @@ describe("AdvancedImage.vue", () => {
2020
});
2121

2222
// wait because @cloudinary/html takes time to update the img element
23-
await nextTick();
23+
await waitTicks(1);
2424

2525
expect(component.html()).toContain(
2626
'<img src="https://res.cloudinary.com/demo/image/upload/sample"'
@@ -36,7 +36,7 @@ describe("AdvancedImage.vue", () => {
3636
});
3737

3838
// wait because @cloudinary/html takes time to update the img element
39-
await nextTick();
39+
await waitTicks(1);
4040

4141
expect(component.html()).toContain('style="opacity: 0.5;"');
4242
});
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { AdvancedImage, lazyload } from "../../src/index";
2+
import { CloudinaryImage } from "@cloudinary/url-gen/assets/CloudinaryImage";
3+
import { mount } from "@vue/test-utils";
4+
import { waitTicks } from "./utils";
5+
import { testWithMockedIntersectionObserver } from "../../../../testUtils/testWithMockedIntersectionObserver";
6+
7+
const cloudinaryImage = new CloudinaryImage(
8+
"sample",
9+
{ cloudName: "demo" },
10+
{ analytics: false }
11+
);
12+
13+
describe("lazy-load", () => {
14+
it("should not have src pre-scroll", async () => {
15+
const rootDiv = document.createElement("div");
16+
rootDiv.id = "root";
17+
const div = document.createElement("div");
18+
div.style.setProperty("height", "10000px");
19+
rootDiv.appendChild(div);
20+
document.body.appendChild(rootDiv);
21+
22+
testWithMockedIntersectionObserver(
23+
// eslint-disable-next-line no-empty-pattern
24+
async (mockIntersectionEvent: ({}) => void) => {
25+
const component = mount(AdvancedImage, {
26+
props: { cldImg: cloudinaryImage, plugins: [lazyload()] },
27+
attachTo: "#root",
28+
});
29+
mockIntersectionEvent([
30+
{ isIntersecting: false, target: component.element },
31+
]);
32+
await waitTicks(2);
33+
// no src pre scroll
34+
expect(component.html()).toBe("<img>");
35+
expect(component.html()).not.toContain(
36+
'src="https://res.cloudinary.com/demo/image/upload/sample"'
37+
);
38+
}
39+
);
40+
});
41+
42+
it("should have src when in view", async () => {
43+
testWithMockedIntersectionObserver(
44+
// eslint-disable-next-line no-empty-pattern
45+
async (mockIntersectionEvent: ({}) => void) => {
46+
const component = mount(AdvancedImage, {
47+
props: { cldImg: cloudinaryImage, plugins: [lazyload()] },
48+
});
49+
mockIntersectionEvent([
50+
{ isIntersecting: true, target: component.element },
51+
]);
52+
await waitTicks(2);
53+
54+
expect(component.html()).toContain(
55+
'src="https://res.cloudinary.com/demo/image/upload/sample"'
56+
);
57+
}
58+
);
59+
});
60+
61+
it("should set lazyload root margin and threshold", async () => {
62+
testWithMockedIntersectionObserver(
63+
// eslint-disable-next-line no-empty-pattern
64+
async (mockIntersectionEvent: ({}) => void) => {
65+
const component = mount(AdvancedImage, {
66+
props: {
67+
cldImg: cloudinaryImage,
68+
plugins: [lazyload({ rootMargin: "10px", threshold: 0.5 })],
69+
},
70+
});
71+
mockIntersectionEvent([
72+
{ isIntersecting: true, target: component.element },
73+
]);
74+
await waitTicks(2);
75+
expect(component.html()).toContain(
76+
'src="https://res.cloudinary.com/demo/image/upload/sample"'
77+
);
78+
}
79+
);
80+
});
81+
});

packages/vue3/tests/unit/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
2+
// @ts-ignore
13
import { nextTick } from "vue";
24

35
/**
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
function testWithMockedIntersectionObserver(cb: (...args:any) => void) {
2-
let intersectCallback: (root?: Element, rootMargin?: string, thresholds?: number) => void = () => {}; // will be
1+
export const testWithMockedIntersectionObserver = (
2+
cb: (...args: any) => void
3+
) => {
4+
let intersectCallback: (
5+
root?: Element,
6+
rootMargin?: string,
7+
thresholds?: number
8+
) => void = () => {}; // will be
39
// populated later
4-
let nativeIntersectionObserver = global.IntersectionObserver;
10+
const nativeIntersectionObserver = global.IntersectionObserver;
511

612
// Mock IntersectionObserver
13+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
714
// @ts-ignore
815
global.IntersectionObserver = class {
9-
constructor(cb: (root?: Element, rootMargin?: string, thresholds?: number) => any) {
16+
constructor(
17+
cb: (root?: Element, rootMargin?: string, thresholds?: number) => any
18+
) {
1019
// This is the callback that notifies when an intersection occurs
1120
// We'll store it to use it later
1221
intersectCallback = cb;
@@ -20,6 +29,4 @@ function testWithMockedIntersectionObserver(cb: (...args:any) => void) {
2029

2130
// restore
2231
global.IntersectionObserver = nativeIntersectionObserver;
23-
}
24-
25-
export default testWithMockedIntersectionObserver;
32+
};

0 commit comments

Comments
 (0)