|
| 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 | +}); |
0 commit comments