Skip to content

Commit

Permalink
Merge pull request #859 from thoov/convert-macro-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Jun 18, 2021
2 parents acdc91c + a3eba4b commit 86e06dd
Show file tree
Hide file tree
Showing 29 changed files with 699 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tests/fixtures/funky-sample-addon/addon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import FakeOther from 'fake-module';

const {
foo, bar
} = FakeOther;

export default function exampleAddonFunction() {
if (foo.isFoo && bar.isBar) {
return true;
} else {
throw new Error('fake-module is not properly importable');
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/funky-sample-addon/fake-other-module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
foo: { isFoo: true },
bar: { isBar: true },
}
35 changes: 35 additions & 0 deletions tests/fixtures/funky-sample-addon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
const path = require('path');

process.env.EMBER_CLI_IGNORE_ADDON_NAME_MISMATCH = true;
module.exports = {
name: 'funky-sample-addon',

treeForAddon() {
const addonTree = this._super.treeForAddon.apply(this, arguments),
fakeModuleSrc = __dirname + '/fake-other-module/index.js';

const fakeModuleTree = funnel(path.dirname(fakeModuleSrc), {
files: [path.basename(fakeModuleSrc)],
getDestinationPath() {
return 'fake-module.js';
}
});

const babelAddon = this.addons.find(
addon => addon.name === 'ember-cli-babel'
);

const modulesArtdeco = funnel(babelAddon.transpileTree(fakeModuleTree), {
destDir: 'modules'
});

return mergeTrees([
addonTree,
modulesArtdeco
]);
}
};
16 changes: 16 additions & 0 deletions tests/fixtures/funky-sample-addon/tests/unit/dummy-app-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import exampleAddonFunction from 'funky-sample-addon';

import FakeOther from 'fake-module';

const {
foo, bar
} = FakeOther;

module('Unit | funky babel step created module is importable', function() {
test('that it works correctly', async function(assert) {
assert.ok(exampleAddonFunction());
assert.ok(foo.isFoo);
assert.ok(bar.isBar);
});
});
11 changes: 11 additions & 0 deletions tests/fixtures/macro-test/app/controllers/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Controller from '@ember/controller';
import { getOwnConfig, isTesting, isDevelopingApp } from '@embroider/macros';

export default class Application extends Controller {
constructor() {
super(...arguments);
this.mode = getOwnConfig()['mode'];
this.isTesting = isTesting();
this.isDeveloping = isDevelopingApp();
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/macro-test/app/helpers/reflect-addon-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { helper } from '@ember/component/helper';
import { getConfig } from '@embroider/macros';

export function reflectAddonConfig(/*params, hash*/) {
return getConfig('macro-sample-addon');
}

export default helper(reflectAddonConfig);
4 changes: 4 additions & 0 deletions tests/fixtures/macro-test/app/templates/application.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div data-test-mode>{{this.mode}}</div>
<div data-test-count>{{macroGetOwnConfig "count"}}</div>
<div>isDeveloping: <span data-test-developing>{{this.isDeveloping}}</span></div>
<div>isTesting: <span data-test-testing>{{this.isTesting}}</span></div>
49 changes: 49 additions & 0 deletions tests/fixtures/macro-test/ember-cli-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const EmberApp = require('ember-cli/lib/broccoli/ember-app');

module.exports = function (defaults) {
let app = new EmberApp(defaults, {
'@embroider/macros': {
setOwnConfig: {
mode: 'amazing',
count: 42,
inner: {
items: [{ name: 'Arthur', awesome: true }],
description: null,
},
},
setConfig: {
'ember-source': {
color: 'orange',
},
'macro-sample-addon': {
configFromMacrosTests: 'exists',
},
},
},
});

app.import('vendor/apple.js', {
using: [{ transformation: 'amd', as: 'amd' }],
outputFile: 'apple.js',
});

app.import('vendor/four.js', { outputFile: 'ordered.js' });
app.import('vendor/two.js', { outputFile: 'ordered.js' });
app.import('vendor/three.js', { outputFile: 'ordered.js' });
app.import('vendor/one.js', { outputFile: 'ordered.js' });

app.import('vendor/prepend/one.js', { prepend: true });
app.import('vendor/prepend/two.js', { prepend: true });
app.import('vendor/prepend/three.js', { prepend: true });
app.import('vendor/prepend/four.js', { prepend: true });
app.import('vendor/prepend/order.js', { prepend: true });

if (process.env.CLASSIC) {
return app.toTree();
}

const Webpack = require('@embroider/webpack').Webpack;
return require('@embroider/compat').compatBuild(app, Webpack);
};
52 changes: 52 additions & 0 deletions tests/fixtures/macro-test/tests/acceptance/basic-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';

module('Acceptance | smoke tests', function(hooks) {
setupApplicationTest(hooks);

test('ensure all scripts in index.html 200', async function(assert) {
for (let { src } of document.scripts) {
let { status } = await fetch(src);
assert.equal(status, 200, `expected: '${src}' to be accessible`);
}
});

test('JS getOwnConfig worked', async function(assert) {
await visit('/');
assert.equal(currentURL(), '/');
assert.equal(this.element.querySelector('[data-test-mode]').textContent.trim(), 'amazing');
});

test('HBS getOwnConfig worked', async function(assert) {
await visit('/');
assert.equal(currentURL(), '/');
assert.equal(this.element.querySelector('[data-test-count]').textContent.trim(), '42');
});

test('JS isTesting worked', async function(assert) {
await visit('/');
assert.equal(currentURL(), '/');
assert.equal(this.element.querySelector('[data-test-testing]').textContent.trim(), 'true');
});

test('/ordered.js is ordered correctly', function(assert) {
assert.deepEqual(self.ORDER, [
// these come via app.import(name, { prepend: true });
// which ultimately end up in vendor.js
// and vendor.js is loaded first
'prepend/four',
'prepend/three',
'prepend/two',
'prepend/one',

// these come via app.import(name, { outputFile: 'ordered.js' });
// so they will end up in ordered.js
// and ordered.js is loaded after vendor.js
'FOUR',
'TWO',
'THREE',
'ONE',
]);
});
});
45 changes: 45 additions & 0 deletions tests/fixtures/macro-test/tests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>App-Template Tests</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

{{content-for "head"}}
{{content-for "test-head"}}

<link rel="stylesheet" href="{{rootURL}}assets/vendor.css">
<link rel="stylesheet" href="{{rootURL}}assets/app-template.css">
<link rel="stylesheet" href="{{rootURL}}assets/test-support.css">

{{content-for "head-footer"}}
{{content-for "test-head-footer"}}
</head>
<body>
{{content-for "body"}}
{{content-for "test-body"}}

<div id="qunit"></div>
<div id="qunit-fixture">
<div id="ember-testing-container">
<div id="ember-testing"></div>
</div>
</div>

<script>
self.ORDER = [];
</script>
<script src="/testem.js" integrity=""></script>
<script src="{{rootURL}}assets/vendor.js"></script>
<script src="{{rootURL}}apple.js"></script>
<script src="{{rootURL}}ordered.js"></script>
<script src="{{rootURL}}assets/test-support.js"></script>
<script src="{{rootURL}}assets/app-template.js"></script>
<script src="{{rootURL}}assets/tests.js"></script>

{{content-for "body-footer"}}
{{content-for "test-body-footer"}}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Macro | common', function(hooks) {
setupRenderingTest(hooks);

test('our macros do not shadow local variables', async function(assert) {
await render(hbs`{{#with "hello" as |macroDependencySatisfies|}} {{macroDependencySatisfies}} {{/with}}`);
assert.equal(this.element.textContent.trim(), 'hello');
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { helper } from '@ember/component/helper';
import { reflectAddonConfig } from 'app-template/helpers/reflect-addon-config';

module('Integration | cross-package-config', function (hooks) {
setupRenderingTest(hooks);

test(`addon's JS can see addon's merged config`, async function (assert) {
assert.expect(1);
this.owner.register(
'helper:my-assertion',
helper(function ([value]) {
assert.deepEqual(value, { hello: 'world', configFromMacrosTests: 'exists' });
})
);
await render(hbs`{{my-assertion (reflect-config)}}`);
});

test(`app's JS can see addon's merged config`, async function (assert) {
assert.deepEqual(reflectAddonConfig(), { hello: 'world', configFromMacrosTests: 'exists' });
});

test(`addon's HBS can see addon's merged config`, async function (assert) {
assert.expect(1);
this.owner.register(
'helper:my-assertion',
helper(function ([value]) {
assert.deepEqual(value, { hello: 'world', configFromMacrosTests: 'exists' });
})
);
await render(hbs`{{#reflect-hbs-config as |config|}} {{my-assertion config}} {{/reflect-hbs-config}}`);
});

test(`app's HBS can see addon's merged config`, async function (assert) {
assert.expect(1);
this.owner.register(
'helper:my-assertion',
helper(function ([value]) {
assert.deepEqual(value, { hello: 'world', configFromMacrosTests: 'exists' });
})
);
await render(hbs`{{my-assertion (macroGetConfig "macro-sample-addon" )}}`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { helper } from '@ember/component/helper';

module('Integration | Macro | dependencySatisfies', function(hooks) {
setupRenderingTest(hooks);

test('macroDependencySatisfies in content position', async function(assert) {
await render(hbs`{{macroDependencySatisfies "ember-cli" "*"}}`);
assert.equal(this.element.textContent.trim(), 'true');
});

test('macroDependencySatisfies in subexpression position', async function(assert) {
assert.expect(1);
this.owner.register('helper:my-assertion', helper(function([value]) {
assert.strictEqual(value, true);
}));
await render(hbs`{{my-assertion (macroDependencySatisfies "ember-cli" "*") }}`);
});

test('macroDependencySatisfies emits false for missing package', async function(assert) {
assert.expect(1);
this.owner.register('helper:my-assertion', helper(function([value]) {
assert.strictEqual(value, false);
}));
await render(hbs`{{my-assertion (macroDependencySatisfies "not-a-package" "*") }}`);
});

test('macroDependencySatisfies emits false for out-of-range package', async function(assert) {
assert.expect(1);
this.owner.register('helper:my-assertion', helper(function([value]) {
assert.strictEqual(value, false);
}));
await render(hbs`{{my-assertion (macroDependencySatisfies "ember-cli" "0.0.1") }}`);
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { module, test } from 'qunit';
import funkyAddon from 'funky-sample-addon';
import FakeModule from 'fake-module'

const {
foo,
bar
} = FakeModule;

module("funky-sample-addon-interopt-test");

test("it works", function(assert) {
assert.ok(funkyAddon(), 'works correctly');
assert.ok(foo.isFoo, 'is the right export');
assert.ok(bar.isBar, 'is the right export');
});
Loading

0 comments on commit 86e06dd

Please sign in to comment.