Skip to content

Commit

Permalink
Merge pull request #14 from Turbo87/qunit
Browse files Browse the repository at this point in the history
Use new QUnit testing API
  • Loading branch information
GavinJoyce authored Nov 14, 2017
2 parents 0b57beb + de4addf commit ded4b30
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 108 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
"ember-cli-htmlbars": "^2.0.1",
"ember-cli-htmlbars-inline-precompile": "^1.0.0",
"ember-cli-inject-live-reload": "^1.4.1",
"ember-cli-qunit": "^4.0.0",
"ember-cli-qunit": "^4.1.1",
"ember-cli-shims": "^1.1.0",
"ember-cli-sri": "^2.1.0",
"ember-cli-uglify": "^2.0.0",
"ember-disable-prototype-extensions": "^1.1.2",
"ember-export-application-global": "^2.0.0",
"ember-load-initializers": "^1.0.0",
"ember-maybe-import-regenerator-for-testing": "^1.0.0",
"ember-resolver": "^4.0.0",
"ember-source": "~2.16.0",
"loader.js": "^4.2.3"
Expand Down
162 changes: 82 additions & 80 deletions tests/integration/did-change-attrs-test.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,112 @@
import Ember from 'ember';
import { test, moduleForComponent } from 'ember-qunit';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from 'ember-test-helpers';
import hbs from 'htmlbars-inline-precompile';
import DidChangeAttrs from 'ember-did-change-attrs';

