forked from nyaruka/temba-components
-
Notifications
You must be signed in to change notification settings - Fork 1
/
temba-modax.test.ts
163 lines (133 loc) · 4.26 KB
/
temba-modax.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
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
import { fixture, expect, assert } from '@open-wc/testing';
import { Button } from '../src/button/Button';
import { Modax } from '../src/dialog/Modax';
import { useFakeTimers } from 'sinon';
import { assertScreenshot, checkTimers, getClip, mockPOST } from './utils.test';
let clock: any;
const getModaxHTML = (endpoint: string): string => {
return `
<temba-modax header="Hello Modax" endpoint="${endpoint}">
<div>Open Me</div>
</temba-modax>
`;
};
const getButtons = (modax: Modax, type: string = null) => {
return modax.shadowRoot
.querySelector('temba-dialog')
.shadowRoot.querySelectorAll(
type ? `temba-button[${type}='']` : 'temba-button'
);
};
const open = async (modax: Modax) => {
modax.open = true;
await modax.updateComplete;
await modax.httpComplete;
await clock.tick(400);
};
const clickPrimary = async (modax: Modax) => {
const buttons = getButtons(modax);
if (buttons.length > 0) {
let primary = buttons[0] as Button;
if (buttons.length > 1) {
// look for our primary flag
buttons.forEach((button: Button) => {
if (button.primary) {
primary = button;
}
});
}
expect(primary).not.equals(undefined, 'Missing primary button');
primary.click();
await clock.tick(500);
await clock.tick(100);
await waitFor(0);
await clock.tick(1000);
}
};
const getDialogClip = (modax: Modax) => {
const dialog = modax.shadowRoot.querySelector('temba-dialog');
return getClip(
dialog.shadowRoot.querySelector('.dialog-container') as HTMLElement
);
};
describe('temba-modax', () => {
beforeEach(function () {
clock = useFakeTimers();
});
afterEach(function () {
clock.restore();
});
it('can be created', async () => {
const modax: Modax = await fixture(
getModaxHTML('/test-assets/modax/hello.html')
);
assert.instanceOf(modax, Modax);
});
it('opens', async () => {
const modax: Modax = await fixture(
getModaxHTML('/test-assets/modax/hello.html')
);
await click('temba-modax');
expect(modax.open).equals(true);
await modax.httpComplete;
await clock.tick(400);
checkTimers(clock);
// Now our body should have our endpoint text
expect(modax.getBody().innerHTML).to.contain('Hello World');
await assertScreenshot('modax/simple', getDialogClip(modax));
});
it('fetches forms', async () => {
const modax: Modax = await fixture(
getModaxHTML('/test-assets/modax/form.html')
);
expect(modax.open).to.equal(false);
await open(modax);
expect(modax.open).to.equal(true);
await assertScreenshot('modax/form', getDialogClip(modax));
});
it('reverts primary name on reuse', async () => {
const modax: Modax = await fixture(
getModaxHTML('/test-assets/modax/hello.html')
);
// await click('temba-modax');
await open(modax);
expect(modax.open).equals(true);
// should only have one button, okay
let buttons = getButtons(modax);
expect(buttons.length).equals(1);
// close our dialog
await clickPrimary(modax);
expect(modax.open).equals(false);
// now fetch form from the same modax
modax.endpoint = '/test-assets/modax/form.html';
await modax.updateComplete;
await modax.httpComplete;
await clock.tick(400);
await open(modax);
expect(modax.open).equals(true);
await modax.httpComplete;
await clock.tick(400);
await waitFor(100);
// now we should have two buttons, 'Save Everything' and 'Cancel'
buttons = getButtons(modax);
expect(buttons.length).equals(2);
// secondary should be Cancel, not Ok
const secondary = getButtons(modax, 'secondary')[0] as Button;
expect(secondary.name).equals('Cancel');
});
it('closes after redirect', async () => {
const modax: Modax = await fixture(
getModaxHTML('/test-assets/modax/form.html')
);
await open(modax);
const primary = getButtons(modax, 'primary')[0] as Button;
expect(primary.name).equals('Save Everything');
// click the submit button
mockPOST(/\/test-assets\/modax\/form\.html/, '', {
'Temba-Success': '/newpage',
});
await clickPrimary(modax);
// our modal should go away as we redirect
expect(modax.open).equals(false, 'Modal still visible');
});
});