Skip to content

Commit

Permalink
Transform tests to 'setup-test' syntax (#90)
Browse files Browse the repository at this point in the history
Transform tests to 'setup-test' syntax
  • Loading branch information
rwjblue committed Jan 24, 2020
2 parents d67de0e + 7502336 commit be46f96
Show file tree
Hide file tree
Showing 13 changed files with 511 additions and 497 deletions.
2 changes: 1 addition & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

module.exports = function(/* environment, appConfig */) {
return { };
};
}
5 changes: 4 additions & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');

module.exports = function(defaults) {
let app = new EmberAddon(defaults, {
// Add options here
'ember-cli-babel': {
// Needed because ember-maybe-import-regenerator uses Promise
includePolyfill: (EmberAddon.env() === 'test'),
},
});

/*
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"ember-disable-prototype-extensions": "^1.1.0",
"ember-export-application-global": "^2.0.0",
"ember-load-initializers": "^2.0.0",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-resolver": "^5.1.3",
"ember-source": "~2.14.1",
"loader.js": "^4.7.0",
Expand Down
45 changes: 21 additions & 24 deletions tests/acceptance/asset-load-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Ember from 'ember';
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

const { run } = Ember;
const { Promise } = Ember.RSVP;
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { visit, currentRouteName } from '@ember/test-helpers';
import { run } from '@ember/runloop';
import { Promise } from 'rsvp';

function waitFor(checkerFn, timeout = 1000) {
let start = Date.now();
Expand All @@ -25,22 +24,22 @@ function waitFor(checkerFn, timeout = 1000) {
});
}

moduleForAcceptance('Acceptance | asset-load');
module('Acceptance | asset-load', function(hooks) {
setupApplicationTest(hooks);

test('visiting a route which loads a bundle', function(assert) {
assert.expect(7);
test('visiting a route which loads a bundle', async function(assert) {
assert.expect(7);

const container = document.getElementById('ember-testing');
const originalContainerStyle = window.getComputedStyle(container);
const originalBackgroundColor = originalContainerStyle.backgroundColor;
const originalColor = originalContainerStyle.color;
const container = document.getElementById('ember-testing');
const originalContainerStyle = window.getComputedStyle(container);
const originalBackgroundColor = originalContainerStyle.backgroundColor;
const originalColor = originalContainerStyle.color;

const containerText = container.innerText;
assert.equal(containerText, '', 'test container is empty before load');
let containerText = container.innerText;
assert.equal(containerText, '', 'test container is empty before load');

visit('/');
await visit('/');

andThen(function() {
assert.equal(currentRouteName(), 'index', 'transitioned ');

const testScriptText = container.querySelector('h2').innerText;
Expand All @@ -49,7 +48,7 @@ test('visiting a route which loads a bundle', function(assert) {
const routeText = container.querySelector('h1').innerText;
assert.equal(routeText, 'Welcome!', 'route was loaded correctly');

const containerText = container.innerText;
containerText = container.innerText;
assert.ok(containerText.indexOf(testScriptText) < containerText.indexOf(routeText), 'test script was executed before route load');

return waitFor(() => {
Expand All @@ -66,16 +65,14 @@ test('visiting a route which loads a bundle', function(assert) {
assert.notEqual(containerStyle.color, originalColor, 'color is different after css load');
});
});
});

test('visiting a route which fails to load a script removes the node from DOM', function(assert) {
assert.expect(2);
test('visiting a route which fails to load a script removes the node from DOM', async function(assert) {
assert.expect(2);

const getScript = () => document.querySelector('script[src="foo.js"]');
const getScript = () => document.querySelector('script[src="foo.js"]');

visit('asset-error');
await visit('asset-error');

andThen(function() {
assert.equal(currentRouteName(), 'asset-error', 'transitioned ');

return waitFor(() => !getScript())
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = function(environment) {
ENV.APP.LOG_VIEW_LOOKUPS = false;

ENV.APP.rootElement = '#ember-testing';
ENV.APP.autoboot = false;
}

if (environment === 'production') {
Expand Down
11 changes: 5 additions & 6 deletions tests/test-helper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
import { start } from 'ember-cli-qunit';
import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';

setResolver(resolver);
setApplication(Application.create(config.APP));
start();
74 changes: 37 additions & 37 deletions tests/unit/asset-manifest-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,56 @@
import { module, test } from 'qunit';
import require from 'require';

module('Unit | asset-manifest', {
beforeEach() {
module('Unit | asset-manifest', function(hooks) {
hooks.beforeEach(function() {
resetModules();
this.originalNodeModule = requirejs.entries['dummy/config/node-asset-manifest'];
},
});

afterEach() {
hooks.afterEach(function() {
requirejs.entries['dummy/config/node-asset-manifest'] = this.originalNodeModule;
resetModules();
}
});
});

function resetModules() {
requirejs.unsee('dummy/config/node-asset-manifest');
requirejs.unsee('dummy/config/asset-manifest');
}
function resetModules() {
requirejs.unsee('dummy/config/node-asset-manifest');
requirejs.unsee('dummy/config/asset-manifest');
}

test('node-asset-manifest is generated properly', function(assert) {
const nodeManifest = require('dummy/config/node-asset-manifest').default;
delete requirejs.entries['dummy/config/node-asset-manifest'];
test('node-asset-manifest is generated properly', function(assert) {
const nodeManifest = require('dummy/config/node-asset-manifest').default;
delete requirejs.entries['dummy/config/node-asset-manifest'];

const manifest = require('dummy/config/asset-manifest').default;
const manifest = require('dummy/config/asset-manifest').default;

assert.notStrictEqual(nodeManifest, manifest);
assert.deepEqual(nodeManifest, manifest);
});
assert.notStrictEqual(nodeManifest, manifest);
assert.deepEqual(nodeManifest, manifest);
});

test('loads the node-asset-manifest if present', function(assert) {
const replacementModule = {};
define('dummy/config/node-asset-manifest', () => ({ default: replacementModule}));
test('loads the node-asset-manifest if present', function(assert) {
const replacementModule = {};
define('dummy/config/node-asset-manifest', () => ({ default: replacementModule}));

assert.strictEqual(require('dummy/config/asset-manifest').default, replacementModule);
});
assert.strictEqual(require('dummy/config/asset-manifest').default, replacementModule);
});

test('loads the manifest from the meta tag if available', function(assert) {
delete requirejs.entries['dummy/config/node-asset-manifest'];
test('loads the manifest from the meta tag if available', function(assert) {
delete requirejs.entries['dummy/config/node-asset-manifest'];

const meta = document.querySelector('meta[name="dummy/config/asset-manifest"]');
const metaContent = meta.getAttribute('content');
meta.setAttribute('content', '{"derp":"herp"}');
assert.deepEqual(require('dummy/config/asset-manifest').default, { derp: 'herp' });
meta.setAttribute('content', metaContent);
});
const meta = document.querySelector('meta[name="dummy/config/asset-manifest"]');
const metaContent = meta.getAttribute('content');
meta.setAttribute('content', '{"derp":"herp"}');
assert.deepEqual(require('dummy/config/asset-manifest').default, { derp: 'herp' });
meta.setAttribute('content', metaContent);
});

test('throws an error if unable to load the manifest', function(assert) {
delete requirejs.entries['dummy/config/node-asset-manifest'];
test('throws an error if unable to load the manifest', function(assert) {
delete requirejs.entries['dummy/config/node-asset-manifest'];

const meta = document.querySelector('meta[name="dummy/config/asset-manifest"]');
const metaContent = meta.getAttribute('content');
meta.setAttribute('content', 'herp');
assert.throws(() => assert.deepEqual(require('dummy/config/asset-manifest').default, {}));
meta.setAttribute('content', metaContent);
const meta = document.querySelector('meta[name="dummy/config/asset-manifest"]');
const metaContent = meta.getAttribute('content');
meta.setAttribute('content', 'herp');
assert.throws(() => assert.deepEqual(require('dummy/config/asset-manifest').default, {}));
meta.setAttribute('content', metaContent);
});
});
54 changes: 29 additions & 25 deletions tests/unit/errors/asset-load-test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import AssetLoadError from 'ember-asset-loader/errors/asset-load';
import { module, test } from 'qunit';

module('Unit | Error | asset-load', {
asset: {
type: 'js',
uri: 'some-js-file.js'
},
loader: {
loadAsset(asset) {
return `Loaded ${asset.type} asset with uri "${asset.uri}".`;
}
},
originalError: new Error('some error')
});
module('Unit | Error | asset-load', function(hooks) {
hooks.beforeEach(function() {
this.asset = {
type: 'js',
uri: 'some-js-file.js'
};

test('constructor() - accepts an asset and the original error', function(assert) {
const error = new AssetLoadError(this.loader, this.asset, this.originalError);
assert.ok(error instanceof Error, 'AssetLoadError inherits Error');
assert.ok(error.stack, 'stack is preserved');
assert.strictEqual(error.asset, this.asset, 'asset is set');
});
this.loader = {
loadAsset(asset) {
return `Loaded ${asset.type} asset with uri "${asset.uri}".`;
}
};

test('toString() - has correct name and message', function(assert) {
const error = new AssetLoadError(this.loader, this.asset, this.originalError);
assert.equal(error.toString(), 'AssetLoadError: The js asset with uri "some-js-file.js" failed to load with the error: Error: some error.');
});
this.originalError = new Error('some error');
});

test('constructor() - accepts an asset and the original error', function(assert) {
const error = new AssetLoadError(this.loader, this.asset, this.originalError);
assert.ok(error instanceof Error, 'AssetLoadError inherits Error');
assert.ok(error.stack, 'stack is preserved');
assert.strictEqual(error.asset, this.asset, 'asset is set');
});

test('toString() - has correct name and message', function(assert) {
const error = new AssetLoadError(this.loader, this.asset, this.originalError);
assert.equal(error.toString(), 'AssetLoadError: The js asset with uri "some-js-file.js" failed to load with the error: Error: some error.');
});

test('retryLoad() - calls loadAsset on the loader', function(assert) {
const error = new AssetLoadError(this.loader, this.asset, this.originalError);
assert.equal(error.retryLoad(), 'Loaded js asset with uri "some-js-file.js".');
test('retryLoad() - calls loadAsset on the loader', function(assert) {
const error = new AssetLoadError(this.loader, this.asset, this.originalError);
assert.equal(error.retryLoad(), 'Loaded js asset with uri "some-js-file.js".');
});
});
54 changes: 29 additions & 25 deletions tests/unit/errors/bundle-load-test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import BundleLoadError from 'ember-asset-loader/errors/bundle-load';
import { module, test } from 'qunit';

module('Unit | Error | bundle-load', {
bundleName: 'herp-de-derp',
errors: [
new Error('derp error')
],
loader: {
loadBundle(bundleName) {
return `Loaded the bundle "${bundleName}".`;
}
}
});
module('Unit | Error | bundle-load', function(hooks) {
hooks.beforeEach(function() {
this.bundleName = 'herp-de-derp';

test('constructor() - accepts a bundleName and errors array', function(assert) {
const error = new BundleLoadError(this.loader, this.bundleName, this.errors);
assert.ok(error instanceof Error, 'BundleLoadError inherits Error');
assert.ok(error.stack, 'stack is preserved');
assert.equal(error.bundleName, this.bundleName, 'bundleName is set');
assert.strictEqual(error.errors, this.errors, 'errors is set');
});
this.errors = [
new Error('derp error')
];

test('toString() - has correct name and message', function(assert) {
const error = new BundleLoadError(this.loader, this.bundleName, this.errors);
assert.equal(error.toString(), 'BundleLoadError: The bundle "herp-de-derp" failed to load.');
});
this.loader = {
loadBundle(bundleName) {
return `Loaded the bundle "${bundleName}".`;
}
};
});

test('constructor() - accepts a bundleName and errors array', function(assert) {
const error = new BundleLoadError(this.loader, this.bundleName, this.errors);
assert.ok(error instanceof Error, 'BundleLoadError inherits Error');
assert.ok(error.stack, 'stack is preserved');
assert.equal(error.bundleName, this.bundleName, 'bundleName is set');
assert.strictEqual(error.errors, this.errors, 'errors is set');
});

test('toString() - has correct name and message', function(assert) {
const error = new BundleLoadError(this.loader, this.bundleName, this.errors);
assert.equal(error.toString(), 'BundleLoadError: The bundle "herp-de-derp" failed to load.');
});

test('retryLoad() - calls loadBundle on the loader', function(assert) {
const error = new BundleLoadError(this.loader, this.bundleName, this.errors);
assert.equal(error.retryLoad(), 'Loaded the bundle "herp-de-derp".');
test('retryLoad() - calls loadBundle on the loader', function(assert) {
const error = new BundleLoadError(this.loader, this.bundleName, this.errors);
assert.equal(error.retryLoad(), 'Loaded the bundle "herp-de-derp".');
});
});
36 changes: 18 additions & 18 deletions tests/unit/errors/load-test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import LoadError from 'ember-asset-loader/errors/load';
import { module, test } from 'qunit';

module('Unit | Error | load');
module('Unit | Error | load', function() {
test('constructor() - accepts a message and a loader', function(assert) {
const message = 'herp-de-derp';
const loader = {};
const error = new LoadError(message, loader);

test('constructor() - accepts a message and a loader', function(assert) {
const message = 'herp-de-derp';
const loader = {};
const error = new LoadError(message, loader);
assert.ok(error instanceof Error, 'LoadError inherits Error');
assert.ok(error.stack, 'stack is preserved');
assert.equal(error.message, message, 'message is set');
assert.strictEqual(error.loader, loader, 'loader is set');
});

assert.ok(error instanceof Error, 'LoadError inherits Error');
assert.ok(error.stack, 'stack is preserved');
assert.equal(error.message, message, 'message is set');
assert.strictEqual(error.loader, loader, 'loader is set');
});

test('toString() - has correct name and message', function(assert) {
const error = new LoadError('herp-de-derp');
assert.equal(error.toString(), 'LoadError: herp-de-derp');
});
test('toString() - has correct name and message', function(assert) {
const error = new LoadError('herp-de-derp');
assert.equal(error.toString(), 'LoadError: herp-de-derp');
});

test('retryLoad() - throws an error', function(assert) {
const error = new LoadError();
assert.throws(() => error.retryLoad(), /You must define a behavior for 'retryLoad' in a subclass./);
test('retryLoad() - throws an error', function(assert) {
const error = new LoadError();
assert.throws(() => error.retryLoad(), /You must define a behavior for 'retryLoad' in a subclass./);
});
});
Loading

0 comments on commit be46f96

Please sign in to comment.