-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex-test.js
213 lines (190 loc) · 7.66 KB
/
index-test.js
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
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { click, render, resetOnerror } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
module(
'Integration | Component | hds/disclosure-primitive/index',
function (hooks) {
setupRenderingTest(hooks);
hooks.afterEach(() => {
resetOnerror();
});
test('it should render the component with a CSS class that matches the component name', async function (assert) {
await render(
hbs`<Hds::DisclosurePrimitive id="test-disclosure-primitive" />`
);
assert
.dom('div#test-disclosure-primitive')
.hasClass('hds-disclosure-primitive');
});
// TOGGLE + CONTENT
test('it should render the "toggle" block but not the "content', async function (assert) {
await render(hbs`
<Hds::DisclosurePrimitive>
<:toggle>
<button type="button" id="test-disclosure-primitive-button" />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
assert.dom('.hds-disclosure-primitive__toggle').exists();
assert.dom('button#test-disclosure-primitive-button').exists();
assert.dom('.hds-disclosure-primitive__content').exists();
assert.dom('a#test-disclosure-primitive-link').doesNotExist();
});
test('it should render the "content" when the "toggle" is clicked', async function (assert) {
await render(hbs`
<Hds::DisclosurePrimitive>
<:toggle as |t|>
<button type="button" id="test-disclosure-primitive-button" {{on "click" t.onClickToggle}} />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
await click('button#test-disclosure-primitive-button');
assert.dom('a#test-disclosure-primitive-link').exists();
});
// isOpen
test('it should toggle the "content" when @isOpen is set', async function (assert) {
this.set('isOpen', true);
await render(hbs`
<Hds::DisclosurePrimitive @isOpen={{this.isOpen}} id="test-disclosure-primitive">
<:toggle as |t|>
<button type="button" id="test-toggle-button" {{on "click" t.onClickToggle}} />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
assert.dom('a#test-disclosure-primitive-link').exists();
this.set('isOpen', false);
assert.dom('a#test-disclosure-primitive-link').doesNotExist();
});
test('it should allow @isOpen to override an internal _isOpen=true', async function (assert) {
await render(hbs`
<Hds::DisclosurePrimitive @isOpen={{this.isOpen}} id="test-disclosure-primitive">
<:toggle as |t|>
<button type="button" id="test-toggle-button" {{on "click" t.onClickToggle}} />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
await click('button#test-toggle-button');
assert.dom('a#test-disclosure-primitive-link').exists();
this.set('isOpen', false);
assert.dom('a#test-disclosure-primitive-link').doesNotExist();
});
test('it should allow @isOpen to override an internal _isOpen=false', async function (assert) {
await render(hbs`
<Hds::DisclosurePrimitive @isOpen={{this.isOpen}} id="test-disclosure-primitive">
<:toggle as |t|>
<button type="button" id="test-toggle-button" {{on "click" t.onClickToggle}} />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
assert.dom('a#test-disclosure-primitive-link').doesNotExist();
this.set('isOpen', true);
assert.dom('a#test-disclosure-primitive-link').exists();
});
test('it should allow the internal _isOpen to override @isOpen=true', async function (assert) {
this.set('isOpen', true);
await render(hbs`
<Hds::DisclosurePrimitive @isOpen={{this.isOpen}} id="test-disclosure-primitive">
<:toggle as |t|>
<button type="button" id="test-toggle-button" {{on "click" t.onClickToggle}} />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
assert.dom('a#test-disclosure-primitive-link').exists();
await click('button#test-toggle-button');
assert.dom('a#test-disclosure-primitive-link').doesNotExist();
});
test('it should allow the internal _isOpen to override @isOpen=false', async function (assert) {
this.set('isOpen', false);
await render(hbs`
<Hds::DisclosurePrimitive @isOpen={{this.isOpen}} id="test-disclosure-primitive">
<:toggle as |t|>
<button type="button" id="test-toggle-button" {{on "click" t.onClickToggle}} />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
assert.dom('a#test-disclosure-primitive-link').doesNotExist();
await click('button#test-toggle-button');
assert.dom('a#test-disclosure-primitive-link').exists();
});
// contentId
test('it should set the contentId on the content block', async function (assert) {
await render(hbs`
<Hds::DisclosurePrimitive>
<:toggle>
<button type="button" id="test-disclosure-primitive-button" />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
assert.dom('.hds-disclosure-primitive__content').hasAttribute('id');
});
// CLOSE DISCLOSED CONTENT ON CLICK
test('it should hide the "content" when an interactive element triggers `close`', async function (assert) {
await render(hbs`
<Hds::DisclosurePrimitive id="test-disclosure-primitive">
<:toggle as |t|>
<button type="button" id="test-toggle-button" {{on "click" t.onClickToggle}} />
</:toggle>
<:content as |c|>
<button id="test-content-button" {{on "click" c.close}}>test</button>
</:content>
</Hds::DisclosurePrimitive>
`);
await click('button#test-toggle-button');
assert.dom('button#test-content-button').exists();
await click('button#test-content-button');
assert.dom('button#test-content-button').doesNotExist();
});
// CALLBACK
test('it should invoke the `onClickToggle` callback', async function (assert) {
let opened = false;
this.set('onClickToggle', () => (opened = !opened));
await render(hbs`
<Hds::DisclosurePrimitive @onClickToggle={{this.onClickToggle}} id="test-disclosure-primitive">
<:toggle as |t|>
<button type="button" id="test-toggle-button" {{on "click" t.onClickToggle}} />
</:toggle>
<:content>
<a id="test-disclosure-primitive-link" href="#">test</a>
</:content>
</Hds::DisclosurePrimitive>
`);
// toggle to open
await click('button#test-toggle-button');
assert.true(opened);
assert.dom('a#test-disclosure-primitive-link').exists();
// toggle to close
await click('button#test-toggle-button');
assert.false(opened);
assert.dom('a#test-disclosure-primitive-link').doesNotExist();
});
}
);