diff --git a/__testfixtures__/fourteen-testing-api/setup-model-test2.input.js b/__testfixtures__/fourteen-testing-api/setup-model-test2.input.js new file mode 100644 index 0000000..aac070d --- /dev/null +++ b/__testfixtures__/fourteen-testing-api/setup-model-test2.input.js @@ -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; + }); +}); diff --git a/__testfixtures__/fourteen-testing-api/setup-model-test2.output.js b/__testfixtures__/fourteen-testing-api/setup-model-test2.output.js new file mode 100644 index 0000000..cc848c7 --- /dev/null +++ b/__testfixtures__/fourteen-testing-api/setup-model-test2.output.js @@ -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; + }); +}); diff --git a/fourteen-testing-api.js b/fourteen-testing-api.js index a511cf3..0ef8fa9 100755 --- a/fourteen-testing-api.js +++ b/fourteen-testing-api.js @@ -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(