Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions __testfixtures__/fourteen-testing-api/setup-model-test2.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupModelTest } from 'ember-mocha';

describe('Unit | Model | rental', function() {
setupModelTest('rental', {
needs: []
});
// Replace this with your real tests.
it('exists', function() {
let model = this.subject({num: 5, obj: {key: 'val'}, arr: [1,'two']});
expect(model).to.be.ok;
});
});
14 changes: 14 additions & 0 deletions __testfixtures__/fourteen-testing-api/setup-model-test2.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupTest } from 'ember-mocha';

describe('Unit | Model | rental', function() {
setupTest();
// Replace this with your real tests.
it('exists', function() {
let model = Ember.run(
() => this.owner.lookup('service:store').createRecord('rental', {num: 5, obj: {key: 'val'}, arr: [1,'two']})
);
expect(model).to.be.ok;
});
});
57 changes: 43 additions & 14 deletions fourteen-testing-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,23 +502,52 @@ module.exports = function(file, api) {
)
);
} else if (subjectType === 'model') {
p.replace(
j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(
j.memberExpression(j.thisExpression(), j.identifier('owner')),
j.identifier('lookup')
let createRecordArg = p.node.arguments[0], /* the argument provided to this.subject() */
emptyObjectExpression = j.objectExpression([]); /* empty object expression {} */

if( createRecordArg ){
// If there is an argument provided to this.subject, wrap it in a run loop
p.replace(
j.callExpression(
j.memberExpression(
j.identifier('Ember'), j.identifier('run')
), [j.arrowFunctionExpression(
[],
j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(
j.memberExpression(j.thisExpression(), j.identifier('owner')),
j.identifier('lookup')
),
[j.literal('service:store')]
),
[j.literal('service:store')]
j.identifier('createRecord')
),
j.identifier('createRecord')
[j.literal(subjectName), createRecordArg].filter(Boolean)
)
)]
)
);
} else {
p.replace(
j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(
j.memberExpression(j.thisExpression(), j.identifier('owner')),
j.identifier('lookup')
),
[j.literal('service:store')]
),
// creating an empty object expression {} as the 2nd argument here
// because setupModelTests shouldn't need store dependencies
[j.literal(subjectName), j.objectExpression([])].filter(Boolean)
)
);
j.identifier('createRecord')
),
// creating an empty object expression {} as the 2nd argument here
[j.literal(subjectName), emptyObjectExpression].filter(Boolean)
)
);
}

} else {
p.replace(
j.callExpression(
Expand Down