-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemba-dialog.test.ts
119 lines (95 loc) · 3.27 KB
/
temba-dialog.test.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
import { assert, expect, fixture } from '@open-wc/testing';
import * as sinon from 'sinon';
import { Dialog } from '../src/dialog/Dialog';
import { assertScreenshot, getClip } from './utils.test';
const getDialogClip = (dialog: Dialog) => {
return getClip(
dialog.shadowRoot.querySelector('.dialog-container') as HTMLElement
);
};
const getDialogHTML = (hideOnClick = false) => {
return `
<temba-dialog header="Hello Dialog" ${hideOnClick ? 'hideOnClick' : ''}>
<input name="comment" type="text" style="margin: 10px"/>
</temba-dialog>
`;
};
const open = async (dialog: Dialog) => {
const clock = sinon.useFakeTimers();
dialog.open = true;
await dialog.updateComplete;
// our dialog will animate onto the screen
clock.tick(400);
await dialog.updateComplete;
// gain focus for first text input
clock.tick(100);
await dialog.updateComplete;
clock.restore();
};
const close = async (dialog: Dialog) => {
const clock = sinon.useFakeTimers();
dialog.open = false;
await dialog.updateComplete;
// tick forward for close to complete
clock.tick(400);
clock.restore();
};
describe('temba-dialog', () => {
it('can be created', async () => {
const dialog: Dialog = await fixture(getDialogHTML());
assert.instanceOf(dialog, Dialog);
});
it('can be opened', async () => {
const dialog: Dialog = await fixture(getDialogHTML());
await open(dialog);
expect(dialog.ready).to.equal(true);
});
it('can be closed by attribute', async () => {
const dialog: Dialog = await fixture(getDialogHTML());
await open(dialog);
expect(dialog.ready).to.equal(true);
dialog.open = false;
});
it('can be canceled', async () => {
const dialog: Dialog = await fixture(getDialogHTML());
await open(dialog);
expect(dialog.ready).to.equal(true);
dialog.getCancelButton().click();
expect(dialog.open).to.equal(false);
});
it('restricts and restores background scrolling', async () => {
const dialog: Dialog = await fixture(getDialogHTML());
await open(dialog);
const body = document.querySelector('body');
expect(body.style.position).to.equal('fixed');
await close(dialog);
dialog.getCancelButton().click();
await dialog.updateComplete;
expect(body.style.position).to.equal('');
});
it('focuses the first text input', async () => {
const dialog: Dialog = await fixture(getDialogHTML());
await open(dialog);
const input = dialog.querySelector('input');
expect(document.activeElement).to.equal(input);
await assertScreenshot('dialog/focused', getDialogClip(dialog));
});
it('hides on click', async () => {
const dialog: Dialog = await fixture(getDialogHTML(true));
await open(dialog);
const mask: HTMLDivElement =
dialog.shadowRoot.querySelector('#dialog-mask');
mask.click();
await dialog.updateComplete;
expect(dialog.open).to.equal(false);
});
it('hides on escape', async () => {
const dialog: Dialog = await fixture(getDialogHTML(true));
await open(dialog);
expect(dialog.open).to.equal(true);
// simulate the escape key
const element = dialog.shadowRoot.querySelector('.dialog-container');
element.dispatchEvent(new KeyboardEvent('keyup', { key: 'Escape' }));
expect(dialog.open).to.equal(false);
});
});