Skip to content

Commit

Permalink
Add v2 addon-dev tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Mar 10, 2022
1 parent b2067e3 commit 2071415
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/addon-dev/sample-rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import babel from '@rollup/plugin-babel';
import { babel } from '@rollup/plugin-babel';
import { Addon } from '@embroider/addon-dev/rollup';

const addon = new Addon({
Expand Down
23 changes: 15 additions & 8 deletions tests/scenarios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"dependencies": {
"@embroider/shared-internals": "1.5.0",
"@types/qunit": "^2.11.1",
"execa": "^5.0.0",
"fastboot": "^3.1.0",
"fs-extra": "^10.0.0",
"globby": "^11.0.3",
Expand All @@ -21,37 +22,43 @@
},
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.17.5",
"@babel/plugin-proposal-class-properties": "^7.16.7",
"@babel/plugin-proposal-decorators": "^7.17.2",
"@babel/preset-env": "^7.16.11",
"@ember/string": "^1.0.0",
"@embroider/macros": "1.5.0",
"@embroider/addon-shim": "1.5.0",
"@embroider/router": "1.5.0",
"@embroider/util": "1.5.0",
"@rollup/plugin-babel": "^5.3.1",
"bootstrap": "^4.3.1",
"broccoli-funnel": "^3.0.5",
"broccoli-merge-trees": "^3.0.2",
"broccoli-persistent-filter": "^3.1.2",
"ember-bootstrap": "^5.0.0",
"ember-cli": "~3.28.0",
"ember-cli-3.16": "npm:ember-cli@~3.16.0",
"ember-cli-3.24": "npm:ember-cli@~3.24.0",
"ember-cli": "~3.28.0",
"ember-cli-beta": "npm:ember-cli@beta",
"ember-cli-htmlbars-inline-precompile": "^2.1.0",
"ember-cli-fastboot": "^3.2.0",
"ember-cli-htmlbars-3": "npm:ember-cli-htmlbars@3",
"ember-cli-htmlbars-inline-precompile": "^2.1.0",
"ember-cli-latest": "npm:ember-cli@latest",
"ember-cli-fastboot": "^3.2.0",
"ember-composable-helpers": "^4.4.1",
"ember-data": "~3.28.0",
"ember-data-3.16": "npm:ember-data@~3.16.0",
"ember-data-3.24": "npm:ember-data@~3.24.0",
"ember-data": "~3.28.0",
"ember-data-latest": "npm:ember-data@latest",
"ember-engines": "^0.8.17",
"ember-inline-svg": "^0.2.1",
"ember-source-latest": "npm:ember-source@latest",
"ember-source-beta": "npm:ember-source@beta",
"ember-source": "~3.28.0",
"ember-source-3.16": "npm:ember-source@~3.16.0",
"ember-source-3.24": "npm:ember-source@~3.24.0",
"ember-source": "~3.28.0",
"ember-truth-helpers": "^3.0.0"
"ember-source-beta": "npm:ember-source@beta",
"ember-source-latest": "npm:ember-source@latest",
"ember-truth-helpers": "^3.0.0",
"rollup": "^2.69.1"
},
"volta": {
"node": "14.16.1",
Expand Down
227 changes: 227 additions & 0 deletions tests/scenarios/v2-addon-dev-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import path from 'path';
import { appScenarios, baseV2Addon } from './scenarios';
import { PreparedApp } from 'scenario-tester';
import QUnit from 'qunit';
import merge from 'lodash/merge';
import execa from 'execa';
import { pathExists } from 'fs-extra';

const { module: Qmodule, test } = QUnit;

/**
* The type of addon this is testing with only works in
* [email protected]+
*/
appScenarios
.skip('lts_3_16')
.skip('lts_3_24')
.map('v2 addon can have imports of template-only components', async project => {
let addon = baseV2Addon();
addon.pkg.name = 'v2-addon';
addon.pkg.files = ['dist'];
addon.pkg.exports = {
'./*': './dist/*',
'./addon-main.js': './addon-main.js',
};

merge(addon.files, {
'babel.config.json': `
{
"presets": [
["@babel/preset-env", {
"targets": ["last 1 firefox versions"]
}]
],
"plugins": [
"@embroider/addon-dev/template-colocation-plugin",
["@babel/plugin-proposal-decorators", { "legacy": true }],
[ "@babel/plugin-proposal-class-properties" ]
]
}
`,
'rollup.config.mjs': `
import { babel } from '@rollup/plugin-babel';
import { Addon } from '@embroider/addon-dev/rollup';
const addon = new Addon({
srcDir: 'src',
destDir: 'dist',
});
export default {
output: addon.output(),
plugins: [
addon.publicEntrypoints([
'**/*.js',
'components/demo/out.hbs',
]),
addon.appReexports([
'components/**/*.js',
'components/demo/out.hbs',
]),
addon.hbs(),
addon.dependencies(),
babel({ babelHelpers: 'bundled' }),
addon.clean(),
],
};
`,
src: {
components: {
demo: {
'button.hbs': `
<button {{on 'click' @onClick}}>
flip
</button>
`,
'out.hbs': `
<out>{{yield}}</out>
`,
'index.js': `
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import FlipButton from './button';
import BlahButton from './button.hbs';
import Out from './out';
export default class ExampleComponent extends Component {
Button = FlipButton;
Button2 = BlahButton;
Out = Out;
@tracked active = false;
flip = () => (this.active = !this.active);
}
`,
'index.hbs': `
Hello there!
<this.Out>{{this.active}}</this.Out>
<this.Button @onClick={{this.flip}} />
<this.Button2 @onClick={{this.flip}} />
`,
},
},
},
});

addon.linkDependency('@embroider/addon-shim', { baseDir: __dirname });
addon.linkDependency('@embroider/addon-dev', { baseDir: __dirname });
addon.linkDevDependency('@babel/core', { baseDir: __dirname });
addon.linkDevDependency('@babel/plugin-proposal-class-properties', { baseDir: __dirname });
addon.linkDevDependency('@babel/plugin-proposal-decorators', { baseDir: __dirname });
addon.linkDevDependency('@babel/preset-env', { baseDir: __dirname });
addon.linkDevDependency('@rollup/plugin-babel', { baseDir: __dirname });
addon.linkDevDependency('rollup', { baseDir: __dirname });

project.addDevDependency(addon);

merge(project.files, {
tests: {
'the-test.js': `
import { click, render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
module('v2 addon tests', function (hooks) {
setupRenderingTest(hooks);
test('<Demo />', async function (assert) {
await render(hbs\`<Demo />\`);
assert.dom('out').containsText('false');
await click('button');
assert.dom('out').containsText('true');
});
test('<Demo::Out />', async function (assert) {
await render(hbs\`<Demo::Out>hi</Demo::Out>\`);
assert.dom('out').containsText('hi');
});
});
`,
},
});
})
.forEachScenario(scenario => {
Qmodule(scenario.name, function (hooks) {
let app: PreparedApp;

async function getAddonInfo() {
let pkgPath = path.resolve(path.join(app.dir, 'node_modules/v2-addon/package.json'));
let dir = path.dirname(pkgPath);

return {
dir,
distDir: path.join(dir, 'dist'),
build: async () => {
let rollupBin = 'node_modules/rollup/dist/bin/rollup';

await execa(path.join(dir, rollupBin), ['-c', './rollup.config.mjs'], {
cwd: dir,
});
},
reExports: async () => {
let pkgInfo = await import(pkgPath);
return pkgInfo['ember-addon']['app-js'] as Record<string, string>;
},
};
}

hooks.before(async () => {
app = await scenario.prepare();

let { build } = await getAddonInfo();

await build();
});

Qmodule('The addon', function () {
test('output directories exist', async function (assert) {
let { distDir } = await getAddonInfo();

assert.strictEqual(await pathExists(distDir), true, 'dist/');
assert.strictEqual(await pathExists(path.join(distDir, '_app_')), true, 'dist/_app_');
});

test('package.json is modified appropriately', async function (assert) {
let { reExports } = await getAddonInfo();

assert.deepEqual(await reExports(), {
'./components/demo/index.js': './dist/_app_/components/demo/index.js',
'./components/demo/out.js': './dist/_app_/components/demo/out.js',
});
});

test('the addon was built successfully', async function (assert) {
let { reExports, dir } = await getAddonInfo();
let files = Object.values(await reExports());

assert.expect(files.length);

for (let pathName of files) {
assert.deepEqual(await pathExists(path.join(dir, pathName)), true, `pathExists: ${pathName}`);
}
});
});

Qmodule('Consuming app', function () {
test(`yarn test`, async function (assert) {
let result = await app.execute('yarn test');
assert.equal(result.exitCode, 0, result.output);
});
});
});
});
Loading

0 comments on commit 2071415

Please sign in to comment.