-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathblock.e2e.ts
499 lines (431 loc) · 16.5 KB
/
block.e2e.ts
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// @ts-strict-ignore
import { newE2EPage } from "@arcgis/lumina-compiler/puppeteerTesting";
import { describe, expect, it } from "vitest";
import {
accessible,
defaults,
delegatesToFloatingUiOwningComponent,
disabled,
focusable,
handlesActionMenuPlacements,
hidden,
reflects,
renders,
slots,
t9n,
themed,
} from "../../tests/commonTests";
import { html } from "../../../support/formatting";
import { openClose } from "../../tests/commonTests";
import { skipAnimations } from "../../tests/utils";
import { defaultEndMenuPlacement } from "../../utils/floating-ui";
import { CSS, IDS, SLOTS } from "./resources";
describe("calcite-block", () => {
describe("renders", () => {
renders("calcite-block", { display: "flex" });
});
describe("honors hidden attribute", () => {
hidden("calcite-block");
});
describe("defaults", () => {
defaults("calcite-block", [
{
propertyName: "collapsible",
defaultValue: false,
},
{
propertyName: "dragDisabled",
defaultValue: false,
},
{
propertyName: "headingLevel",
defaultValue: undefined,
},
{
propertyName: "open",
defaultValue: false,
},
{
propertyName: "overlayPositioning",
defaultValue: "absolute",
},
{
propertyName: "menuPlacement",
defaultValue: defaultEndMenuPlacement,
},
{
propertyName: "menuFlipPlacements",
defaultValue: undefined,
},
{
propertyName: "sortHandleOpen",
defaultValue: false,
},
]);
});
describe("reflects", () => {
reflects("calcite-block", [
{
propertyName: "collapsible",
value: true,
},
{
propertyName: "headingLevel",
value: 2,
},
{
propertyName: "open",
value: true,
},
{
propertyName: "overlayPositioning",
value: "fixed",
},
{
propertyName: "menuPlacement",
value: "bottom",
},
{
propertyName: "dragDisabled",
value: true,
},
{
propertyName: "sortHandleOpen",
value: true,
},
]);
});
describe("openClose", () => {
openClose("calcite-block");
});
describe("slots", () => {
slots("calcite-block", SLOTS);
});
describe("accessible", () => {
accessible(html`
<calcite-block heading="heading" description="description" open collapsible>
<div slot=${SLOTS.icon}>✅</div>
<div>content</div>
<label slot=${SLOTS.control}>test <input placeholder="control" /></label>
</calcite-block>
`);
});
describe("setFocus", () => {
describe("focuses block heading toggle", () => {
focusable(
html`<calcite-block heading="Heading" description="summary" collapsible open>
<calcite-block-section text="input block-section" open>
<calcite-input
icon="form-field"
placeholder="This is an input field... enter something here"
></calcite-input>
</calcite-block-section>
</calcite-block>`,
{
shadowFocusTargetSelector: `.${CSS.toggle}`,
},
);
});
const blockSectionClass = "my-block-section";
describe("focuses block section", () => {
focusable(
html`<calcite-block heading="Heading" description="summary" open>
<calcite-block-section class="${blockSectionClass}" text="input block-section" open>
<calcite-input
icon="form-field"
placeholder="This is an input field... enter something here"
></calcite-input>
</calcite-block-section>
</calcite-block>`,
{
focusTargetSelector: `.${blockSectionClass}`,
},
);
});
});
describe("disabled", () => {
disabled(html`<calcite-block heading="heading" description="description" collapsible></calcite-block>`);
});
describe("delegates to floating-ui-owner component", () => {
delegatesToFloatingUiOwningComponent(
html`<calcite-block>
<calcite-action label="Add" icon="plus" slot="header-menu-actions"></calcite-action>
</calcite-block>`,
"calcite-action-menu",
);
});
describe("handles action-menu placement and flipPlacements", () => {
handlesActionMenuPlacements(html`
<calcite-block heading="heading" description="description">
<calcite-action text="test" icon="banana" slot="${SLOTS.headerMenuActions}"></calcite-action>
<div class="content">content</div>
</calcite-block>
`);
});
it("has a loading state", async () => {
const page = await newE2EPage({
html: `
<calcite-block heading="heading" description="description" open collapsible>
<div class="content">content</div>
</calcite-block>
`,
});
await page.waitForChanges();
expect(await page.find("calcite-block >>> calcite-scrim")).toBeNull();
const content = await page.find(".content");
const clickSpy = await content.spyOnEvent("click");
await content.click();
expect(clickSpy).toHaveReceivedEventTimes(1);
const block = await page.find("calcite-block");
block.setProperty("loading", true);
await page.waitForChanges();
await content.click();
expect(clickSpy).toHaveReceivedEventTimes(1);
expect(await page.find("calcite-block >>> calcite-scrim")).toBeTruthy();
});
it("can display/hide content", async () => {
const page = await newE2EPage();
await page.setContent(
html`<calcite-block heading="heading" description="description"><div>Hello world!</div></calcite-block>`,
);
await skipAnimations(page);
const element = await page.find("calcite-block");
const content = await page.find(`calcite-block >>> #${IDS.content}`);
expect(await element.getProperty("open")).toBe(false);
expect(await content.isVisible()).toBe(false);
element.setProperty("open", true);
await page.waitForChanges();
expect(await content.isVisible()).toBe(true);
element.setProperty("open", false);
await page.waitForChanges();
expect(await content.isVisible()).toBe(false);
});
it("allows toggling its content", async () => {
const heading = "heading";
const page = await newE2EPage();
await page.setContent(html`<calcite-block collapsible heading=${heading}></calcite-block>`);
await skipAnimations(page);
const messages = await import("./assets/t9n/messages.json");
const element = await page.find("calcite-block");
const toggleSpy = await element.spyOnEvent("calciteBlockToggle");
const openSpy = await element.spyOnEvent("calciteBlockOpen");
const closeSpy = await element.spyOnEvent("calciteBlockClose");
const toggle = await page.find(`calcite-block >>> .${CSS.toggle}`);
expect(toggle.getAttribute("aria-expanded")).toBe("false");
expect(toggle.getAttribute("title")).toBe(messages.expand);
await toggle.click();
expect(toggleSpy).toHaveReceivedEventTimes(1);
expect(openSpy).toHaveReceivedEventTimes(1);
expect(await element.getProperty("open")).toBe(true);
expect(toggle.getAttribute("aria-expanded")).toBe("true");
expect(toggle.getAttribute("title")).toBe(messages.collapse);
await toggle.click();
expect(toggleSpy).toHaveReceivedEventTimes(2);
expect(closeSpy).toHaveReceivedEventTimes(1);
expect(await element.getProperty("open")).toBe(false);
expect(toggle.getAttribute("aria-expanded")).toBe("false");
expect(toggle.getAttribute("title")).toBe(messages.expand);
});
describe("header", () => {
it("renders a heading", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-block heading="test-heading"></calcite-block>`);
const heading = await page.find(`calcite-block >>> .${CSS.heading}`);
expect(heading).toBeTruthy();
expect(heading.innerText).toBe("test-heading");
const description = await page.find(`calcite-block >>> .${CSS.description}`);
expect(description).toBeNull();
});
it("renders a heading with optional description", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-block heading="test-heading" description="test-description"></calcite-block>`);
const heading = await page.find(`calcite-block >>> .${CSS.heading}`);
expect(heading).toBeTruthy();
const description = await page.find(`calcite-block >>> .${CSS.description}`);
expect(description.innerText).toBe("test-description");
});
it("allows users to add a control in a collapsible block", async () => {
const page = await newE2EPage();
await page.setContent(html`
<calcite-block heading="test-heading" collapsible>
<div class="nested-control" tabindex="0" slot=${SLOTS.control}>fake space/enter-bubbling control</div>
</calcite-block>
`);
await skipAnimations(page);
const control = await page.find(".nested-control");
expect(await control.isVisible()).toBe(true);
const controlSlot = await page.find(`calcite-block >>> slot[name=${SLOTS.control}]`);
expect(await controlSlot.isVisible()).toBe(true);
const block = await page.find("calcite-block");
const blockToggleSpy = await block.spyOnEvent("calciteBlockToggle");
const blockOpenSpy = await block.spyOnEvent("calciteBlockOpen");
const blockCloseSpy = await block.spyOnEvent("calciteBlockClose");
await control.press("Space");
await control.press("Enter");
await control.click();
expect(blockOpenSpy).toHaveReceivedEventTimes(0);
expect(blockToggleSpy).toHaveReceivedEventTimes(0);
expect(blockCloseSpy).toHaveReceivedEventTimes(0);
await block.click();
await block.click();
expect(blockToggleSpy).toHaveReceivedEventTimes(2);
expect(blockOpenSpy).toHaveReceivedEventTimes(1);
expect(blockCloseSpy).toHaveReceivedEventTimes(1);
});
it("displays a status icon instead of a header icon when `status` is an accepted value", async () => {
const page = await newE2EPage();
await page.setContent(
`<calcite-block status="invalid">
<div class="header-icon" slot=${SLOTS.icon} /></calcite-block>
</calcite-block>`,
);
const headerIcon = await page.find("calcite-block >>> .header-icon");
expect(headerIcon).toBeNull();
const statusIcon = await page.find(`calcite-block >>> .${CSS.statusIcon}`);
expect(statusIcon).not.toBeNull();
});
it("displays a loading icon when `loading` is set to true and `open` is set to false", async () => {
const headerIcon = "header-icon";
const page = await newE2EPage();
await page.setContent(
`<calcite-block status="invalid" loading>
<div class="${headerIcon}" slot=${SLOTS.icon} /></calcite-block>
</calcite-block>`,
);
const headerIconEle = await page.find(`calcite-block >>> .${headerIcon}`);
expect(headerIconEle).toBeNull();
const statusIcon = await page.find(`calcite-block >>> .${CSS.statusIcon}`);
const loader = await page.find(`calcite-block >>> calcite-loader`);
expect(statusIcon).toBeNull();
expect(loader).not.toBeNull();
});
it("allows users to slot in actions in a header menu", async () => {
const page = await newE2EPage({
html: html` <calcite-block heading="With header actions" description="has header actions">
<calcite-action label="Add" icon="plus" slot="header-menu-actions"></calcite-action>
</calcite-block>`,
});
const menuSlot = await page.find(`calcite-block >>> calcite-action-menu slot[name=${SLOTS.headerMenuActions}]`);
expect(menuSlot).toBeDefined();
const actionAssignedSlot = await page.$eval("calcite-action", (action) => action.assignedSlot.name);
expect(actionAssignedSlot).toBe(SLOTS.headerMenuActions);
});
it("applies correct header spacing when heading or description properties are present", async () => {
const page = await newE2EPage();
await page.setContent(`<calcite-block></calcite-block>`);
const block = await page.find("calcite-block");
const header = await page.find(`calcite-block >>> .${CSS.header}`);
block.setAttribute("heading", "test-heading");
await page.waitForChanges();
expect(header).toHaveClass(CSS.headerHasText);
block.removeAttribute("heading");
await page.waitForChanges();
expect(header).not.toHaveClass(CSS.headerHasText);
block.setAttribute("description", "test-description");
await page.waitForChanges();
expect(header).toHaveClass(CSS.headerHasText);
});
});
it("should allow the CSS custom property to be overridden when applied to :root", async () => {
const overrideStyle = "0px";
const page = await newE2EPage();
await page.setContent(
`<style>
:root {
--calcite-block-padding: ${overrideStyle}
}
</style>
<calcite-block heading="test-heading" collapsible style="--calcite-block-padding: ${overrideStyle}" open>
<calcite-action text="test" icon="banana" slot="${SLOTS.headerMenuActions}"></calcite-action>
</calcite-block>`,
);
const content = await page.find(`calcite-block >>> .${CSS.content}`);
const contentStyles = await content.getComputedStyle();
const contentPadding = await contentStyles.getPropertyValue("padding");
expect(contentPadding).toEqual(overrideStyle);
});
it("should allow the CSS custom property to be overridden when applied to element", async () => {
const overrideStyle = "0px";
const page = await newE2EPage();
await page.setContent(
`<calcite-block heading="test-heading" collapsible style="--calcite-block-padding: ${overrideStyle}" open>
<calcite-action text="test" icon="banana" slot="${SLOTS.headerMenuActions}"></calcite-action>
</calcite-block>`,
);
const content = await page.find(`calcite-block >>> .${CSS.content}`);
const contentStyles = await content.getComputedStyle();
const contentPadding = await contentStyles.getPropertyValue("padding");
expect(contentPadding).toEqual(overrideStyle);
});
it("should set aria-label", async () => {
const label = "Spatial";
const page = await newE2EPage();
await page.setContent(
html`<calcite-block label=${label} open>
<calcite-notice open>
<div slot="message">Use layer effects sparingly, for emphasis</div>
</calcite-notice>
</calcite-block>`,
);
const article = await page.find(`calcite-block >>> article`);
expect(article.getAttribute("aria-label")).toEqual(label);
});
describe("translation support", () => {
t9n("calcite-block");
});
describe("theme", () => {
describe("default", () => {
themed(
html`<calcite-block
heading="heading"
description="description"
open
collapsible
icon-end="pen"
icon-start="pen"
>
<calcite-icon icon="compass" slot="content-start"></calcite-icon>
<div>content</div>
</calcite-block>`,
{
"--calcite-block-border-color": {
targetProp: "borderColor",
},
"--calcite-block-header-background-color": {
shadowSelector: `.${CSS.toggle}`,
targetProp: "backgroundColor",
},
"--calcite-block-header-background-color-hover": {
shadowSelector: `.${CSS.toggle}`,
targetProp: "backgroundColor",
state: "hover",
},
"--calcite-block-text-color": [
{ shadowSelector: `.${CSS.description}`, targetProp: "color" },
{ shadowSelector: `.${CSS.contentStart}`, targetProp: "color" },
{ shadowSelector: `.${CSS.iconEnd}`, targetProp: "color" },
{ shadowSelector: `.${CSS.iconStart}`, targetProp: "color" },
{ shadowSelector: `.${CSS.toggleIcon}`, targetProp: "color" },
],
"--calcite-block-heading-text-color-press": [
{
shadowSelector: `.${CSS.toggleIcon}`,
targetProp: "color",
state: "hover",
},
{
shadowSelector: `.${CSS.heading}`,
targetProp: "color",
state: { press: { attribute: "class", value: CSS.heading } },
},
],
},
);
});
describe("closed", () => {
themed(html`<calcite-block heading="heading"></calcite-block>`, {
"--calcite-block-heading-text-color": { shadowSelector: `.${CSS.heading}`, targetProp: "color" },
});
});
});
});