-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtemba-checkbox.test.ts
168 lines (144 loc) · 5.39 KB
/
temba-checkbox.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
import { html, fixture, expect } from '@open-wc/testing';
import { Checkbox } from '../src/checkbox/Checkbox';
import { assertScreenshot, getClip } from './utils.test';
describe('temba-checkbox', () => {
it('renders default checkbox', async () => {
const el: Checkbox = await fixture(html`
<temba-checkbox label="My Checkbox"></temba-checkbox>
`);
expect(el.label).to.equal('My Checkbox');
await assertScreenshot('checkbox/default', getClip(el));
});
it('can select by clicking on the label', async () => {
const el: Checkbox = await fixture(html`
<temba-checkbox
label="My Checkbox"
animatechange="false"
></temba-checkbox>
`);
(el.shadowRoot.querySelector('.checkbox-label') as HTMLDivElement).click();
expect(el.checked).to.equal(true);
await assertScreenshot('checkbox/checked', getClip(el));
});
it('fires change event on click', async () => {
// eslint-disable-next-line no-async-promise-executor
return new Promise<void>(async (resolve) => {
const checkbox: Checkbox = await fixture(html`
<temba-checkbox label="My Checkbox"></temba-checkbox>
`);
checkbox.addEventListener('change', () => {
resolve();
});
click('temba-checkbox');
});
});
it('checks via click method', async () => {
const checkbox: Checkbox = await fixture(html`
<temba-checkbox label="My Checkbox"></temba-checkbox>
`);
checkbox.click();
expect(checkbox.checked).to.equal(true);
});
it('has background hover effect when label is set', async () => {
const el: Checkbox = await fixture(html`
<temba-checkbox name="My Checkbox" label="My Label"></temba-checkbox>
`);
expect(el.label).to.equal('My Label');
//the ".wrapper.label" style results in the background hover effect
const wrapperDivEl = el.shadowRoot.querySelector(
'div.wrapper.label'
) as HTMLDivElement;
expect(wrapperDivEl).to.not.equal(null);
await assertScreenshot(
'checkbox/checkbox-label-background-hover',
getClip(el)
);
});
//note: sometimes upstream logic sets an empty checkbox label to the name value,
//but this is the expected behavior if the label value is still empty,
//upon rendering the component
it('has no background hover effect when label is empty', async () => {
const el: Checkbox = await fixture(html`
<temba-checkbox name="My Checkbox"></temba-checkbox>
`);
expect(el.label).to.equal(null);
//the ".wrapper.label" style results in the background hover effect
const wrapperDivEl = el.shadowRoot.querySelector(
'div.wrapper.label'
) as HTMLDivElement;
expect(wrapperDivEl).to.equal(null);
await assertScreenshot(
'checkbox/checkbox-no-label-no-background-hover',
getClip(el)
);
});
it('has no background hover effect when label is whitespace', async () => {
const el: Checkbox = await fixture(html`
<temba-checkbox name="My Checkbox" label=" "></temba-checkbox>
`);
expect(el.label).to.equal('');
//the ".wrapper.label" style results in the background hover effect
const wrapperDivEl = el.shadowRoot.querySelector(
'div.wrapper.label'
) as HTMLDivElement;
expect(wrapperDivEl).to.equal(null);
await assertScreenshot(
'checkbox/checkbox-whitespace-label-no-background-hover',
getClip(el)
);
});
it('submits as boolean without value', async () => {
const form = (await fixture(html`
<form>
<temba-checkbox name="my-cb"></temba-checkbox>
</form>
`)) as HTMLFormElement;
// if we didn't click it, it shouldn't be in the form data
let data = new FormData(form);
expect(data.get('my-cb')).to.equal(null);
// click our checkbox
const checkbox = form.querySelector('temba-checkbox') as Checkbox;
await click('temba-checkbox');
expect(checkbox.checked).to.equal(true);
// clicking a non-value checkbox should set it to 1
data = new FormData(form);
expect(data.get('my-cb')).to.equal('1');
});
it('supports custom values', async () => {
const form = (await fixture(html`
<form>
<temba-checkbox name="my-cb" value="3"></temba-checkbox>
</form>
`)) as HTMLFormElement;
// if we didn't click it, it shouldn't be in the form data
let data = new FormData(form);
expect(data.get('my-cb')).to.equal(null);
// click our checkbox
const checkbox = form.querySelector('temba-checkbox') as Checkbox;
await click('temba-checkbox');
expect(checkbox.checked).to.equal(true);
// clicking a non-value checkbox should set it to 1
data = new FormData(form);
expect(data.get('my-cb')).to.equal('3');
});
it('supports programmtically updated values', async () => {
// start with empty value
const form = (await fixture(html`
<form>
<temba-checkbox name="my-cb"></temba-checkbox>
</form>
`)) as HTMLFormElement;
// update our value directly
const checkbox = form.querySelector('temba-checkbox') as Checkbox;
checkbox.value = '5';
// we set a custom value, but we still aren't checked
let data = new FormData(form);
expect(data.get('my-cb')).to.equal(null);
// click our checkbox
await click('temba-checkbox');
expect(checkbox.checked).to.equal(true);
// clicking a non-value checkbox should set it to 1
data = new FormData(form);
expect(data.get('my-cb')).to.equal('5');
});
});