1 |
|
2 |
|
3 | import hbs from 'htmlbars-inline-precompile';
|
4 | import { module } from 'qunit';
|
5 | import {
|
6 | start,
|
7 | test,
|
8 | skip,
|
9 | only,
|
10 | todo,
|
11 | setResolver,
|
12 | setupRenderingTest,
|
13 | setupTest,
|
14 | SetupTestOptions,
|
15 | setupApplicationTest,
|
16 | } from 'ember-qunit';
|
17 | import { render, RenderingTestContext, TestContext } from '@ember/test-helpers';
|
18 | import EmberResolver from 'ember-resolver';
|
19 | import EmberObject from '@ember/object';
|
20 |
|
21 |
|
22 | setResolver(EmberResolver.create());
|
23 |
|
24 |
|
25 | module('rendering', function (hooks) {
|
26 | setupRenderingTest(hooks);
|
27 |
|
28 | test('it renders', function (this: RenderingTestContext, assert) {
|
29 | assert.expect(2);
|
30 |
|
31 |
|
32 | this.set('value', 'cat');
|
33 |
|
34 |
|
35 | this.render(hbs`
|
36 | {{ x-foo value=value action="result" }}
|
37 | `);
|
38 |
|
39 |
|
40 |
|
41 | this.render();
|
42 |
|
43 | this.render('{{ x-foo value=value action="result" }}');
|
44 |
|
45 | this.render(['{{ x-foo value=value action="result" }}']);
|
46 |
|
47 | const el = this.element.querySelector('div');
|
48 | assert.equal(el?.innerText, 'cat', 'The component shows the correct value');
|
49 |
|
50 | this.element.querySelector('button')?.click();
|
51 | });
|
52 |
|
53 | test('it renders', async function (this: RenderingTestContext, assert) {
|
54 | assert.expect(1);
|
55 |
|
56 |
|
57 | await render(hbs`<Foo />`);
|
58 |
|
59 | await render(hbs`<Foo @item={{42}} />`);
|
60 |
|
61 | const { inputFormat } = this.setProperties({
|
62 | inputFormat: 'M/D/YY',
|
63 | outputFormat: 'MMMM D, YYYY',
|
64 | date: '5/3/10',
|
65 | });
|
66 |
|
67 | const { inputFormat: if2, outputFormat } = this.getProperties(
|
68 | 'inputFormat',
|
69 | 'outputFormat'
|
70 | );
|
71 |
|
72 | const inputFormat2 = this.get('inputFormat');
|
73 |
|
74 |
|
75 | this.render(hbs`<div>bar</div>`);
|
76 | assert.equal(this.element.querySelector('div')?.innerText, 'bar');
|
77 | });
|
78 | });
|
79 |
|
80 | module('misc and async', function (hooks) {
|
81 | hooks.beforeEach(async function (assert) {
|
82 | assert.ok(true, 'hooks can be async');
|
83 | });
|
84 |
|
85 | test('It can calculate the result', function (assert) {
|
86 | assert.expect(1);
|
87 |
|
88 | interface Foo extends EmberObject {
|
89 | value: string;
|
90 | result: string;
|
91 | }
|
92 |
|
93 | const subject = this.owner.lookup('foo:bar') as Foo;
|
94 |
|
95 | subject.set('value', 'foo');
|
96 | assert.equal(subject.get('result'), 'bar');
|
97 | });
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | test('it can be async', async function (this: RenderingTestContext, assert) {
|
103 | assert.expect(1);
|
104 |
|
105 | await this.render(hbs`<p>Hello</p>`);
|
106 |
|
107 | assert.ok(true, 'rendered');
|
108 | });
|
109 |
|
110 | skip('disabled test');
|
111 |
|
112 | skip('disabled test', function (assert) {});
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | skip('it can skip async', async function (this: RenderingTestContext, assert) {
|
118 | assert.expect(1);
|
119 |
|
120 | await this.render(hbs`<p>Hello</p>`);
|
121 |
|
122 | assert.ok(true, 'rendered');
|
123 | });
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | only(
|
129 | 'it can only run async',
|
130 | async function (this: RenderingTestContext, assert) {
|
131 | assert.expect(1);
|
132 |
|
133 | await this.render(hbs`<p>Hello</p>`);
|
134 |
|
135 | assert.ok(true, 'rendered');
|
136 | }
|
137 | );
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | todo(
|
143 | 'it can have an async todo',
|
144 | async function (this: RenderingTestContext, assert) {
|
145 | assert.expect(1);
|
146 |
|
147 | await this.render(hbs`<p>Hello</p>`);
|
148 |
|
149 | assert.ok(true, 'rendered');
|
150 | }
|
151 | );
|
152 | });
|
153 |
|
154 |
|
155 | module('returning a promise', function () {
|
156 | test('it can return Promise<void>', function (this: TestContext, assert) {
|
157 | return Promise.resolve();
|
158 | });
|
159 |
|
160 | test('it can return a non-empty Promise', function (this: TestContext, assert) {
|
161 | return Promise.resolve('foo');
|
162 | });
|
163 | });
|
164 |
|
165 |
|
166 | QUnit.module('some description', function (hooks) {
|
167 | hooks.before(() => {});
|
168 | hooks.beforeEach(() => {});
|
169 | hooks.afterEach(() => {});
|
170 | hooks.after(() => {});
|
171 |
|
172 | QUnit.test('it blends', function (assert) {
|
173 | assert.ok(true, 'of course!');
|
174 | });
|
175 | });
|
176 |
|
177 |
|
178 | module('x-foo', function (hooks) {
|
179 | setupRenderingTest(hooks);
|
180 | });
|
181 |
|
182 |
|
183 | module('foo service', function (hooks) {
|
184 | setupTest(hooks);
|
185 | });
|
186 |
|
187 |
|
188 | module('engine foo component', function (hooks) {
|
189 | setupTest(hooks, {
|
190 | resolver: EmberResolver.create(),
|
191 | });
|
192 | });
|
193 |
|
194 | module('all the hooks', function (hooks) {
|
195 | setupTest(hooks);
|
196 |
|
197 | hooks.after(function () {
|
198 | this.owner.lookup('service:store');
|
199 | });
|
200 |
|
201 | hooks.afterEach(function () {
|
202 | this.owner.lookup('service:store');
|
203 | });
|
204 |
|
205 | hooks.before(function () {
|
206 | this.owner.lookup('service:store');
|
207 | });
|
208 |
|
209 | hooks.beforeEach(function () {
|
210 | this.owner.lookup('service:store');
|
211 | });
|
212 | });
|
213 |
|
214 | module.only('exclusive module with hooks', function (hooks) {
|
215 | setupTest(hooks);
|
216 |
|
217 | hooks.after(function () {
|
218 | this.owner.lookup('service:store');
|
219 | });
|
220 |
|
221 | hooks.afterEach(function () {
|
222 | this.owner.lookup('service:store');
|
223 | });
|
224 |
|
225 | hooks.before(function () {
|
226 | this.owner.lookup('service:store');
|
227 | });
|
228 |
|
229 | hooks.beforeEach(function () {
|
230 | this.owner.lookup('service:store');
|
231 | });
|
232 | });
|
233 |
|
234 | module('extending TestContext works', function () {
|
235 | interface Context extends TestContext {
|
236 | someProp: string;
|
237 | anotherProp: boolean;
|
238 | }
|
239 |
|
240 | module('it works with non-async', function (nonAsyncHooks) {
|
241 | nonAsyncHooks.before(function (this: Context) {
|
242 | this.someProp = 'hello';
|
243 | });
|
244 |
|
245 | nonAsyncHooks.beforeEach(function (this: Context) {
|
246 | this.anotherProp = true;
|
247 | });
|
248 |
|
249 | nonAsyncHooks.after(function (this: Context) {
|
250 | this.someProp = 'goodbye';
|
251 | });
|
252 |
|
253 | nonAsyncHooks.afterEach(function (this: Context) {
|
254 | this.anotherProp = false;
|
255 | });
|
256 |
|
257 | test('it works with tests', function (this: Context, assert) {
|
258 | this.someProp = this.someProp + ' cool person';
|
259 | assert.true(this.anotherProp);
|
260 | });
|
261 |
|
262 | skip('it works with skip', function (this: Context, assert) {
|
263 | this.someProp = 'wahoo';
|
264 | assert.ok(typeof this.someProp === 'string');
|
265 | });
|
266 |
|
267 | only('it works with only', function (this: Context, assert) {
|
268 | this.someProp = 'crazy pants';
|
269 | assert.ok(typeof this.someProp === 'string');
|
270 | });
|
271 |
|
272 | todo('it works with todo', function (this: Context, assert) {
|
273 | this.someProp = 'tada';
|
274 | assert.ok(typeof this.someProp === 'string');
|
275 | });
|
276 | });
|
277 |
|
278 | module('it works with async, too', function (asyncHooks) {
|
279 | asyncHooks.before(async function (this: Context) {
|
280 | this.someProp = 'hello';
|
281 | await Promise.resolve(this.someProp);
|
282 | });
|
283 |
|
284 | asyncHooks.beforeEach(async function (this: Context) {
|
285 | this.anotherProp = true;
|
286 | await Promise.resolve(this.anotherProp);
|
287 | });
|
288 |
|
289 | asyncHooks.after(async function (this: Context) {
|
290 | this.someProp = 'goodbye';
|
291 | await Promise.resolve(this.someProp);
|
292 | });
|
293 |
|
294 | asyncHooks.afterEach(async function (this: Context) {
|
295 | this.anotherProp = false;
|
296 | await Promise.resolve(this.anotherProp);
|
297 | });
|
298 |
|
299 | test('it works with tests', async function (this: Context, assert) {
|
300 | this.someProp = this.someProp + ' cool person';
|
301 | assert.true(this.anotherProp);
|
302 | await Promise.resolve('cool');
|
303 | });
|
304 |
|
305 | skip('it works with skip', async function (this: Context, assert) {
|
306 | this.someProp = 'wahoo';
|
307 | const result = await Promise.resolve(this.someProp);
|
308 | assert.ok(typeof result === 'string');
|
309 | });
|
310 |
|
311 | only('it works with only', async function (this: Context, assert) {
|
312 | this.someProp = 'crazy pants';
|
313 | const result = await Promise.resolve(this.someProp);
|
314 | assert.ok(typeof result === 'string');
|
315 | });
|
316 |
|
317 | todo('it works with todo', async function (this: Context, assert) {
|
318 | this.someProp = 'tada';
|
319 | const result = await Promise.resolve(this.someProp);
|
320 | assert.ok(typeof result === 'string');
|
321 | });
|
322 | });
|
323 | });
|
324 |
|
325 | start();
|
326 |
|
327 | module('with setup options', function (hooks) {
|
328 |
|
329 | type SetupTestOptions = Parameters<typeof setupTest>[1];
|
330 |
|
331 | type SetupRenderingTestOptions = Parameters<typeof setupRenderingTest>[1];
|
332 |
|
333 | type SetupApplicationTestOptions = Parameters<typeof setupApplicationTest>[1];
|
334 |
|
335 | const resolver = EmberResolver.create();
|
336 |
|
337 | setupTest(hooks, {});
|
338 | setupRenderingTest(hooks, {});
|
339 | setupApplicationTest(hooks, {});
|
340 |
|
341 | setupTest(hooks, { resolver });
|
342 | setupRenderingTest(hooks, { resolver });
|
343 | setupApplicationTest(hooks, { resolver });
|
344 | });
|