This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Image.spec.tsx
191 lines (157 loc) · 7.15 KB
/
Image.spec.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { createElement } from "react";
import { mount, render } from "enzyme";
import { ImageViewer, ImageViewerProps } from "../Image/index";
import { Lightbox } from "../Lightbox";
import { ModalProps } from "react-overlays/esm/Modal";
jest.mock("../../assets/ic24-close.svg", () => "close-button-icon-svg");
jest.mock("react-overlays/Modal", () => (props: ModalProps) => {
const MockName = "react-overlays-modal-mock";
// The backdrop is rendered somewhere else in a portal, but for testing sake we put it here since we also mock.
const BackdropMockName = "react-overlays-modal-backdrop-mock";
return (
// @ts-expect-error lower case custom name to make clear it's a mock
<MockName {...props}>
{props.children}
{/* @ts-expect-error lower case custom name to make clear it's a mock */}
<BackdropMockName>{props.renderBackdrop?.({ onClick: jest.fn(), ref: jest.fn() })}</BackdropMockName>
</MockName>
);
});
const imageProps: ImageViewerProps = {
class: "",
type: "image",
image: "https://pbs.twimg.com/profile_images/1905729715/llamas_1_.jpg",
height: 300,
heightUnit: "pixels",
width: 300,
widthUnit: "pixels",
iconSize: 0,
responsive: true,
onClickType: "action",
displayAs: "fullImage"
};
const glyphiconProps: ImageViewerProps = {
class: "",
type: "icon",
image: "glyphicon-asterisk",
iconSize: 20,
height: 0,
heightUnit: "pixels",
width: 0,
widthUnit: "pixels",
responsive: true,
onClickType: "action",
displayAs: "fullImage"
};
describe("ImageViewer", () => {
it("renders the structure with an image", () => {
expect(render(<ImageViewer {...imageProps} />)).toMatchSnapshot();
});
it("renders the structure with an image and percentage dimensions", () => {
expect(
render(<ImageViewer {...imageProps} height={100} width={100} heightUnit="auto" widthUnit="percentage" />)
).toMatchSnapshot();
});
it("renders the structure with an icon", () => {
expect(render(<ImageViewer {...glyphiconProps} />)).toMatchSnapshot();
});
describe("when the onClickType is action", () => {
it("calls the onClick when clicking on an image", () => {
const onClickMock = jest.fn();
const imageViewer = mount(<ImageViewer {...imageProps} onClick={onClickMock} onClickType="action" />);
const image = imageViewer.find("img");
expect(image).toHaveLength(1);
image.simulate("click");
expect(onClickMock).toHaveBeenCalled();
});
it("calls the onClick when clicking on an icon", () => {
const onClickMock = jest.fn();
const imageViewer = mount(<ImageViewer {...glyphiconProps} onClick={onClickMock} onClickType="action" />);
const glyphicon = imageViewer.find("span");
expect(glyphicon).toHaveLength(1);
glyphicon.simulate("click");
expect(onClickMock).toHaveBeenCalled();
});
});
describe("when the onClickType is enlarge", () => {
it("shows a lightbox when the user clicks on the image", () => {
const imageViewer = mount(<ImageViewer {...imageProps} onClickType="enlarge" />);
expect(imageViewer.find(Lightbox)).toHaveLength(0);
const image = imageViewer.find("img");
expect(image).toHaveLength(1);
image.simulate("click");
expect(imageViewer.find(Lightbox)).toHaveLength(1);
});
it("closes the lightbox when the user clicks on the close button after opening it", () => {
const imageViewer = mount(<ImageViewer {...imageProps} onClickType="enlarge" />);
const image = imageViewer.find("img");
expect(image).toHaveLength(1);
image.simulate("click");
expect(imageViewer.find(Lightbox)).toHaveLength(1);
const closeButton = imageViewer.find("button");
expect(closeButton).toHaveLength(1);
closeButton.simulate("click");
expect(imageViewer.find(Lightbox)).toHaveLength(0);
});
});
it("does not trigger on clicks from containers if clicked on the image", () => {
const onClickOuterMock = jest.fn();
const onClickImageMock = jest.fn();
const imageViewer = mount(
<div onClick={onClickOuterMock}>
<ImageViewer {...imageProps} onClickType="action" onClick={onClickImageMock} />
</div>
);
const image = imageViewer.find("img");
expect(image).toHaveLength(1);
image.simulate("click");
expect(onClickImageMock).toHaveBeenCalledTimes(1);
expect(onClickOuterMock).not.toHaveBeenCalled();
});
describe("when there is an accessibility alt text", () => {
it("is set properly on an image", () => {
const imageViewer = mount(<ImageViewer {...imageProps} altText="this is an awesome image" />);
const image = imageViewer.find("img");
expect(image.prop("alt")).toBe("this is an awesome image");
});
it("is set properly on a glyphicon", () => {
const imageViewer = mount(<ImageViewer {...glyphiconProps} altText="this is an awesome icon" />);
const image = imageViewer.find("span");
expect(image.prop("aria-label")).toBe("this is an awesome icon");
expect(image.prop("role")).toBe("img");
});
});
describe("when there is no accessibility alt text", () => {
it("nothing is set on an image", () => {
const imageViewer = mount(<ImageViewer {...imageProps} />);
const image = imageViewer.find("img");
expect(image.prop("alt")).toBe(undefined);
});
it("nothing is set on a glyphicon", () => {
const imageViewer = mount(<ImageViewer {...glyphiconProps} />);
const image = imageViewer.find("span");
expect(image).not.toHaveProperty("aria-label");
expect(image).not.toHaveProperty("role");
});
});
describe("when showing an image as a thumbnail", () => {
it("includes the thumb=true URL param in the image", () => {
const imageViewer = mount(<ImageViewer {...imageProps} displayAs="thumbnail" />);
const image = imageViewer.find("img");
expect(image.prop("src")).toContain("thumb=true");
});
it("does not include the thumb=true URL param in the lightbox image", () => {
const imageViewer = mount(<ImageViewer {...imageProps} displayAs="thumbnail" onClickType="enlarge" />);
const image = imageViewer.find("img");
expect(image.prop("src")).toContain("thumb=true");
expect(image).toHaveLength(1);
image.simulate("click");
const allImages = imageViewer.findWhere(
node => node.type() === "img" && node.prop("src").startsWith(imageProps.image)
);
expect(allImages).toHaveLength(2);
expect(allImages.at(0).prop("src")).toContain("thumb=true");
expect(allImages.at(1).prop("src")).not.toContain("thumb=true");
});
});
});