From 35007281bd90d474f1483520822a03d0b3da2426 Mon Sep 17 00:00:00 2001 From: Kelly Selden Date: Tue, 13 Nov 2018 13:48:04 -0800 Subject: [PATCH] port ember-macro-test-helpers --- README.md | 25 ++++ addon-test-support/compute.js | 61 +++++++++ addon-test-support/expect-imports.js | 9 ++ .../files/tests/unit/macros/__name__-test.js | 2 +- blueprints/macro-test/index.js | 10 -- .../fixtures/macro-test/foo-bar-test.txt | 2 +- package-lock.json | 106 ---------------- package.json | 1 - tests/acceptance/top-level-imports-test.js | 2 +- tests/integration/compute-test.js | 117 ++++++++++++++++++ tests/integration/computed-test.js | 2 +- .../integration/create-class-computed-test.js | 2 +- tests/integration/curried-computed-test.js | 2 +- tests/integration/lazy-computed-test.js | 2 +- .../integration/lazy-curried-computed-test.js | 2 +- tests/integration/literal-test.js | 2 +- tests/integration/reads-test.js | 2 +- tests/unit/create-class-computed-test.js | 2 +- 18 files changed, 223 insertions(+), 128 deletions(-) create mode 100644 addon-test-support/compute.js create mode 100644 addon-test-support/expect-imports.js create mode 100644 tests/integration/compute-test.js diff --git a/README.md b/README.md index a41f532..dcac821 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ import { nameOfMacro } from 'ember-macro-helpers'; ### Contents - [API](#api) - [Custom macros](#custom-macros) + - [Test helpers](#test-helpers) ### API @@ -415,6 +416,30 @@ these files will get you started. More explanation is given in the [introduction video](https://youtu.be/kIDIa1NBZZI?t=18m40s). +### Test helpers +This comes with a `compute` helper. Here is a sample usage: + +```js +import myMacro from 'my-app/macros/my-macro'; +import compute from 'ember-macro-helpers/test-support/compute'; + +// ... + +test('it works', function(assert) { + compute({ + assert, + computed: myMacro('key1', 'key2'), + properties: { + key1: 1, + key2: 2 + }, + strictEqual: 3 + }); +}); +``` + +View all the possible ways to use [here](https://github.com/kellyselden/ember-macro-helpers/blob/master/tests/integration/compute-test.js). + Contributing ------------------------------------------------------------------------------ diff --git a/addon-test-support/compute.js b/addon-test-support/compute.js new file mode 100644 index 0000000..49dd0db --- /dev/null +++ b/addon-test-support/compute.js @@ -0,0 +1,61 @@ +import Component from '@ember/component'; +import { get, set, setProperties } from '@ember/object'; + +export default function({ + assert, + baseClass = Component, + computed, + properties, + strictEqual, + deepEqual, + assertion, + assertReadOnly +}) { + let MyComponent = baseClass.extend({ + computed + }); + let subject; + try { + subject = MyComponent.create({ + renderer: {} + }); + } catch (err) { + // this is for ember < 2.10 + // can remove once only support 2.12 + subject = MyComponent.create(); + } + + // compute initial value + // to test recomputes + get(subject, 'computed'); + + setProperties(subject, properties); + + let result = get(subject, 'computed'); + + function doAssertion(result) { + if (assertion) { + assert.ok(assertion(result)); + } else if (deepEqual) { + assert.deepEqual(result, deepEqual); + } else if (assertReadOnly) { + let func = () => set(subject, 'computed', 'assert read only'); + assert.throws(func, /Cannot set read-only property/); + } else if (assert) { + assert.strictEqual(result, strictEqual); + } + } + + let promise; + if (result && typeof result === 'object' && typeof result.then === 'function') { + promise = result.then(doAssertion); + } else { + doAssertion(result); + } + + return { + subject, + result, + promise + }; +} diff --git a/addon-test-support/expect-imports.js b/addon-test-support/expect-imports.js new file mode 100644 index 0000000..913cbbf --- /dev/null +++ b/addon-test-support/expect-imports.js @@ -0,0 +1,9 @@ +const exclude = [ + '__esModule', + 'default' +]; + +// helps prevent forgetting to test a new import +export default function(assert, obj) { + assert.expect(Object.getOwnPropertyNames(obj).filter(p => exclude.indexOf(p) === -1).length); +} diff --git a/blueprints/macro-test/files/tests/unit/macros/__name__-test.js b/blueprints/macro-test/files/tests/unit/macros/__name__-test.js index a58f4a5..9813500 100644 --- a/blueprints/macro-test/files/tests/unit/macros/__name__-test.js +++ b/blueprints/macro-test/files/tests/unit/macros/__name__-test.js @@ -1,6 +1,6 @@ import <%= camelizedModuleName %> from '<%= dasherizedModulePrefix %>/macros/<%= dasherizedModuleName %>'; import { module, test } from 'qunit'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; module('<%= friendlyTestName %>', function(hooks) { diff --git a/blueprints/macro-test/index.js b/blueprints/macro-test/index.js index 154966d..9a83036 100644 --- a/blueprints/macro-test/index.js +++ b/blueprints/macro-test/index.js @@ -11,15 +11,5 @@ module.exports = { friendlyTestName: testInfo.name(options.entity.name, 'Unit', 'Macro'), dasherizedModulePrefix: dasherize(options.project.config().modulePrefix) }; - }, - - afterInstall() { - // >= 2.11 - // return this.addAddonToProject('ember-macro-test-helpers'); - // https://github.com/ember-cli/ember-cli/issues/6318 - return this.addAddonsToProject({ - packages: ['ember-macro-test-helpers'], - blueprintOptions: { saveDev: true } - }); } }; diff --git a/node-tests/fixtures/macro-test/foo-bar-test.txt b/node-tests/fixtures/macro-test/foo-bar-test.txt index 4cb0fb0..c392101 100644 --- a/node-tests/fixtures/macro-test/foo-bar-test.txt +++ b/node-tests/fixtures/macro-test/foo-bar-test.txt @@ -1,6 +1,6 @@ import fooBar from 'my-app/macros/foo-bar'; import { module, test } from 'qunit'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; module('Unit | Macro | foo bar', function(hooks) { diff --git a/package-lock.json b/package-lock.json index 4337224..7e2a085 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5726,112 +5726,6 @@ "ember-cli-babel": "^7.0.0" } }, - "ember-macro-test-helpers": { - "version": "3.5.0", - "resolved": "https://registry.npmjs.org/ember-macro-test-helpers/-/ember-macro-test-helpers-3.5.0.tgz", - "integrity": "sha512-zyhU0MUwWzpvG0vqweLAFZYg2Jjtt8a6XeG7WhjaxaHKcgerj85jkdjEo05vg6cd9+Ktg2/4VaW/kHNIQk9Okg==", - "dev": true, - "requires": { - "broccoli-funnel": "^2.0.1", - "ember-cli-babel": "^6.6.0" - }, - "dependencies": { - "amd-name-resolver": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/amd-name-resolver/-/amd-name-resolver-1.2.0.tgz", - "integrity": "sha512-hlSTWGS1t6/xq5YCed7YALg7tKZL3rkl7UwEZ/eCIkn8JxmM6fU6Qs/1hwtjQqfuYxlffuUcgYEm0f5xP4YKaA==", - "dev": true, - "requires": { - "ensure-posix-path": "^1.0.1" - } - }, - "babel-plugin-debug-macros": { - "version": "0.2.0-beta.6", - "resolved": "https://registry.npmjs.org/babel-plugin-debug-macros/-/babel-plugin-debug-macros-0.2.0-beta.6.tgz", - "integrity": "sha1-7N9uQI1chjqyF0DXrX9D8CfS+RI=", - "dev": true, - "requires": { - "semver": "^5.3.0" - } - }, - "broccoli-babel-transpiler": { - "version": "6.5.0", - "resolved": "https://registry.npmjs.org/broccoli-babel-transpiler/-/broccoli-babel-transpiler-6.5.0.tgz", - "integrity": "sha512-c5OLGY40Sdmv6rP230Jt8yoK49BHfOw1LXiDMu9EC9k2U6sqlpNRK78SzvByQ8IzKtBYUfeWCxeZHcvW+gH7VQ==", - "dev": true, - "requires": { - "babel-core": "^6.26.0", - "broccoli-funnel": "^2.0.1", - "broccoli-merge-trees": "^2.0.0", - "broccoli-persistent-filter": "^1.4.3", - "clone": "^2.0.0", - "hash-for-dep": "^1.2.3", - "heimdalljs-logger": "^0.1.7", - "json-stable-stringify": "^1.0.0", - "rsvp": "^4.8.2", - "workerpool": "^2.3.0" - } - }, - "broccoli-funnel": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/broccoli-funnel/-/broccoli-funnel-2.0.1.tgz", - "integrity": "sha512-C8Lnp9TVsSSiZMGEF16C0dCiNg2oJqUKwuZ1K4kVC6qRPG/2Cj/rtB5kRCC9qEbwqhX71bDbfHROx0L3J7zXQg==", - "dev": true, - "requires": { - "array-equal": "^1.0.0", - "blank-object": "^1.0.1", - "broccoli-plugin": "^1.3.0", - "debug": "^2.2.0", - "fast-ordered-set": "^1.0.0", - "fs-tree-diff": "^0.5.3", - "heimdalljs": "^0.2.0", - "minimatch": "^3.0.0", - "mkdirp": "^0.5.0", - "path-posix": "^1.0.0", - "rimraf": "^2.4.3", - "symlink-or-copy": "^1.0.0", - "walk-sync": "^0.3.1" - } - }, - "broccoli-merge-trees": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/broccoli-merge-trees/-/broccoli-merge-trees-2.0.1.tgz", - "integrity": "sha512-WjaexJ+I8BxP5V5RNn6um/qDRSmKoiBC/QkRi79FT9ClHfldxRyCDs9mcV7mmoaPlsshmmPaUz5jdtcKA6DClQ==", - "dev": true, - "requires": { - "broccoli-plugin": "^1.3.0", - "merge-trees": "^1.0.1" - } - }, - "ember-cli-babel": { - "version": "6.17.1", - "resolved": "https://registry.npmjs.org/ember-cli-babel/-/ember-cli-babel-6.17.1.tgz", - "integrity": "sha512-VRblEFfOmxYDAznsz3JreErf1avKXL+dIPjhd0utyb6o1lGL4hUoOkp+h0xK3kr0/u9SdnvOxE1cEUHhxzpdoQ==", - "dev": true, - "requires": { - "amd-name-resolver": "1.2.0", - "babel-plugin-debug-macros": "^0.2.0-beta.6", - "babel-plugin-ember-modules-api-polyfill": "^2.4.0", - "babel-plugin-transform-es2015-modules-amd": "^6.24.0", - "babel-polyfill": "^6.26.0", - "babel-preset-env": "^1.7.0", - "broccoli-babel-transpiler": "^6.5.0", - "broccoli-debug": "^0.6.4", - "broccoli-funnel": "^2.0.0", - "broccoli-source": "^1.1.0", - "clone": "^2.0.0", - "ember-cli-version-checker": "^2.1.2", - "semver": "^5.5.0" - } - }, - "rsvp": { - "version": "4.8.4", - "resolved": "https://registry.npmjs.org/rsvp/-/rsvp-4.8.4.tgz", - "integrity": "sha512-6FomvYPfs+Jy9TfXmBpBuMWNH94SgCsZmJKcanySzgNNP6LjWxBvyLTa9KaMfDDM5oxRfrKDB0r/qeRsLwnBfA==", - "dev": true - } - } - }, "ember-maybe-import-regenerator": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/ember-maybe-import-regenerator/-/ember-maybe-import-regenerator-0.1.6.tgz", diff --git a/package.json b/package.json index aba5a20..ee0af25 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "ember-data": "^3.1.1", "ember-disable-prototype-extensions": "^1.1.3", "ember-load-initializers": "^2.0.0", - "ember-macro-test-helpers": "^3.5.0", "ember-maybe-import-regenerator": "^0.1.6", "ember-qunit": "^3.4.1", "ember-resolver": "^5.0.1", diff --git a/tests/acceptance/top-level-imports-test.js b/tests/acceptance/top-level-imports-test.js index 177bb83..91fbf6a 100644 --- a/tests/acceptance/top-level-imports-test.js +++ b/tests/acceptance/top-level-imports-test.js @@ -1,5 +1,5 @@ import { module, test } from 'qunit'; -import expect from 'ember-macro-test-helpers/expect-imports'; +import expect from 'ember-macro-helpers/test-support/expect-imports'; import helpers, { computed, createClassComputed, diff --git a/tests/integration/compute-test.js b/tests/integration/compute-test.js new file mode 100644 index 0000000..875e2e5 --- /dev/null +++ b/tests/integration/compute-test.js @@ -0,0 +1,117 @@ +import EmberObject from '@ember/object'; +import { readOnly } from '@ember/object/computed'; +import { resolve } from 'rsvp'; +import { compute } from 'ember-macro-helpers/test-support/compute'; +import { module, test } from 'qunit'; + +module('Integration | compute', function() { + module('verify API', function() { + test('it works without properties', function(assert) { + compute({ + assert, + computed: readOnly('key'), + strictEqual: undefined + }); + }); + + test('it accepts a different base class', function(assert) { + compute({ + assert, + baseClass: EmberObject.extend({ + foo: 'bar' + }), + computed: readOnly('foo'), + strictEqual: 'bar' + }); + }); + + test('it uses properties to calculate value', function(assert) { + compute({ + assert, + computed: readOnly('key'), + properties: { + key: 'test value' + }, + strictEqual: 'test value' + }); + }); + + test('it will deep equal', function(assert) { + compute({ + assert, + computed: readOnly('key'), + properties: { + key: ['test value'] + }, + deepEqual: ['test value'] + }); + }); + + test('it will allow you to calculate the assertion', function(assert) { + compute({ + assert, + computed: readOnly('key'), + properties: { + key: ['test value'] + }, + assertion([value]) { + return value === 'test value'; + } + }); + }); + + test('it can assert readOnly', function(assert) { + compute({ + assert, + computed: readOnly('key'), + assertReadOnly: true + }); + }); + + test('it is promise-aware', function(assert) { + return compute({ + assert, + computed: readOnly('key'), + properties: { + key: resolve('test value') + }, + strictEqual: 'test value' + }).promise; + }); + + test('it returns result', function(assert) { + let { result } = compute({ + computed: readOnly('key'), + properties: { + key: 'test value' + } + }); + + assert.strictEqual(result, 'test value'); + }); + + test('it returns subject', function(assert) { + let { subject } = compute({ + computed: readOnly('key'), + properties: { + key: 'test value' + } + }); + + assert.strictEqual(subject.get('computed'), 'test value'); + }); + }); + + module('edge cases', function() { + test('it handles special case null', function(assert) { + compute({ + assert, + computed: readOnly('key'), + properties: { + key: null + }, + strictEqual: null + }); + }); + }); +}); diff --git a/tests/integration/computed-test.js b/tests/integration/computed-test.js index 2c4691a..f31e699 100644 --- a/tests/integration/computed-test.js +++ b/tests/integration/computed-test.js @@ -2,7 +2,7 @@ import computed from 'ember-macro-helpers/computed'; import computedUnsafe from 'ember-macro-helpers/computed-unsafe'; import { module } from 'qunit'; import sinon from 'sinon'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; import namedTest from '../helpers/named-test'; import ArrayProxy from '@ember/array/proxy'; import { A as emberA } from '@ember/array'; diff --git a/tests/integration/create-class-computed-test.js b/tests/integration/create-class-computed-test.js index c470bd7..860fd8d 100644 --- a/tests/integration/create-class-computed-test.js +++ b/tests/integration/create-class-computed-test.js @@ -6,7 +6,7 @@ import raw from 'ember-macro-helpers/raw'; import { module, test } from 'qunit'; import EmberObject from '@ember/object'; import { A as emberA } from '@ember/array'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; import destroy from '../helpers/destroy'; const { WeakMap } = Ember; diff --git a/tests/integration/curried-computed-test.js b/tests/integration/curried-computed-test.js index 9dd9c48..9ac822f 100644 --- a/tests/integration/curried-computed-test.js +++ b/tests/integration/curried-computed-test.js @@ -1,6 +1,6 @@ import curriedComputed from 'ember-macro-helpers/curried-computed'; import { module, test } from 'qunit'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; let computed; diff --git a/tests/integration/lazy-computed-test.js b/tests/integration/lazy-computed-test.js index f10e946..0589b18 100644 --- a/tests/integration/lazy-computed-test.js +++ b/tests/integration/lazy-computed-test.js @@ -3,7 +3,7 @@ import computed from 'ember-macro-helpers/computed'; import getValue from 'ember-macro-helpers/get-value'; import { module, test } from 'qunit'; import sinon from 'sinon'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; const getReturnValue = 'get return value test'; const setReturnValue = 'set return value test'; diff --git a/tests/integration/lazy-curried-computed-test.js b/tests/integration/lazy-curried-computed-test.js index 8119207..2580f66 100644 --- a/tests/integration/lazy-curried-computed-test.js +++ b/tests/integration/lazy-curried-computed-test.js @@ -1,6 +1,6 @@ import lazyCurriedComputed from 'ember-macro-helpers/lazy-curried-computed'; import { module, test } from 'qunit'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; let computed; diff --git a/tests/integration/literal-test.js b/tests/integration/literal-test.js index dcb3b2f..f6ac95b 100644 --- a/tests/integration/literal-test.js +++ b/tests/integration/literal-test.js @@ -1,6 +1,6 @@ import literal from 'ember-macro-helpers/literal'; import { module, test } from 'qunit'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; module('Integration | literal', function() { test('it returns value verbatim', function(assert) { diff --git a/tests/integration/reads-test.js b/tests/integration/reads-test.js index 845e6fe..d05691c 100644 --- a/tests/integration/reads-test.js +++ b/tests/integration/reads-test.js @@ -3,7 +3,7 @@ import raw from 'ember-macro-helpers/raw'; import computed from 'ember-macro-helpers/computed'; import { module, test } from 'qunit'; import sinon from 'sinon'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; const getReturnValue = 'get return value test'; const setReturnValue = 'set return value test'; diff --git a/tests/unit/create-class-computed-test.js b/tests/unit/create-class-computed-test.js index d25ec31..c0f22f5 100644 --- a/tests/unit/create-class-computed-test.js +++ b/tests/unit/create-class-computed-test.js @@ -4,7 +4,7 @@ import { module, test } from 'qunit'; import EmberObject from '@ember/object'; import { A as emberA } from '@ember/array'; import sinon from 'sinon'; -import { compute } from 'ember-macro-test-helpers'; +import { compute } from 'ember-macro-helpers/test-support/compute'; import destroy from '../helpers/destroy'; let callback;