UNPKG

9.84 kBPlain TextView Raw
1// These tests were ported directly from DefinitelyTyped and are unlikely to be
2// 100% desireable for the future.
3import hbs from 'htmlbars-inline-precompile';
4import { module } from 'qunit';
5import {
6 start,
7 test,
8 skip,
9 only,
10 todo,
11 setResolver,
12 setupRenderingTest,
13 setupTest,
14 SetupTestOptions,
15 setupApplicationTest,
16} from 'ember-qunit';
17import { render, RenderingTestContext, TestContext } from '@ember/test-helpers';
18import EmberResolver from 'ember-resolver';
19import EmberObject from '@ember/object';
20
21// if you don't have a custom resolver, do it like this:
22setResolver(EmberResolver.create());
23
24// (modified) tests ported from ember-test-helpers
25module('rendering', function (hooks) {
26 setupRenderingTest(hooks);
27
28 test('it renders', function (this: RenderingTestContext, assert) {
29 assert.expect(2);
30
31 // setup the outer context
32 this.set('value', 'cat');
33
34 // render the component
35 this.render(hbs`
36 {{ x-foo value=value action="result" }}
37 `);
38
39 // has to be a template
40 // @ts-expect-error
41 this.render();
42 // @ts-expect-error
43 this.render('{{ x-foo value=value action="result" }}');
44 // @ts-expect-error
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 // creates the component instance
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 // render the component on the page
75 this.render(hbs`<div>bar</div>`);
76 assert.equal(this.element.querySelector('div')?.innerText, 'bar');
77 });
78});
79
80module('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 // This test is intended to ensure the appropriate behavior for @typescript-eslint/no-misused-promises.
100 // However, we don't actually use typescript-eslint in this project and tslint has no equivalent,
101 // so we can't properly test it.
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 // This test is intended to ensure the appropriate behavior for @typescript-eslint/no-misused-promises.
115 // However, we don't actually use typescript-eslint in this project and tslint has no equivalent,
116 // so we can't properly test it.
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 // This test is intended to ensure the appropriate behavior for @typescript-eslint/no-misused-promises.
126 // However, we don't actually use typescript-eslint in this project and tslint has no equivalent,
127 // so we can't properly test it.
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 // This test is intended to ensure the appropriate behavior for @typescript-eslint/no-misused-promises.
140 // However, we don't actually use typescript-eslint in this project and tslint has no equivalent,
141 // so we can't properly test it.
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// end tests ported from ember-test-helpers
154
155module('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// https://github.com/emberjs/rfcs/blob/master/text/0232-simplify-qunit-testing-api.md#qunit-nested-modules-api
166QUnit.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// http://rwjblue.com/2017/10/23/ember-qunit-simplication/#setuprenderingtest
178module('x-foo', function (hooks) {
179 setupRenderingTest(hooks);
180});
181
182// http://rwjblue.com/2017/10/23/ember-qunit-simplication/#setuptest
183module('foo service', function (hooks) {
184 setupTest(hooks);
185});
186
187// RFC-232 equivalent of https://github.com/ember-engines/ember-engines#unitintegration-testing-for-in-repo-engines
188module('engine foo component', function (hooks) {
189 setupTest(hooks, {
190 resolver: EmberResolver.create(),
191 });
192});
193
194module('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
214module.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
234module('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
325start();
326
327module('with setup options', function (hooks) {
328 // $ExpectType SetupTestOptions | undefined
329 type SetupTestOptions = Parameters<typeof setupTest>[1];
330 // $ExpectType SetupTestOptions | undefined
331 type SetupRenderingTestOptions = Parameters<typeof setupRenderingTest>[1];
332 // $ExpectType SetupTestOptions | undefined
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});