-
Notifications
You must be signed in to change notification settings - Fork 137
/
static-app-test.ts
387 lines (345 loc) · 14.4 KB
/
static-app-test.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import { appScenarios } from './scenarios';
import { PreparedApp, Project } from 'scenario-tester';
import QUnit from 'qunit';
import merge from 'lodash/merge';
import { dirname } from 'path';
const { module: Qmodule, test } = QUnit;
appScenarios
.map('static-app', project => {
project.linkDevDependency('bootstrap', { baseDir: __dirname });
project.addDevDependency(emberBootstrap());
project.linkDevDependency('@embroider/macros', { baseDir: __dirname });
project.linkDevDependency('ember-composable-helpers', { baseDir: __dirname });
merge(project.files, {
app: {
adapters: {
'post.js': `
import JSONAPIAdapter from '@ember-data/adapter/json-api';
export default class PostAdapter extends JSONAPIAdapter {
findRecord(store, type, id, snapshot) {
return { data: { type: 'posts', id: '0', attributes: { title: 'Hello world' } } };
}
}
`,
},
components: {
'fancy-box.js': `
import Component from '@glimmer/component';
export default class FancyBox extends Component {
get titleComponentWithDefault() {
return this.args.titleComponent || 'default-title';
}
}
`,
},
helpers: {
'loaded-components.js': `
/* global requirejs */
import { helper } from '@ember/component/helper';
export function loadedComponents() {
let result = new Set();
for (let name of Object.keys(requirejs.entries)) {
let m = /^[a-zA-Z0-9_-]+\\/components\\/(.*)/.exec(name);
if (m) {
result.add(m[1]);
}
m = /^[a-zA-Z0-9_-]+\\/templates\\/components\\/(.*)/.exec(name);
if (m) {
result.add(m[1]);
}
}
return [...result];
}
export default helper(loadedComponents);
`,
'loaded-helpers.js': `
/* global requirejs */
import { helper } from '@ember/component/helper';
export function loadedHelpers() {
return Object.keys(requirejs.entries)
.map(k => { let m = /^[a-zA-Z0-9_-]+\\/helpers\\/(.*)/.exec(k); if (m){ return m[1]}}).filter(Boolean).sort();
}
export default helper(loadedHelpers);
`,
},
models: {
'post.js': `
import Model, { attr } from '@ember-data/model';
export default class PostModel extends Model {
@attr() title;
}
`,
},
routes: {
'ember-data-example.js': `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class EmberDataExampleRoute extends Route {
@service() store;
model() {
return this.store.findRecord('post', 0);
}
}
`,
},
serializers: {
'application.js': `
import JSONAPISerializer from '@ember-data/serializer/json-api';
export default class extends JSONAPISerializer {};
`,
},
templates: {
components: {
'default-title.hbs': `
<div data-example="default" class="the-default-title-component">{{@title}}</div>
`,
'fancy-box.hbs': `
{{component this.titleComponentWithDefault title=@title}}
`,
'my-title.hbs': `
<div data-example="customized" class="my-title-component">{{@title}}</div>
`,
},
'components-example.hbs': `
{{! this uses a component from ember-bootstrap }}
<BsButton>Button</BsButton>
{{! then this lists all the components loaded into our app.}}
{{#each (loaded-components) as |name|}}
<div data-component-name={{name}}>{{name}}</div>
{{/each}}
`,
'helpers-example.hbs': `
{{! this uses two helpers from ember-composable-helpers }}
{{#each (reverse (array "alpha" "beta")) as |word| }}
<div data-word={{word}}>{{word}}</div>
{{/each}}
{{! then this lists all the helpers loaded into our app. It should have the two
above, but none of the other stuff from composable-helpers }}
{{#each (loaded-helpers) as |name|}}
<div data-helper-name={{name}}>{{name}}</div>
{{/each}}
`,
'macros-example.hbs': `
<h1 data-macro>Welcome to this {{#if (macroCondition (macroGetOwnConfig "isClassic"))}}classic{{else}}embroider{{/if}} app!</h1>
`,
'static-component-rules-example.hbs': `
<FancyBox @title="With Default" />
<FancyBox @title="With Custom" @titleComponent="my-title" />
`,
'ember-data-example.hbs': `<h1>{{@model.title}}</h1>`,
},
'router.js': `
import EmberRouter from '@ember/routing/router';
import config from 'app-template/config/environment';
export default class Router extends EmberRouter {
location = config.locationType;
rootURL = config.rootURL;
}
Router.map(function() {
this.route('helpers-example');
this.route('components-example');
this.route('static-component-rules-example');
this.route('macros-example');
this.route('ember-data-example');
});
`,
},
tests: {
acceptance: {
'components-example-test.js': `
import { module, test } from 'qunit';
import { visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { getOwnConfig } from '@embroider/macros';
module('Acceptance | components-example', function(hooks) {
setupApplicationTest(hooks);
test('static components', async function(assert) {
await visit('/components-example');
let button = document.querySelector('.btn');
assert.ok(button, 'found ember-bootstrap button');
if (button) {
assert.equal(getComputedStyle(button)['background-color'], "rgb(108, 117, 125)", "bs-button has its CSS");
}
let components = [...document.querySelectorAll("[data-component-name]")].map(elt => elt.dataset.componentName);
assert.ok(components.includes('bs-button'), 'expected to find bs-button');
if (getOwnConfig().isClassic) {
assert.ok(components.includes('bs-carousel'), 'expected to find bs-carousel in classic build');
} else {
assert.ok(!components.includes('bs-carousel'), 'expected not to find bs-carousel in embroider build');
}
});
});
`,
'helpers-example-test.js': `
import { module, test } from 'qunit';
import { visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { getOwnConfig } from '@embroider/macros';
module('Acceptance | helpers-example', function(hooks) {
setupApplicationTest(hooks);
test('static helpers', async function(assert) {
await visit('/helpers-example');
assert.deepEqual(
[...document.querySelectorAll("[data-word]")].map(elt => elt.dataset.word),
['beta', 'alpha'],
'array and reverse worked'
);
let helpers = [...document.querySelectorAll("[data-helper-name]")].map(elt => elt.dataset.helperName);
assert.ok(helpers.includes('reverse'), 'expected to find reverse');
if (getOwnConfig().isClassic) {
assert.ok(helpers.includes('intersect'), 'expected to find intersect');
} else {
assert.ok(!helpers.includes('intersect'), 'expected not to find intersect');
}
});
});
`,
'macros-example-test.js': `
import { module, test } from 'qunit';
import { visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { getOwnConfig } from '@embroider/macros';
module('Acceptance | macros-example', function(hooks) {
setupApplicationTest(hooks);
test('macros work', async function(assert) {
await visit('/macros-example');
if (getOwnConfig().isClassic) {
assert.dom('[data-macro]').hasText('Welcome to this classic app!');
} else {
assert.dom('[data-macro]').hasText('Welcome to this embroider app!');
}
});
});
`,
'static-component-rules-example-test.js': `
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
module('Acceptance | static component rules example', function(hooks) {
setupApplicationTest(hooks);
test('visiting /static-component-rules-example', async function(assert) {
await visit('/static-component-rules-example');
assert.equal(currentURL(), '/static-component-rules-example');
assert.ok(document.querySelector('[data-example="default"].the-default-title-component'), 'default exists');
assert.ok(document.querySelector('[data-example="customized"].my-title-component'), 'customized exists');
});
});
`,
'ember-data-example-test.js': `
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
module('Acceptance | ember data example', function (hooks) {
setupApplicationTest(hooks);
test('visiting /ember-data-example', async function (assert) {
await visit('/ember-data-example');
assert.equal(currentURL(), '/ember-data-example');
assert.dom('h1').containsText('Hello world');
});
});
`,
},
unit: {
'missing-import-sync-renamed-test.js': `
import { module, test } from 'qunit';
import { importSync as i } from '@embroider/macros';
module('Unit | missing modules referenced by i which was renamed from importSync', function() {
test('it works', function(assert) {
assert.expect(2);
assert.throws(() => {
i('foobar');
}, /Error: Could not find module \`foobar\`/);
assert.throws(() => {
i('foobaz');
}, /Error: Could not find module \`foobaz\`/);
});
});
`,
'missing-import-test.js': `
import { module, test } from 'qunit';
import { importSync } from '@embroider/macros';
module('Unit | missing modules referenced by importSync', function() {
test('it works', function(assert) {
assert.expect(2);
assert.throws(() => {
importSync('bar');
}, /Error: Could not find module \`bar\`/);
assert.throws(() => {
importSync('baz');
}, /Error: Could not find module \`baz\`/);
});
});
`,
},
},
'ember-cli-build.js': `
'use strict';
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const { MacrosConfig } = require('@embroider/macros/src/node');
module.exports = function (defaults) {
let app = new EmberApp(defaults, {
'ember-bootstrap': {
bootstrapVersion: 4,
importBootstrapCSS: true
}
});
MacrosConfig.for(app).setOwnConfig(__filename, {
isClassic: Boolean(process.env.CLASSIC),
});
if (process.env.CLASSIC) {
return app.toTree();
}
const Webpack = require('@embroider/webpack').Webpack;
return require('@embroider/compat').compatBuild(app, Webpack, {
workspaceDir: process.env.WORKSPACE_DIR,
staticAddonTestSupportTrees: true,
staticAddonTrees: true,
staticComponents: true,
staticHelpers: true,
staticModifiers: true,
packageRules: [
{
package: 'app-template',
appModules: {
'components/fancy-box.js': {
dependsOnComponents: ['{{default-title}}'],
},
},
components: {
'{{fancy-box}}': {
acceptsComponentArguments: [{ name: 'titleComponent', becomes: 'this.titleComponentWithDefault' }],
},
},
},
],
skipBabel: [
{ package: 'qunit' },
{ package: 'macro-decorators' },
],
});
};
`,
});
})
.forEachScenario(scenario => {
Qmodule(scenario.name, function (hooks) {
let app: PreparedApp;
hooks.before(async () => {
app = await scenario.prepare();
});
['production', 'development'].forEach(env => {
test(`yarn test: ${env}`, async function (assert) {
let result = await app.execute(`cross-env EMBER_ENV=${env} yarn test`);
assert.equal(result.exitCode, 0, result.output);
});
});
});
});
function emberBootstrap() {
// https://github.com/kaliber5/ember-bootstrap/pull/1750
let modifiers = Project.fromDir(dirname(require.resolve('@ember/render-modifiers')), { linkDeps: true });
modifiers.removeDependency('ember-source');
let eb = Project.fromDir(dirname(require.resolve('ember-bootstrap')), { linkDeps: true });
eb.addDependency(modifiers);
return eb;
}