-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathEvaluatedValuePopup.test.tsx
More file actions
216 lines (176 loc) · 6.33 KB
/
Copy pathEvaluatedValuePopup.test.tsx
File metadata and controls
216 lines (176 loc) · 6.33 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import store from "store";
import React from "react";
import { ThemeProvider } from "styled-components";
import { Provider } from "react-redux";
import { fireEvent, render, screen } from "@testing-library/react";
import EvaluatedValuePopup, {
getEvaluatedPopupPlacement,
} from "./EvaluatedValuePopup";
import { theme } from "constants/DefaultTheme";
import { EditorTheme } from "./EditorConfig";
import { LOCAL_STORAGE_KEYS } from "utils/localStorage";
function popupJsx(props?: {
hasError?: boolean;
hideEvaluatedValue?: boolean;
isOpen?: boolean;
}) {
return (
<Provider store={store}>
<ThemeProvider theme={theme}>
<EvaluatedValuePopup
errors={[]}
hasError={props?.hasError || false}
hideEvaluatedValue={props?.hideEvaluatedValue || false}
isOpen={props?.isOpen ?? true}
theme={EditorTheme.LIGHT}
>
<div>children</div>
</EvaluatedValuePopup>
</ThemeProvider>
</Provider>
);
}
function renderPopup(props?: {
hasError?: boolean;
hideEvaluatedValue?: boolean;
isOpen?: boolean;
}) {
return render(popupJsx(props));
}
// Drags the popup by its handle so its internal dragged-position state is
// set, exactly as a user drag would. jsdom elements measure 0x0, and a
// zero-size popup is treated as closed on mouseup, so give the popper
// element a real box first.
function dragPopupTo(clientX: number, clientY: number) {
const popperElement = document.querySelector(".t--CodeEditor-evaluatedValue")
?.parentElement as HTMLElement;
popperElement.getBoundingClientRect = () =>
({
left: 100,
top: 100,
width: 300,
height: 118,
right: 400,
bottom: 218,
x: 100,
y: 100,
toJSON: () => ({}),
}) as DOMRect;
const handle = document.querySelector(
"[id$='-popper-draghandler']",
) as HTMLElement;
fireEvent.mouseDown(handle, { clientX: 10, clientY: 10 });
fireEvent.mouseMove(document, { clientX, clientY });
fireEvent.mouseUp(document, { clientX, clientY });
return popperElement;
}
describe("EvaluatedValuePopup", () => {
beforeEach(() => {
localStorage.removeItem(LOCAL_STORAGE_KEYS.EVALUATED_VALUE_POPUP_COLLAPSED);
});
afterEach(() => {
jest.restoreAllMocks();
});
it("should render evaluated popup when hideEvaluatedValue is false", () => {
renderPopup();
const input = screen.queryByTestId("evaluated-value-popup-title");
expect(input).toBeTruthy();
});
it("should not render evaluated popup when hideEvaluatedValue is true", () => {
renderPopup({ hideEvaluatedValue: true });
const input = screen.queryByTestId("evaluated-value-popup-title");
expect(input).toBeNull();
});
it("should collapse the popup and persist the preference when the toggle is clicked", () => {
renderPopup();
fireEvent.click(screen.getByTestId("t--evaluated-popup-collapse-toggle"));
expect(screen.queryByTestId("evaluated-value-popup-title")).toBeNull();
expect(
localStorage.getItem(LOCAL_STORAGE_KEYS.EVALUATED_VALUE_POPUP_COLLAPSED),
).toBe("true");
});
it("should render collapsed when the persisted preference is set", () => {
localStorage.setItem(
LOCAL_STORAGE_KEYS.EVALUATED_VALUE_POPUP_COLLAPSED,
"true",
);
renderPopup();
expect(screen.queryByTestId("evaluated-value-popup-title")).toBeNull();
fireEvent.click(screen.getByTestId("t--evaluated-popup-collapse-toggle"));
expect(screen.getByTestId("evaluated-value-popup-title")).toBeTruthy();
expect(
localStorage.getItem(LOCAL_STORAGE_KEYS.EVALUATED_VALUE_POPUP_COLLAPSED),
).toBe("false");
});
it("should place the popup left of fields on the right half of the viewport (property pane)", () => {
expect(getEvaluatedPopupPlacement(900, 1440, 36)).toEqual([
"left-start",
"0, 15",
]);
expect(getEvaluatedPopupPlacement(720, 1440, 300)).toEqual([
"left-start",
"0, 15",
]);
});
it("should place the popup below short wide-form fields on the left half of the viewport", () => {
expect(getEvaluatedPopupPlacement(100, 1440, 36)).toEqual([
"bottom-start",
"0, 8",
]);
expect(getEvaluatedPopupPlacement(719, 1440, 60)).toEqual([
"bottom-start",
"0, 8",
]);
});
it("should keep the popup on the left for tall multiline editors on the left half", () => {
expect(getEvaluatedPopupPlacement(300, 1440, 61)).toEqual([
"left-start",
"0, 5",
]);
expect(getEvaluatedPopupPlacement(100, 1440, 500)).toEqual([
"left-start",
"0, 5",
]);
});
it("should show an error indicator on the collapsed pill when there is an error", () => {
localStorage.setItem(
LOCAL_STORAGE_KEYS.EVALUATED_VALUE_POPUP_COLLAPSED,
"true",
);
renderPopup({ hasError: true });
expect(
screen.getByTestId("t--evaluated-popup-error-indicator"),
).toBeTruthy();
});
it("should keep the dragged position across a window-level blur (minimize / app switch)", () => {
jest.spyOn(document, "hasFocus").mockReturnValue(false);
const { rerender } = renderPopup();
// the anchor ref is null on the very first render; a second render lets
// the popper attach and create its drag handle
rerender(popupJsx({ isOpen: true }));
dragPopupTo(60, 60);
// window loses focus -> editor blurs -> popup closes
rerender(popupJsx({ isOpen: false }));
// window restored, editor refocuses -> popup reopens
rerender(popupJsx({ isOpen: true }));
const popperElement = document.querySelector(
".t--CodeEditor-evaluatedValue",
)?.parentElement as HTMLElement;
expect(popperElement.style.top).toBe("100px");
expect(popperElement.style.left).toBe("100px");
});
it("should re-anchor after an in-app blur discards the dragged position", () => {
jest.spyOn(document, "hasFocus").mockReturnValue(true);
const { rerender } = renderPopup();
rerender(popupJsx({ isOpen: true }));
dragPopupTo(60, 60);
// the user moves on inside the app: close and reopen
rerender(popupJsx({ isOpen: false }));
rerender(popupJsx({ isOpen: true }));
const popperElement = document.querySelector(
".t--CodeEditor-evaluatedValue",
)?.parentElement as HTMLElement;
expect(popperElement.style.top).not.toBe("100px");
expect(popperElement.style.left).not.toBe("100px");
});
});