function registerComponent(testSuite, hash, klass = Ember.Component) {
testSuite.register('component:x-changer', klass.extend(DidChangeAttrs, hash));
function registerComponent(owner, hash, klass = Ember.Component) {
owner.register('component:x-changer', klass.extend(DidChangeAttrs, hash));
}

moduleForComponent('x-changer', 'Integration | DidChangeAttrs', {
integration: true
});

test('Basic usage', function(assert) {
let changedAttrs, didChangeAttrsCallCount = 0;
module('Integration | DidChangeAttrs', function(hooks) {
setupRenderingTest(hooks);

registerComponent(this, {
didChangeAttrsConfig: {
attrs: ['email', 'isAdmin']
},
test('Basic usage', async function(assert) {
let changedAttrs, didChangeAttrsCallCount = 0;

didChangeAttrs(changes) {
this._super(...arguments);
registerComponent(this.owner, {
didChangeAttrsConfig: {
attrs: ['email', 'isAdmin']
},

didChangeAttrsCallCount++;
changedAttrs = changes;
}
});
didChangeAttrs(changes) {
this._super(...arguments);

this.set('name', 'Tomster');
this.set('email', '[email protected]');
this.set('isAdmin', false);
didChangeAttrsCallCount++;
changedAttrs = changes;
}
});

this.render(hbs`{{x-changer email=email isAdmin=isAdmin name=name}}`);
this.set('name', 'Tomster');
this.set('email', '[email protected]');
this.set('isAdmin', false);

assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called on initial render');
await render(hbs`{{x-changer email=email isAdmin=isAdmin name=name}}`);

this.set('email', '[email protected]');
assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is called when an attribute changed');
assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called on initial render');

assert.deepEqual(changedAttrs, {
email: {
previous: '[email protected]',
current: '[email protected]'
}
}, '`changedAttrs` contains the attribute changes');
this.set('email', '[email protected]');
assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is called when an attribute changed');

this.set('name', 'TheTomster');
assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is not called when an untracked attribute is changed');
});
assert.deepEqual(changedAttrs, {
email: {
previous: '[email protected]',
current: '[email protected]'
}
}, '`changedAttrs` contains the attribute changes');

test('Custom isEqual', function(assert) {
let changedAttrs, didChangeAttrsCallCount = 0;
this.set('name', 'TheTomster');
assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is not called when an untracked attribute is changed');
});

registerComponent(this, {
didChangeAttrsConfig: {
attrs: ['user', 'isAdmin'],
isEqual(key, a, b) {
if (key === 'user') {
return (a && b) ? a.id === b.id : a === b;
test('Custom isEqual', async function(assert) {
let changedAttrs, didChangeAttrsCallCount = 0;

registerComponent(this.owner, {
didChangeAttrsConfig: {
attrs: ['user', 'isAdmin'],
isEqual(key, a, b) {
if (key === 'user') {
return (a && b) ? a.id === b.id : a === b;
}
return a === b;
}
return a === b;
},

didChangeAttrs(changes) {
this._super(...arguments);

didChangeAttrsCallCount++;
changedAttrs = changes;
}
},
});

didChangeAttrs(changes) {
this._super(...arguments);
this.set('user', { name: 'Tomster', id: '123' });
this.set('isAdmin', false);

didChangeAttrsCallCount++;
changedAttrs = changes;
}
});
await render(hbs`{{x-changer user=user isAdmin=isAdmin}}`);

this.set('user', { name: 'Tomster', id: '123' });
this.set('isAdmin', false);
assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called on initial render');

this.render(hbs`{{x-changer user=user isAdmin=isAdmin}}`);
this.set('user', { name: 'TheTomster', id: '123' });

assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called on initial render');
assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called because user entities are equal');

this.set('user', { name: 'TheTomster', id: '123' });
this.set('user', { name: 'Zoey', id: '456' });

assert.equal(didChangeAttrsCallCount, 0, '`didChangeAttrs` is not called because user entities are equal');
assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is called because user entities are not equal');
assert.deepEqual(changedAttrs, {
user: {
previous: {
id: '123',
name: 'Tomster'
},
current: {
id: '456',
name: 'Zoey'
}
}
}, '`user` included in `changedAttrs` because `user.id` is different');

this.set('user', { name: 'Zoey', id: '456' });
this.set('isAdmin', true);

assert.equal(didChangeAttrsCallCount, 1, '`didChangeAttrs` is called because user entities are not equal');
assert.deepEqual(changedAttrs, {
user: {
previous: {
id: '123',
name: 'Tomster'
},
current: {
id: '456',
name: 'Zoey'
assert.equal(didChangeAttrsCallCount, 2, '`didChangeAttrs` is called because isAdmin changed');
assert.deepEqual(changedAttrs, {
isAdmin: {
previous: false,
current: true
}
}
}, '`user` included in `changedAttrs` because `user.id` is different');

this.set('isAdmin', true);

assert.equal(didChangeAttrsCallCount, 2, '`didChangeAttrs` is called because isAdmin changed');
assert.deepEqual(changedAttrs, {
isAdmin: {
previous: false,
current: true
}
}, '`isAdmin` included in `changedAttrs` because it changed');
}, '`isAdmin` included in `changedAttrs` because it changed');
});
});
86 changes: 59 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
# yarn lockfile v1


"@ember/test-helpers@^0.7.1":
version "0.7.1"
resolved "https://registry.yarnpkg.com/@ember/test-helpers/-/test-helpers-0.7.1.tgz#14ba828ebc5b7b0e6eb7889352cb40af0c995349"
dependencies:
broccoli-funnel "^2.0.1"
ember-cli-babel "^6.8.1"

"@glimmer/compiler@^0.25.4":
version "0.25.4"
resolved "https://registry.yarnpkg.com/@glimmer/compiler/-/compiler-0.25.4.tgz#349791f826de0be271fdfa88e3bac395dcb94ee9"
Expand Down Expand Up @@ -1115,7 +1122,7 @@ broccoli-funnel@^1.0.0, broccoli-funnel@^1.0.1, broccoli-funnel@^1.1.0, broccoli
symlink-or-copy "^1.0.0"
walk-sync "^0.3.1"

broccoli-funnel@^2.0.0:
broccoli-funnel@^2.0.0, broccoli-funnel@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/broccoli-funnel/-/broccoli-funnel-2.0.1.tgz#6823c73b675ef78fffa7ab800f083e768b51d449"
dependencies:
Expand Down Expand Up @@ -1525,6 +1532,12 @@ commander@^2.6.0, commander@~2.11.0:
version "2.11.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563"

common-tags@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.4.0.tgz#1187be4f3d4cf0c0427d43f74eef1f73501614c0"
dependencies:
babel-runtime "^6.18.0"

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/component-bind/-/component-bind-1.0.0.tgz#00c608ab7dcd93897c0009651b1d3a8e1e73bbd1"
Expand Down Expand Up @@ -1809,6 +1822,23 @@ ember-cli-babel@^6.0.0, ember-cli-babel@^6.0.0-beta.7, ember-cli-babel@^6.6.0, e
clone "^2.0.0"
ember-cli-version-checker "^2.0.0"

ember-cli-babel@^6.3.0:
version "6.9.0"
resolved "https://registry.yarnpkg.com/ember-cli-babel/-/ember-cli-babel-6.9.0.tgz#5147391389bdbb7091d15f81ae1dff1eb49d71aa"
dependencies:
amd-name-resolver "0.0.7"
babel-plugin-debug-macros "^0.1.11"
babel-plugin-ember-modules-api-polyfill "^2.0.1"
babel-plugin-transform-es2015-modules-amd "^6.24.0"
babel-polyfill "^6.16.0"
babel-preset-env "^1.5.1"
broccoli-babel-transpiler "^6.1.2"
broccoli-debug "^0.6.2"
broccoli-funnel "^1.0.0"
broccoli-source "^1.1.0"
clone "^2.0.0"
ember-cli-version-checker "^2.1.0"

ember-cli-broccoli-sane-watcher@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/ember-cli-broccoli-sane-watcher/-/ember-cli-broccoli-sane-watcher-2.0.4.tgz#f43f42f75b7509c212fb926cd9aea86ae19264c6"
Expand Down Expand Up @@ -1924,20 +1954,12 @@ ember-cli-preprocess-registry@^3.1.0:
process-relative-require "^1.0.0"
silent-error "^1.0.0"

ember-cli-qunit@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/ember-cli-qunit/-/ember-cli-qunit-4.0.1.tgz#905aa07620ae9fdb417c7e48d45bd2277b62f864"
ember-cli-qunit@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/ember-cli-qunit/-/ember-cli-qunit-4.1.1.tgz#307a157e9f36a0d32621ae247effb891ff951fc7"
dependencies:
broccoli-funnel "^2.0.0"
broccoli-merge-trees "^2.0.0"
ember-cli-babel "^6.8.1"
ember-cli-test-loader "^2.2.0"
ember-cli-version-checker "^2.0.0"
ember-qunit "^2.2.0"
qunit-notifications "^0.1.1"
qunitjs "^2.4.0"
resolve "^1.4.0"
silent-error "^1.1.0"
ember-qunit "^3.1.0"

ember-cli-shims@^1.1.0:
version "1.1.0"
Expand Down Expand Up @@ -2100,11 +2122,25 @@ ember-load-initializers@^1.0.0:
dependencies:
ember-cli-babel "^6.0.0-beta.7"

ember-qunit@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/ember-qunit/-/ember-qunit-2.2.0.tgz#3cdf400031c93a38de781a7304819738753b7f99"
ember-maybe-import-regenerator-for-testing@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ember-maybe-import-regenerator-for-testing/-/ember-maybe-import-regenerator-for-testing-1.0.0.tgz#894b8089c5b3067c920b492c81233603852d5c2f"
dependencies:
broccoli-funnel "^1.0.1"
ember-cli-babel "^6.6.0"
regenerator-runtime "^0.9.5"

ember-qunit@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/ember-qunit/-/ember-qunit-3.1.0.tgz#4995a6207ab66b5d0cf807d0459d48f55f9eee5f"
dependencies:
ember-test-helpers "^0.6.3"
"@ember/test-helpers" "^0.7.1"
broccoli-funnel "^2.0.1"
broccoli-merge-trees "^2.0.0"
common-tags "^1.4.0"
ember-cli-babel "^6.3.0"
ember-cli-test-loader "^2.2.0"
qunit "^2.4.1"

ember-resolver@^4.0.0:
version "4.5.0"
Expand Down Expand Up @@ -2155,10 +2191,6 @@ ember-source@~2.16.0:
simple-dom "^0.3.0"
simple-html-tokenizer "^0.4.1"

ember-test-helpers@^0.6.3:
version "0.6.3"
resolved "https://registry.yarnpkg.com/ember-test-helpers/-/ember-test-helpers-0.6.3.tgz#f864cdf6f4e75f3f8768d6537785b5ab6e82d907"

ember-try-config@^2.0.1:
version "2.1.0"
resolved "https://registry.yarnpkg.com/ember-try-config/-/ember-try-config-2.1.0.tgz#e0e156229a542346a58ee6f6ad605104c98edfe0"
Expand Down Expand Up @@ -4314,13 +4346,9 @@ quick-temp@^0.1.2, quick-temp@^0.1.3, quick-temp@^0.1.5, quick-temp@^0.1.8:
rimraf "^2.5.4"
underscore.string "~3.3.4"

qunit-notifications@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/qunit-notifications/-/qunit-notifications-0.1.1.tgz#3001afc6a6a77dfbd962ccbcddde12dec5286c09"

qunitjs@^2.4.0:
qunit@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/qunitjs/-/qunitjs-2.4.1.tgz#88aba055a9e2ec3dbebfaad02471b2cb002c530b"
resolved "https://registry.yarnpkg.com/qunit/-/qunit-2.4.1.tgz#373c826b3b91795f3e5479cc94f0f6fa14dedc47"
dependencies:
chokidar "1.6.1"
commander "2.9.0"
Expand Down Expand Up @@ -4423,6 +4451,10 @@ regenerator-runtime@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.11.0.tgz#7e54fe5b5ccd5d6624ea6255c3473be090b802e1"

regenerator-runtime@^0.9.5:
version "0.9.6"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.9.6.tgz#d33eb95d0d2001a4be39659707c51b0cb71ce029"

regenerator-transform@^0.10.0:
version "0.10.1"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.10.1.tgz#1e4996837231da8b7f3cf4114d71b5691a0680dd"
Expand Down

0 comments on commit ded4b30

Please sign in to comment.