forked from emberjs/ember-qunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.ts
339 lines (265 loc) · 9.3 KB
/
tests.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// These tests were ported directly from DefinitelyTyped and are unlikely to be
// 100% desireable for the future.
import { hbs } from 'ember-cli-htmlbars';
import { module } from 'qunit';
import {
start,
test,
skip,
only,
todo,
setResolver,
setupRenderingTest,
setupTest,
setupApplicationTest,
} from 'ember-qunit';
import { render, TestContext } from '@ember/test-helpers';
import EmberResolver from 'ember-resolver';
import EmberObject from '@ember/object';
// if you don't have a custom resolver, do it like this:
setResolver(EmberResolver.create());
// (modified) tests ported from ember-test-helpers
module('rendering', function (hooks) {
setupRenderingTest(hooks);
test('it renders', async function (assert) {
assert.expect(2);
// setup the outer context
this.set('value', 'cat');
// render the component
await render(hbs`
{{ x-foo value=value action="result" }}
`);
// has to be a template
await render(hbs);
await render(hbs`{{ x-foo value=value action="result" data-test-content}}`);
assert.equal('[data-test-content]', 'cat', 'The component shows the correct value');
});
// this test doesn't make sense, but it also doesn't fail, so I'm setting it to skip until we can improve it.
skip('it renders', async function (assert) {
assert.expect(1);
// creates the component instance
await render(hbs`<Foo />`);
await render(hbs`<Foo @item={{42}} />`);
const { inputFormat } = this.setProperties({
inputFormat: 'M/D/YY',
outputFormat: 'MMMM D, YYYY',
date: '5/3/10',
});
const { inputFormat: if2, outputFormat } = this.getProperties(
'inputFormat',
'outputFormat'
);
const inputFormat2 = this.get('inputFormat');
// render the component on the page
await render(hbs`<div data-test-content>bar</div>`);
assert.equal('[data-test-content]', 'bar');
});
});
module('misc and async', function (hooks) {
hooks.beforeEach(async function (assert) {
assert.ok(true, 'hooks can be async');
});
test('It can calculate the result', function (assert) {
assert.expect(1);
interface Foo extends EmberObject {
value: string;
result: string;
}
const subject = this.owner.lookup('foo:bar') as Foo;
subject.set('value', 'foo');
assert.equal(subject.get('result'), 'bar');
});
// This test is intended to ensure the appropriate behavior for @typescript-eslint/no-misused-promises.
// However, we don't actually use typescript-eslint in this project and tslint has no equivalent,
// so we can't properly test it.
test('it can be async', async function (assert) {
assert.expect(1);
await render(hbs`<p>Hello</p>`);
assert.ok(true, 'rendered');
});
skip('disabled test');
skip('disabled test', function (assert) {});
// This test is intended to ensure the appropriate behavior for @typescript-eslint/no-misused-promises.
// However, we don't actually use typescript-eslint in this project and tslint has no equivalent,
// so we can't properly test it.
skip('it can skip async', async function (assert) {
assert.expect(1);
await render(hbs`<p>Hello</p>`);
assert.ok(true, 'rendered');
});
// This test is intended to ensure the appropriate behavior for @typescript-eslint/no-misused-promises.
// However, we don't actually use typescript-eslint in this project and tslint has no equivalent,
// so we can't properly test it.
only(
'it can only run async',
async function (assert) {
assert.expect(1);
await render(hbs`<p>Hello</p>`);
assert.ok(true, 'rendered');
}
);
// This test is intended to ensure the appropriate behavior for @typescript-eslint/no-misused-promises.
// However, we don't actually use typescript-eslint in this project and tslint has no equivalent,
// so we can't properly test it.
todo(
'it can have an async todo',
async function (assert) {
assert.expect(1);
await render(hbs`<p>Hello</p>`);
assert.ok(true, 'rendered');
}
);
});
// end tests ported from ember-test-helpers
module('returning a promise', function () {
test('it can return Promise<void>', function (this: TestContext, assert) {
return Promise.resolve();
});
test('it can return a non-empty Promise', function (this: TestContext, assert) {
return Promise.resolve('foo');
});
});
// https://github.com/emberjs/rfcs/blob/master/text/0232-simplify-qunit-testing-api.md#qunit-nested-modules-api
QUnit.module('some description', function (hooks) {
hooks.before(() => {});
hooks.beforeEach(() => {});
hooks.afterEach(() => {});
hooks.after(() => {});
QUnit.test('it blends', function (assert) {
assert.ok(true, 'of course!');
});
});
// http://rwjblue.com/2017/10/23/ember-qunit-simplication/#setuprenderingtest
module('x-foo', function (hooks) {
setupRenderingTest(hooks);
});
// http://rwjblue.com/2017/10/23/ember-qunit-simplication/#setuptest
module('foo service', function (hooks) {
setupTest(hooks);
});
// RFC-232 equivalent of https://github.com/ember-engines/ember-engines#unitintegration-testing-for-in-repo-engines
module('engine foo component', function (hooks) {
setupTest(hooks, {
resolver: EmberResolver.create(),
});
});
module('all the hooks', function (hooks) {
setupTest(hooks);
hooks.after(function () {
this.owner.lookup('service:store');
});
hooks.afterEach(function () {
this.owner.lookup('service:store');
});
hooks.before(function () {
this.owner.lookup('service:store');
});
hooks.beforeEach(function () {
this.owner.lookup('service:store');
});
});
module.only('exclusive module with hooks', function (hooks) {
setupTest(hooks);
hooks.after(function () {
this.owner.lookup('service:store');
});
hooks.afterEach(function () {
this.owner.lookup('service:store');
});
hooks.before(function () {
this.owner.lookup('service:store');
});
hooks.beforeEach(function () {
this.owner.lookup('service:store');
});
});
module('extending TestContext works', function () {
interface Context extends TestContext {
someProp: string;
anotherProp: boolean;
}
module('it works with non-async', function (nonAsyncHooks) {
nonAsyncHooks.before(function (this: Context) {
this.someProp = 'hello';
});
nonAsyncHooks.beforeEach(function (this: Context) {
this.anotherProp = true;
});
nonAsyncHooks.after(function (this: Context) {
this.someProp = 'goodbye';
});
nonAsyncHooks.afterEach(function (this: Context) {
this.anotherProp = false;
});
test('it works with tests', function (this: Context, assert) {
this.someProp = this.someProp + ' cool person';
assert.true(this.anotherProp);
});
skip('it works with skip', function (this: Context, assert) {
this.someProp = 'wahoo';
assert.ok(typeof this.someProp === 'string');
});
only('it works with only', function (this: Context, assert) {
this.someProp = 'crazy pants';
assert.ok(typeof this.someProp === 'string');
});
todo('it works with todo', function (this: Context, assert) {
this.someProp = 'tada';
assert.ok(typeof this.someProp === 'string');
});
});
module('it works with async, too', function (asyncHooks) {
asyncHooks.before(async function (this: Context) {
this.someProp = 'hello';
await Promise.resolve(this.someProp);
});
asyncHooks.beforeEach(async function (this: Context) {
this.anotherProp = true;
await Promise.resolve(this.anotherProp);
});
asyncHooks.after(async function (this: Context) {
this.someProp = 'goodbye';
await Promise.resolve(this.someProp);
});
asyncHooks.afterEach(async function (this: Context) {
this.anotherProp = false;
await Promise.resolve(this.anotherProp);
});
test('it works with tests', async function (this: Context, assert) {
this.someProp = this.someProp + ' cool person';
assert.true(this.anotherProp);
await Promise.resolve('cool');
});
skip('it works with skip', async function (this: Context, assert) {
this.someProp = 'wahoo';
const result = await Promise.resolve(this.someProp);
assert.ok(typeof result === 'string');
});
only('it works with only', async function (this: Context, assert) {
this.someProp = 'crazy pants';
const result = await Promise.resolve(this.someProp);
assert.ok(typeof result === 'string');
});
todo('it works with todo', async function (this: Context, assert) {
this.someProp = 'tada';
const result = await Promise.resolve(this.someProp);
assert.ok(typeof result === 'string');
});
});
});
start();
module('with setup options', function (hooks) {
// $ExpectType SetupTestOptions | undefined
type SetupTestOptions = Parameters<typeof setupTest>[1];
// $ExpectType SetupTestOptions | undefined
type SetupRenderingTestOptions = Parameters<typeof setupRenderingTest>[1];
// $ExpectType SetupTestOptions | undefined
type SetupApplicationTestOptions = Parameters<typeof setupApplicationTest>[1];
const resolver = EmberResolver.create();
setupTest(hooks, {});
setupRenderingTest(hooks, {});
setupApplicationTest(hooks, {});
setupTest(hooks, { resolver });
setupRenderingTest(hooks, { resolver });
setupApplicationTest(hooks, { resolver });
});