Skip to content

Commit 24dba96

Browse files
author
Nir Maoz
authored
test: vue3 placeholder (#159)
1 parent 47283b5 commit 24dba96

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { AdvancedImage, placeholder } from "../../src/index";
2+
import { CloudinaryImage } from "@cloudinary/url-gen/assets/CloudinaryImage";
3+
import { PLACEHOLDER_IMAGE_OPTIONS } from "../../../html/src/utils/internalConstants";
4+
import { sepia } from "@cloudinary/url-gen/actions/effect";
5+
import { mount } from "@vue/test-utils";
6+
import { waitTicks } from "./utils";
7+
8+
describe("placeholder", () => {
9+
let cloudinaryImage: CloudinaryImage;
10+
11+
const mockImage = {
12+
src: null,
13+
onload: () => {},
14+
onerror: () => {},
15+
};
16+
beforeEach(() => {
17+
// @ts-ignore
18+
window.Image = function () {
19+
return mockImage;
20+
};
21+
cloudinaryImage = new CloudinaryImage(
22+
"sample",
23+
{ cloudName: "demo" },
24+
{ analytics: false }
25+
);
26+
});
27+
it("should apply default", async () => {
28+
const component = mount(AdvancedImage, {
29+
props: { cldImg: cloudinaryImage, plugins: [placeholder()] },
30+
});
31+
await waitTicks(1);
32+
33+
expect(component.html()).toContain(
34+
`src="https://res.cloudinary.com/demo/image/upload/${PLACEHOLDER_IMAGE_OPTIONS.vectorize}/sample"`
35+
);
36+
});
37+
38+
it("should apply 'vectorize'", async () => {
39+
const component = mount(AdvancedImage, {
40+
props: {
41+
cldImg: cloudinaryImage,
42+
plugins: [placeholder({ mode: "vectorize" })],
43+
},
44+
});
45+
await waitTicks(1);
46+
47+
expect(component.html()).toContain(
48+
`src="https://res.cloudinary.com/demo/image/upload/${PLACEHOLDER_IMAGE_OPTIONS.vectorize}/sample"`
49+
);
50+
});
51+
52+
it("should apply pixelate", async () => {
53+
const component = mount(AdvancedImage, {
54+
props: {
55+
cldImg: cloudinaryImage,
56+
plugins: [placeholder({ mode: "pixelate" })],
57+
},
58+
});
59+
await waitTicks(1);
60+
61+
expect(component.html()).toContain(
62+
`src="https://res.cloudinary.com/demo/image/upload/${PLACEHOLDER_IMAGE_OPTIONS.pixelate}/sample"`
63+
);
64+
});
65+
66+
it("should apply blur", async () => {
67+
const component = mount(AdvancedImage, {
68+
props: {
69+
cldImg: cloudinaryImage,
70+
plugins: [placeholder({ mode: "blur" })],
71+
},
72+
});
73+
await waitTicks(1);
74+
75+
expect(component.html()).toContain(
76+
`src="https://res.cloudinary.com/demo/image/upload/${PLACEHOLDER_IMAGE_OPTIONS.blur}/sample"`
77+
);
78+
});
79+
80+
it("should apply predominant-color", async () => {
81+
const component = mount(AdvancedImage, {
82+
props: {
83+
cldImg: cloudinaryImage,
84+
plugins: [placeholder({ mode: "predominant-color" })],
85+
},
86+
});
87+
await waitTicks(1);
88+
89+
expect(component.html()).toContain(
90+
`src="https://res.cloudinary.com/demo/image/upload/${PLACEHOLDER_IMAGE_OPTIONS["predominant-color"]}/sample"`
91+
);
92+
});
93+
94+
it("should default if supplied with incorrect mode", async () => {
95+
const component = mount(AdvancedImage, {
96+
props: {
97+
cldImg: cloudinaryImage,
98+
plugins: [placeholder({ mode: "ddd" })],
99+
},
100+
});
101+
await waitTicks(1);
102+
103+
expect(component.html()).toContain(
104+
`src="https://res.cloudinary.com/demo/image/upload/${PLACEHOLDER_IMAGE_OPTIONS.vectorize}/sample"`
105+
);
106+
});
107+
108+
it("should append placeholder transformation", async () => {
109+
cloudinaryImage.effect(sepia());
110+
const component = mount(AdvancedImage, {
111+
props: {
112+
cldImg: cloudinaryImage,
113+
plugins: [placeholder()],
114+
},
115+
});
116+
117+
await waitTicks(2);
118+
expect(component.html()).toContain(
119+
`src="https://res.cloudinary.com/demo/image/upload/e_sepia/${PLACEHOLDER_IMAGE_OPTIONS.vectorize}/sample"`
120+
);
121+
});
122+
123+
it("should not fail on error", async () => {
124+
const component = mount(AdvancedImage, {
125+
props: { cldImg: cloudinaryImage, plugins: [placeholder()] },
126+
});
127+
// @ts-ignore
128+
component.element.onload(); // simulate element onload
129+
mockImage.onerror(); // simulate image onerror
130+
131+
await waitTicks(2);
132+
expect(mockImage.src).toBe(
133+
"https://res.cloudinary.com/demo/image/upload/sample"
134+
);
135+
});
136+
});

0 commit comments

Comments
 (0)