Skip to content

Commit 1218f89

Browse files
committed
migrate setupModelTest to setupTest
1 parent de3e836 commit 1218f89

3 files changed

Lines changed: 46 additions & 18 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
import { setupModelTest } from 'ember-mocha';
4+
5+
describe('Unit | Model | rental', function() {
6+
setupModelTest('rental', {
7+
needs: []
8+
});
9+
// Replace this with your real tests.
10+
it('exists', function() {
11+
let model = this.subject();
12+
expect(model).to.be.ok;
13+
});
14+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
import { setupTest } from 'ember-mocha';
4+
5+
describe('Unit | Model | rental', function() {
6+
setupTest();
7+
// Replace this with your real tests.
8+
it('exists', function() {
9+
let model = this.owner.lookup('service:store').createRecord('rental', {});
10+
expect(model).to.be.ok;
11+
});
12+
});

fourteen-testing-api.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = function(file, api) {
44
const j = api.jscodeshift;
55
const root = j(file.source);
66

7-
const SETUP_TYPE_METHODS = ["setupComponentTest"];
7+
const SETUP_TYPE_METHODS = ["setupComponentTest", "setupModelTest"];
88

99
function isSetupTypeMethod(nodePath) {
1010
return SETUP_TYPE_METHODS.some(name => {
@@ -193,6 +193,9 @@ module.exports = function(file, api) {
193193
this.setupType = "setupTest";
194194
this.subjectContainerKey = j.literal(`component:${node.expression.arguments[0].value}`);
195195
}
196+
} else if (calleeName === 'setupModelTest') {
197+
this.setupType = 'setupTest';
198+
this.modelName = node.expression.arguments[0].value;
196199
}
197200

198201
this.setupTypeMethodInvocationNode = node.expression;
@@ -459,9 +462,16 @@ module.exports = function(file, api) {
459462

460463
thisDotSubjectUsage.forEach(p => {
461464
let options = p.node.arguments[0];
462-
let split = subject.value.split(':');
463-
let subjectType = split[0];
464-
let subjectName = split[1];
465+
let subjectType;
466+
let subjectName;
467+
if ('modelName' in moduleInfo) {
468+
subjectType = 'model';
469+
subjectName = moduleInfo.modelName;
470+
} else {
471+
let split = subject.value.split(':');
472+
subjectType = split[0];
473+
subjectName = split[1];
474+
}
465475
let isSingletonSubject = ['model', 'component'].indexOf(subjectType) === -1;
466476

467477
// if we don't have `options` and the type is a singleton type
@@ -477,15 +487,7 @@ module.exports = function(file, api) {
477487
)
478488
);
479489
} else if (subjectType === 'model') {
480-
ensureImportWithSpecifiers({
481-
source: '@ember/runloop',
482-
specifiers: ['run'],
483-
});
484-
485490
p.replace(
486-
j.callExpression(j.identifier('run'), [
487-
j.arrowFunctionExpression(
488-
[],
489491
j.callExpression(
490492
j.memberExpression(
491493
j.callExpression(
@@ -497,11 +499,10 @@ module.exports = function(file, api) {
497499
),
498500
j.identifier('createRecord')
499501
),
500-
[j.literal(subjectName), options].filter(Boolean)
501-
),
502-
true
503-
),
504-
])
502+
// creating an empty object expression {} as the 2nd argument here
503+
// because setupModelTests shouldn't need store dependencies
504+
[j.literal(subjectName), j.objectExpression([])].filter(Boolean)
505+
)
505506
);
506507
} else {
507508
p.replace(
@@ -631,7 +632,8 @@ module.exports = function(file, api) {
631632

632633
function updateToNewEmberMochaImports() {
633634
let mapping = {
634-
setupComponentTest: "setupRenderingTest"
635+
setupComponentTest: "setupRenderingTest",
636+
setupModelTest: "setupTest"
635637
};
636638

637639
let emberMochaImports = root.find(j.ImportDeclaration, { source: { value: "ember-mocha" } });

0 commit comments

Comments
 (0)