@khanacademy/wonder-blocks-layout 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,274 +0,0 @@
1
- import {resizeWindow, checkQuery, matchMedia} from "./test-util";
2
-
3
- describe("Test utils", () => {
4
- describe("resizeWindow", () => {
5
- it("should set the width to 1200px for large", () => {
6
- // Arrange
7
- const expectedWidth = 1200;
8
-
9
- // Act
10
- resizeWindow("large");
11
-
12
- // Assert
13
- expect(window.innerWidth).toBe(expectedWidth);
14
- });
15
-
16
- it("should set the width to 800px for medium", () => {
17
- // Arrange
18
- const expectedWidth = 800;
19
-
20
- // Act
21
- resizeWindow("medium");
22
-
23
- // Assert
24
- expect(window.innerWidth).toBe(expectedWidth);
25
- });
26
-
27
- it("should set the width to 640px for small", () => {
28
- // Arrange
29
- const expectedWidth = 640;
30
-
31
- // Act
32
- resizeWindow("small");
33
-
34
- // Assert
35
- expect(window.innerWidth).toBe(expectedWidth);
36
- });
37
-
38
- it("should dispatch a 'resize' event", async () => {
39
- // Arrange
40
- let handler: any;
41
- const promise = new Promise((resolve: any, reject: any) => {
42
- handler = window.addEventListener("resize", resolve);
43
- });
44
-
45
- // Act
46
- resizeWindow("small");
47
- await promise;
48
-
49
- // Assert
50
- await expect(promise).resolves.toHaveProperty("type", "resize");
51
- window.removeEventListener("resize", handler);
52
- });
53
- });
54
-
55
- describe("checkQuery", () => {
56
- describe("(max-width: 767px)", () => {
57
- it("should match 700px", () => {
58
- // Arrange
59
- const width = 700;
60
-
61
- // Act
62
- const result = checkQuery("(max-width: 767px)", width);
63
-
64
- // Assert
65
- expect(result).toBe(true);
66
- });
67
-
68
- it("should not match 800px", () => {
69
- // Arrange
70
- const width = 800;
71
-
72
- // Act
73
- const result = checkQuery("(max-width: 767px)", width);
74
-
75
- // Assert
76
- expect(result).toBe(false);
77
- });
78
- });
79
-
80
- describe("(min-width: 768px) and (max-width: 1023px)", () => {
81
- it("should not match 700px", () => {
82
- // Arrange
83
- const width = 700;
84
-
85
- // Act
86
- const result = checkQuery(
87
- "(min-width: 768px) and (max-width: 1023px)",
88
- width,
89
- );
90
-
91
- // Assert
92
- expect(result).toBe(false);
93
- });
94
-
95
- it("should match 800px", () => {
96
- // Arrange
97
- const width = 800;
98
-
99
- // Act
100
- const result = checkQuery(
101
- "(min-width: 768px) and (max-width: 1023px)",
102
- width,
103
- );
104
-
105
- // Assert
106
- expect(result).toBe(true);
107
- });
108
-
109
- it("should not match 1024px", () => {
110
- // Arrange
111
- const width = 1024;
112
-
113
- // Act
114
- const result = checkQuery(
115
- "(min-width: 768px) and (max-width: 1023px)",
116
- width,
117
- );
118
-
119
- // Assert
120
- expect(result).toBe(false);
121
- });
122
- });
123
-
124
- describe("(min-width: 1024px)", () => {
125
- it("should not match 800px", () => {
126
- // Arrange
127
- const width = 800;
128
-
129
- // Act
130
- const result = checkQuery("(min-width: 1024px)", width);
131
-
132
- // Assert
133
- expect(result).toBe(false);
134
- });
135
-
136
- it("should match 1024px", () => {
137
- // Arrange
138
- const width = 1024;
139
-
140
- // Act
141
- const result = checkQuery("(min-width: 1024px)", width);
142
-
143
- // Assert
144
- expect(result).toBe(true);
145
- });
146
- });
147
- });
148
-
149
- describe("matchMedia", () => {
150
- describe("matches", () => {
151
- it("should be true when the window matches the initial value", () => {
152
- // Arrange
153
- resizeWindow("small");
154
-
155
- // Act
156
- const watcher = matchMedia("(max-width: 767px)");
157
-
158
- // Assert
159
- expect(watcher.matches).toBe(true);
160
- });
161
-
162
- it("should be false when the window doesn't match the initial value", () => {
163
- // Arrange
164
- resizeWindow("large");
165
-
166
- // Act
167
- const watcher = matchMedia("(max-width: 767px)");
168
-
169
- // Assert
170
- expect(watcher.matches).toBe(false);
171
- });
172
-
173
- it("should be true when the window matches the current value", () => {
174
- // Arrange
175
- resizeWindow("large");
176
-
177
- // Act
178
- const watcher = matchMedia("(max-width: 767px)");
179
- resizeWindow("small");
180
-
181
- // Assert
182
- expect(watcher.matches).toBe(true);
183
- });
184
-
185
- it("should be true when the window doesn't match the current value", () => {
186
- // Arrange
187
- resizeWindow("small");
188
-
189
- // Act
190
- const watcher = matchMedia("(max-width: 767px)");
191
- resizeWindow("large");
192
-
193
- // Assert
194
- expect(watcher.matches).toBe(false);
195
- });
196
- });
197
-
198
- describe("listeners", () => {
199
- test("should be called when window resizes", () => {
200
- // Arrange
201
- const listener = jest.fn();
202
- resizeWindow("small");
203
- const watcher = matchMedia("(max-width: 767px)");
204
- watcher.addListener(listener);
205
-
206
- // Act
207
- resizeWindow("large");
208
-
209
- // Assert
210
- expect(listener).toHaveBeenCalled();
211
- });
212
-
213
- test("should be called all listeners", () => {
214
- // Arrange
215
- const listener1 = jest.fn();
216
- const listener2 = jest.fn();
217
- resizeWindow("small");
218
- const watcher = matchMedia("(max-width: 767px)");
219
- watcher.addListener(listener1);
220
- watcher.addListener(listener2);
221
-
222
- // Act
223
- resizeWindow("large");
224
-
225
- // Assert
226
- expect(listener1).toHaveBeenCalled();
227
- expect(listener2).toHaveBeenCalled();
228
- });
229
-
230
- test("should not call a listener that's been removed", () => {
231
- // Arrange
232
- const listener = jest.fn();
233
- resizeWindow("small");
234
- const watcher = matchMedia("(max-width: 767px)");
235
- watcher.addListener(listener);
236
-
237
- // Act
238
- watcher.removeListener(listener);
239
- resizeWindow("large");
240
-
241
- // Assert
242
- expect(listener).not.toHaveBeenCalled();
243
- });
244
-
245
- test("should be passed {matches: true} when the window size matches", () => {
246
- // Arrange
247
- const listener = jest.fn();
248
- resizeWindow("large");
249
- const watcher = matchMedia("(max-width: 767px)");
250
- watcher.addListener(listener);
251
-
252
- // Act
253
- resizeWindow("small");
254
-
255
- // Assert
256
- expect(listener.mock.calls[0][0].matches).toBe(true);
257
- });
258
-
259
- test("should be passed {matches: false} when the window size doesn't match", () => {
260
- // Arrange
261
- const listener = jest.fn();
262
- resizeWindow("small");
263
- const watcher = matchMedia("(max-width: 767px)");
264
- watcher.addListener(listener);
265
-
266
- // Act
267
- resizeWindow("large");
268
-
269
- // Assert
270
- expect(listener.mock.calls[0][0].matches).toBe(false);
271
- });
272
- });
273
- });
274
- });
@@ -1,71 +0,0 @@
1
- import type {MediaSize} from "./types";
2
-
3
- /**
4
- * Helper function for setting the window size, for use in tests.
5
- * @param {MediaSize} size
6
- */
7
- export function resizeWindow(size: MediaSize) {
8
- if (size === "large") {
9
- window.innerWidth = 1200;
10
- } else if (size === "medium") {
11
- window.innerWidth = 800;
12
- } else if (size === "small") {
13
- window.innerWidth = 640;
14
- }
15
-
16
- window.dispatchEvent(new Event("resize"));
17
- }
18
-
19
- const queries = {
20
- small: "(max-width: 767px)",
21
- medium: "(min-width: 768px) and (max-width: 1023px)",
22
- large: "(min-width: 1024px)",
23
- } as const;
24
-
25
- export function checkQuery(
26
- queryString: typeof queries[keyof typeof queries],
27
- width: number,
28
- ): boolean {
29
- return (
30
- (width < 768 && queryString === queries.small) ||
31
- (width >= 768 && width < 1024 && queryString === queries.medium) ||
32
- (width >= 1024 && queryString === queries.large)
33
- );
34
- }
35
-
36
- type Listener = (arg1: {matches: boolean}) => void;
37
- type MatchMedia = {
38
- matches: boolean;
39
- addListener: (listener: Listener) => void;
40
- removeListener: (listener: Listener) => void;
41
- };
42
-
43
- /**
44
- *
45
- * @param {MediaQuery} query
46
- */
47
- export function matchMedia(
48
- query: typeof queries[keyof typeof queries],
49
- ): MatchMedia {
50
- const listeners = new Set<Listener>();
51
-
52
- window.addEventListener("resize", () => {
53
- for (const listener of listeners) {
54
- listener({
55
- matches: checkQuery(query, window.innerWidth),
56
- });
57
- }
58
- });
59
-
60
- return {
61
- get matches() {
62
- return checkQuery(query, window.innerWidth);
63
- },
64
- addListener: (listener: Listener) => {
65
- listeners.add(listener);
66
- },
67
- removeListener: (listener: Listener) => {
68
- listeners.delete(listener);
69
- },
70
- };
71
- }
package/src/util/types.ts DELETED
@@ -1,22 +0,0 @@
1
- export type MediaSize = "small" | "medium" | "large";
2
-
3
- export type MediaQuery = "all" | "mdOrSmaller" | "mdOrLarger" | MediaSize;
4
-
5
- type Spec = {
6
- /** The query to use to match the viewport against. */
7
- query: string;
8
- /** The total number of columns to use for the layout. */
9
- totalColumns: number;
10
- /** The width of the gutter between columns, in pixels. */
11
- gutterWidth: number;
12
- /** The width of the margin, wrapping the row, in pixels. */
13
- marginWidth: number;
14
- /** A possible maximum width to have on the layout. */
15
- maxWidth?: number;
16
- };
17
-
18
- export type MediaSpec = {
19
- small?: Spec;
20
- medium?: Spec;
21
- large?: Spec;
22
- };
@@ -1,234 +0,0 @@
1
- import {queryMatchesSize} from "./util";
2
-
3
- describe("queryMatchesSize", () => {
4
- describe("all", () => {
5
- // Arrange
6
- const query = "all";
7
-
8
- it("should match small", () => {
9
- // Arrange
10
- const size = "small";
11
-
12
- // Act
13
- const actualResult = queryMatchesSize(query, size);
14
-
15
- // Assert
16
- expect(actualResult).toBe(true);
17
- });
18
-
19
- it("should match medium", () => {
20
- // Arrange
21
- const size = "medium";
22
-
23
- // Act
24
- const actualResult = queryMatchesSize(query, size);
25
-
26
- // Assert
27
- expect(actualResult).toBe(true);
28
- });
29
-
30
- it("should match large", () => {
31
- // Arrange
32
- const size = "large";
33
-
34
- // Act
35
- const actualResult = queryMatchesSize(query, size);
36
-
37
- // Assert
38
- expect(actualResult).toBe(true);
39
- });
40
- });
41
-
42
- describe("mdOrLarger", () => {
43
- // Arrange
44
- const query = "mdOrLarger";
45
-
46
- it("should not match small", () => {
47
- // Arrange
48
- const size = "small";
49
-
50
- // Act
51
- const actualResult = queryMatchesSize(query, size);
52
-
53
- // Assert
54
- expect(actualResult).toBe(false);
55
- });
56
-
57
- it("should match medium", () => {
58
- // Arrange
59
- const size = "medium";
60
-
61
- // Act
62
- const actualResult = queryMatchesSize(query, size);
63
-
64
- // Assert
65
- expect(actualResult).toBe(true);
66
- });
67
-
68
- it("should match large", () => {
69
- // Arrange
70
- const size = "large";
71
-
72
- // Act
73
- const actualResult = queryMatchesSize(query, size);
74
-
75
- // Assert
76
- expect(actualResult).toBe(true);
77
- });
78
- });
79
-
80
- describe("mdOrSmaller", () => {
81
- // Arrange
82
- const query = "mdOrSmaller";
83
-
84
- it("should match small", () => {
85
- // Arrange
86
- const size = "small";
87
-
88
- // Act
89
- const actualResult = queryMatchesSize(query, size);
90
-
91
- // Assert
92
- expect(actualResult).toBe(true);
93
- });
94
-
95
- it("should match medium", () => {
96
- // Arrange
97
- const size = "medium";
98
-
99
- // Act
100
- const actualResult = queryMatchesSize(query, size);
101
-
102
- // Assert
103
- expect(actualResult).toBe(true);
104
- });
105
-
106
- it("should not match large", () => {
107
- // Arrange
108
- const size = "large";
109
-
110
- // Act
111
- const actualResult = queryMatchesSize(query, size);
112
-
113
- // Assert
114
- expect(actualResult).toBe(false);
115
- });
116
- });
117
-
118
- describe("small", () => {
119
- // Arrange
120
- const query = "small";
121
-
122
- it("should match small", () => {
123
- // Arrange
124
- const size = "small";
125
-
126
- // Act
127
- const actualResult = queryMatchesSize(query, size);
128
-
129
- // Assert
130
- expect(actualResult).toBe(true);
131
- });
132
-
133
- it("should not match medium", () => {
134
- // Arrange
135
- const size = "medium";
136
-
137
- // Act
138
- const actualResult = queryMatchesSize(query, size);
139
-
140
- // Assert
141
- expect(actualResult).toBe(false);
142
- });
143
-
144
- it("should not match large", () => {
145
- // Arrange
146
- const size = "large";
147
-
148
- // Act
149
- const actualResult = queryMatchesSize(query, size);
150
-
151
- // Assert
152
- expect(actualResult).toBe(false);
153
- });
154
- });
155
-
156
- describe("medium", () => {
157
- // Arrange
158
- const query = "medium";
159
-
160
- it("should not match small", () => {
161
- // Arrange
162
- const size = "small";
163
-
164
- // Act
165
- const actualResult = queryMatchesSize(query, size);
166
-
167
- // Assert
168
- expect(actualResult).toBe(false);
169
- });
170
-
171
- it("should match medium", () => {
172
- // Arrange
173
- const size = "medium";
174
-
175
- // Act
176
- const actualResult = queryMatchesSize(query, size);
177
-
178
- // Assert
179
- expect(actualResult).toBe(true);
180
- });
181
-
182
- it("should not match large", () => {
183
- // Arrange
184
- const size = "large";
185
-
186
- // Act
187
- const actualResult = queryMatchesSize(query, size);
188
-
189
- // Assert
190
- expect(actualResult).toBe(false);
191
- });
192
- });
193
-
194
- describe("large", () => {
195
- // Arrange
196
- const query = "small";
197
-
198
- it("should not match small", () => {
199
- // Arrange
200
- const size = "small";
201
-
202
- // Act
203
- const actualResult = queryMatchesSize(query, size);
204
-
205
- // Assert
206
- expect(actualResult).toBe(true);
207
- });
208
-
209
- it("should not match medium", () => {
210
- // Arrange
211
- const size = "medium";
212
-
213
- // Act
214
- const actualResult = queryMatchesSize(query, size);
215
-
216
- // Assert
217
- expect(actualResult).toBe(false);
218
- });
219
-
220
- it("should not match large", () => {
221
- // Arrange
222
- const size = "large";
223
-
224
- // Act
225
- const actualResult = queryMatchesSize(query, size);
226
-
227
- // Assert
228
- expect(actualResult).toBe(false);
229
- });
230
- });
231
- // it("should throw on invalid query", () => {
232
- // expect(() => queryMatchesSize("foobar", "small")).toThrow();
233
- // });
234
- });
package/src/util/util.ts DELETED
@@ -1,39 +0,0 @@
1
- import type {MediaQuery, MediaSize} from "./types";
2
-
3
- /**
4
- * Return where a media size matches a media query.
5
- *
6
- * examples:
7
- * - `queryMatchesSize("all", "small")` returns `true`
8
- * - `queryMatchesSize("mdOrLarger", "small")` returns `false`
9
- *
10
- * @param {MediaQuery} mediaQuery
11
- * @param {MediaSize} mediaSize
12
- */
13
- export const queryMatchesSize = (
14
- mediaQuery: MediaQuery,
15
- mediaSize: MediaSize,
16
- ): boolean => {
17
- switch (mediaQuery) {
18
- case "all":
19
- return true;
20
-
21
- case "small":
22
- return mediaSize === "small";
23
-
24
- case "mdOrSmaller":
25
- return mediaSize === "medium" || mediaSize === "small";
26
-
27
- case "medium":
28
- return mediaSize === "medium";
29
-
30
- case "mdOrLarger":
31
- return mediaSize === "medium" || mediaSize === "large";
32
-
33
- case "large":
34
- return mediaSize === "large";
35
-
36
- default:
37
- throw new Error(`Unsupported mediaSize: ${mediaSize}`);
38
- }
39
- };
@@ -1,12 +0,0 @@
1
- {
2
- "exclude": ["dist"],
3
- "extends": "../tsconfig-shared.json",
4
- "compilerOptions": {
5
- "outDir": "./dist",
6
- "rootDir": "src",
7
- },
8
- "references": [
9
- {"path": "../wonder-blocks-core/tsconfig-build.json"},
10
- {"path": "../wonder-blocks-tokens/tsconfig-build.json"},
11
- ]
12
- }