From 73c49769a247fa1b14c82f968eb485288af1da80 Mon Sep 17 00:00:00 2001 From: John Bryson Date: Tue, 4 Feb 2020 13:05:01 -0500 Subject: [PATCH 1/2] fourteen-testing-api - During model transform preserve the argument passed to this.subject --- .../setup-model-test.input2.js | 14 ++++++++++++++ .../setup-model-test.output2.js | 12 ++++++++++++ fourteen-testing-api.js | 5 ++++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 __testfixtures__/fourteen-testing-api/setup-model-test.input2.js create mode 100644 __testfixtures__/fourteen-testing-api/setup-model-test.output2.js diff --git a/__testfixtures__/fourteen-testing-api/setup-model-test.input2.js b/__testfixtures__/fourteen-testing-api/setup-model-test.input2.js new file mode 100644 index 0000000..aac070d --- /dev/null +++ b/__testfixtures__/fourteen-testing-api/setup-model-test.input2.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-test.output2.js b/__testfixtures__/fourteen-testing-api/setup-model-test.output2.js new file mode 100644 index 0000000..c8bf012 --- /dev/null +++ b/__testfixtures__/fourteen-testing-api/setup-model-test.output2.js @@ -0,0 +1,12 @@ +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 = 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..e03ad5f 100755 --- a/fourteen-testing-api.js +++ b/fourteen-testing-api.js @@ -502,6 +502,9 @@ module.exports = function(file, api) { ) ); } else if (subjectType === 'model') { + let createRecordArg = p.node.arguments[0] ? + p.node.arguments[0] : /* the argument provided to this.subject() */ + j.objectExpression([]) /* empty object expression {} */; p.replace( j.callExpression( j.memberExpression( @@ -516,7 +519,7 @@ module.exports = function(file, api) { ), // 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.literal(subjectName), createRecordArg].filter(Boolean) ) ); } else { From eda2ad0771a1149850ca128ebc70623500907779 Mon Sep 17 00:00:00 2001 From: John Bryson Date: Wed, 5 Feb 2020 00:03:25 -0500 Subject: [PATCH 2/2] Wrap a createModel with a parameter with a runLoop to avoid errors when testing --- ...t.input2.js => setup-model-test2.input.js} | 0 ...output2.js => setup-model-test2.output.js} | 4 +- fourteen-testing-api.js | 60 +++++++++++++------ 3 files changed, 46 insertions(+), 18 deletions(-) rename __testfixtures__/fourteen-testing-api/{setup-model-test.input2.js => setup-model-test2.input.js} (100%) rename __testfixtures__/fourteen-testing-api/{setup-model-test.output2.js => setup-model-test2.output.js} (65%) diff --git a/__testfixtures__/fourteen-testing-api/setup-model-test.input2.js b/__testfixtures__/fourteen-testing-api/setup-model-test2.input.js similarity index 100% rename from __testfixtures__/fourteen-testing-api/setup-model-test.input2.js rename to __testfixtures__/fourteen-testing-api/setup-model-test2.input.js diff --git a/__testfixtures__/fourteen-testing-api/setup-model-test.output2.js b/__testfixtures__/fourteen-testing-api/setup-model-test2.output.js similarity index 65% rename from __testfixtures__/fourteen-testing-api/setup-model-test.output2.js rename to __testfixtures__/fourteen-testing-api/setup-model-test2.output.js index c8bf012..cc848c7 100644 --- a/__testfixtures__/fourteen-testing-api/setup-model-test.output2.js +++ b/__testfixtures__/fourteen-testing-api/setup-model-test2.output.js @@ -6,7 +6,9 @@ describe('Unit | Model | rental', function() { setupTest(); // Replace this with your real tests. it('exists', function() { - let model = this.owner.lookup('service:store').createRecord('rental', {num: 5, obj: {key: 'val'}, arr: [1,'two']}); + 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 e03ad5f..0ef8fa9 100755 --- a/fourteen-testing-api.js +++ b/fourteen-testing-api.js @@ -502,26 +502,52 @@ module.exports = function(file, api) { ) ); } else if (subjectType === 'model') { - let createRecordArg = p.node.arguments[0] ? - p.node.arguments[0] : /* the argument provided to this.subject() */ - j.objectExpression([]) /* empty object expression {} */; - 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), createRecordArg].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(