-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemba-modax.test.ts
176 lines (142 loc) · 4.52 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
164
165
166
167
168
169
170
171
172
173
174
175
176
import { fixture, expect, assert } from '@open-wc/testing';
import { useFakeTimers } from 'sinon';
import { Button } from '../src/button/Button';
import { Modax } from '../src/dialog/Modax';
import { CustomEventType } from '../src/interfaces';
import { assertScreenshot, 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) => {
return new Promise((resolve: any) => {
modax.addEventListener(
CustomEventType.Loaded,
async (event: CustomEvent) => {
await clock.runAll();
resolve(event.detail);
}
);
modax.addEventListener(
CustomEventType.Redirected,
async (event: CustomEvent) => {
await clock.runAll();
resolve(event.detail);
}
);
modax.open = true;
});
};
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();
}
};
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 open(modax);
expect(modax.open).equals(true);
// 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);
expect(modax.buttons[1].name).to.equal('Save Everything');
expect(modax.buttons[0].name).to.equal('Cancel');
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 open(modax);
expect(modax.open).equals(true);
// 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/, 'arst', {
'X-Temba-Success': 'hide'
});
const hideTest = new Promise<void>((resolve) => {
modax.addEventListener(CustomEventType.Submitted, () => {
expect(modax.open).equals(false, 'Modal still visible');
resolve();
});
});
await clickPrimary(modax);
await clock.runAllAsync();
await hideTest;
});
});