forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-test.ts
More file actions
281 lines (237 loc) · 10.2 KB
/
basic-test.ts
File metadata and controls
281 lines (237 loc) · 10.2 KB
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
import { v1AppScenarios, v2AppScenarios } from './scenarios';
import type { PreparedApp, Scenarios } from 'scenario-tester';
import * as QUnit from 'qunit';
const { module: Qmodule, test } = QUnit;
function basicTest(scenarios: Scenarios, appName: string) {
scenarios
.map('basics', (project) => {
project.mergeFiles({
app: {
'router.js': `
import EmberRouter from '@ember/routing/router';
import config from '${appName}/config/environment';
export default class Router extends EmberRouter {
location = config.locationType;
rootURL = config.rootURL;
}
Router.map(function () {
this.route('example-gjs-route')
});
`,
components: {
'interactive-example.js': `
import { template } from '@ember/template-compiler';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { on } from '@ember/modifier';
export default class extends Component {
@tracked
message = 'Hello';
static {
template("<div class='interactive-example' {{on 'click' this.louder}}>{{this.message}}</div>", {
component: this,
scope: () => ({ on })
})
}
louder = () => {
this.message = this.message + '!';
}
}
`,
},
controllers: {
'example-gjs-route.js': `
import Controller from '@ember/controller';
export default class extends Controller {
exampleControllerField = "This is on the controller";
}
`,
},
routes: {
'example-gjs-route.js': `
import Route from '@ember/routing/route';
export default class extends Route {
model() {
return {
message: "I am the model"
}
}
}
`,
},
templates: {
'example-gjs-route.gjs': `
import Component from '@glimmer/component';
export default class extends Component {
get componentGetter() {
return "I am on the component"
}
<template>
<div data-test="model-field">{{@model.message}}</div>
<div data-test="controller-field">{{@controller.exampleControllerField}}</div>
<div data-test="component-getter">{{this.componentGetter}}</div>
</template>
}
`,
},
},
tests: {
acceptance: {
'example-gjs-route-test.js': `
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from '${appName}/tests/helpers';
module('Acceptance | example gjs route', function (hooks) {
setupApplicationTest(hooks);
test('visiting /example-gjs-route', async function (assert) {
await visit('/example-gjs-route');
assert.strictEqual(currentURL(), '/example-gjs-route');
assert.dom('[data-test="model-field"]').containsText('I am the model');
assert.dom('[data-test="controller-field"]').containsText('This is on the controller');
assert.dom('[data-test="component-getter"]').containsText('I am on the component');
});
});
`,
},
integration: {
'tracked-built-ins-macro-test.gjs': `
import { module, test } from 'qunit';
import { TrackedArray } from 'tracked-built-ins';
import { trackedArray } from '@ember/reactive/collections';
module('tracked-built-ins', function () {
test('it works', function (assert) {
assert.ok(new TrackedArray());
assert.ok(trackedArray());
});
});
`,
'destruction-test.gjs': `
import { module, test } from 'qunit';
import { clearRender, render } from '@ember/test-helpers';
import { setupRenderingTest } from 'ember-qunit';
import { destroy, registerDestructor } from '@ember/destroyable';
import Component from '@glimmer/component';
class WillDestroy extends Component {
willDestroy() {
super.willDestroy();
this.args.onDestroy();
}
}
class Destructor extends Component {
constructor(...args) {
super(...args);
let onDestroy = this.args.onDestroy;
registerDestructor(this, () => onDestroy());
}
}
module('@glimmer/component Destruction', function (hooks) {
setupRenderingTest(hooks);
module('after', function (hooks) {
hooks.after(function (assert) {
assert.verifySteps(['WillDestroy destroyed']);
});
test('it calls "@onDestroy"', async function (assert) {
const onDestroy = () => assert.step('WillDestroy destroyed');
await render(
<template><WillDestroy @onDestroy={{onDestroy}} /></template>
);
});
});
module('afterEach', function (hooks) {
hooks.afterEach(function (assert) {
assert.verifySteps(['WillDestroy destroyed']);
});
test('it calls "@onDestroy"', async function (assert) {
const onDestroy = () => assert.step('WillDestroy destroyed');
await render(
<template><WillDestroy @onDestroy={{onDestroy}} /></template>
);
destroy(this.owner);
});
});
test('it calls "@onDestroy"', async function (assert) {
const onDestroy = () => assert.step('destroyed');
await render(<template><WillDestroy @onDestroy={{onDestroy}} /></template>);
await clearRender();
assert.verifySteps(['destroyed']);
});
test('it calls "registerDestructor"', async function (assert) {
const onDestroy = () => assert.step('destroyed');
await render(<template><Destructor @onDestroy={{onDestroy}} /></template>);
await clearRender();
assert.verifySteps(['destroyed']);
});
});
`,
'interactive-example-test.js': `
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, click } from '@ember/test-helpers';
import { template } from '@ember/template-compiler';
import InteractiveExample from '${appName}/components/interactive-example';
module('Integration | component | interactive-example', function(hooks) {
setupRenderingTest(hooks);
test('initial render', async function(assert) {
await render(template("<InteractiveExample />", {
scope: () => ({ InteractiveExample })
}));
assert.dom('.interactive-example').hasText('Hello');
});
test('interactive update', async function(assert) {
await render(template("<InteractiveExample />", {
scope: () => ({ InteractiveExample })
}));
await click('.interactive-example');
assert.dom('.interactive-example').hasText('Hello!');
});
});
`,
'debug-render-tree-test.gjs': `
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { captureRenderTree } from '@ember/debug';
import Component from '@glimmer/component';
function flattenTree(nodes) {
let result = [];
for (let node of nodes) {
result.push(node);
if (node.children) {
result.push(...flattenTree(node.children));
}
}
return result;
}
class HelloWorld extends Component {
<template>{{@arg}}</template>
}
module('Integration | captureRenderTree', function (hooks) {
setupRenderingTest(hooks);
test('scope-based components have correct names in debugRenderTree', async function (assert) {
await render(<template><HelloWorld @arg="first" /></template>);
let tree = captureRenderTree(this.owner);
let allNodes = flattenTree(tree);
let names = allNodes.filter(n => n.type === 'component').map(n => n.name);
assert.true(names.includes('HelloWorld'), 'HelloWorld component name is preserved in the render tree (found: ' + names.join(', ') + ')');
});
});
`,
},
},
});
})
.forEachScenario((scenario) => {
Qmodule(scenario.name, function (hooks) {
let app: PreparedApp;
hooks.before(async () => {
app = await scenario.prepare();
});
test(`ember test`, async function (assert) {
let result = await app.execute(`pnpm test`);
assert.equal(result.exitCode, 0, result.output);
});
});
});
}
basicTest(v1AppScenarios, 'ember-test-app');
basicTest(v2AppScenarios, 'v2-app-template');