Skip to content

Commit

Permalink
port ember-macro-test-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Nov 14, 2018
1 parent 91b8fdb commit 3500728
Show file tree
Hide file tree
Showing 18 changed files with 223 additions and 128 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { nameOfMacro } from 'ember-macro-helpers';
### Contents
- [API](#api)
- [Custom macros](#custom-macros)
- [Test helpers](#test-helpers)

### API

Expand Down Expand Up @@ -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
------------------------------------------------------------------------------
Expand Down
61 changes: 61 additions & 0 deletions addon-test-support/compute.js
Original file line number Diff line number Diff line change
@@ -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
};
}
9 changes: 9 additions & 0 deletions addon-test-support/expect-imports.js
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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) {

Expand Down
10 changes: 0 additions & 10 deletions blueprints/macro-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
});
}
};
2 changes: 1 addition & 1 deletion node-tests/fixtures/macro-test/foo-bar-test.txt
Original file line number Diff line number Diff line change
@@ -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) {

Expand Down
106 changes: 0 additions & 106 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/top-level-imports-test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
117 changes: 117 additions & 0 deletions tests/integration/compute-test.js
Original file line number Diff line number Diff line change
@@ -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
});
});
});
});
2 changes: 1 addition & 1 deletion tests/integration/computed-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading

0 comments on commit 3500728

Please sign in to comment.