Skip to content

Commit 2417f93

Browse files
authored
Merge pull request #44 from rwjblue/only-update-imported-wait
Modify `wait` rewriting to be more intelligent.
2 parents 712edcf + b367bce commit 2417f93

5 files changed

Lines changed: 77 additions & 42 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { test } from 'qunit';
2+
import moduleForAcceptance from '../helpers/module-for-acceptance';
3+
4+
moduleForAcceptance('something');
5+
6+
test('uses global helpers', function(assert) {
7+
visit('/something');
8+
9+
wait().then(() => assert.ok(true));
10+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { test } from 'qunit';
2+
import moduleForAcceptance from '../helpers/module-for-acceptance';
3+
4+
moduleForAcceptance('something');
5+
6+
test('uses global helpers', function(assert) {
7+
visit('/something');
8+
9+
wait().then(() => assert.ok(true));
10+
});

__testfixtures__/ember-qunit-codemod/module-for-component.input.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { moduleForComponent, test } from 'ember-qunit';
2+
import wait from 'ember-test-helpers/wait';
23
import hbs from 'htmlbars-inline-precompile';
34

45
moduleForComponent('foo-bar', 'Integration | Component | FooBar', {
@@ -68,5 +69,7 @@ moduleForComponent('foo-bar', 'Integration | Component | FooBar', {
6869
});
6970

7071
test('can use render in custom method', function (assert) {
71-
assert.equal(this._element.textContent, 'derp');
72+
return wait().then(() => {
73+
assert.equal(this._element.textContent, 'derp');
74+
});
7275
});

__testfixtures__/ember-qunit-codemod/module-for-component.output.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { module, test } from 'qunit';
22
import { setupRenderingTest, setupTest } from 'ember-qunit';
3-
import { clearRender, render } from 'ember-test-helpers';
3+
import { clearRender, render, settled } from 'ember-test-helpers';
44
import hbs from 'htmlbars-inline-precompile';
55

66
module('Integration | Component | FooBar', function(hooks) {
@@ -71,6 +71,8 @@ module('Integration | Component | FooBar', function(hooks) {
7171
});
7272

7373
test('can use render in custom method', function (assert) {
74-
assert.equal(this.element.textContent, 'derp');
74+
return settled().then(() => {
75+
assert.equal(this.element.textContent, 'derp');
76+
});
7577
});
7678
});

ember-qunit-codemod.js

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ module.exports = function(file, api, options) {
22
const j = api.jscodeshift;
33
const root = j(file.source);
44

5+
function ensureImportWithSpecifiers({ source, specifiers, anchor, positionMethod }) {
6+
let importStatement = ensureImport(source, anchor, positionMethod);
7+
let combinedSpecifiers = new Set(specifiers);
8+
9+
importStatement
10+
.find(j.ImportSpecifier)
11+
.forEach(i => combinedSpecifiers.add(i.node.imported.name))
12+
.remove();
13+
14+
importStatement.get('specifiers').replace(
15+
Array.from(combinedSpecifiers)
16+
.sort()
17+
.map(s => j.importSpecifier(j.identifier(s)))
18+
);
19+
}
20+
521
function ensureImport(source, anchor, method = 'insertAfter') {
622
let desiredImport = root.find(j.ImportDeclaration, { source: { value: source } });
723
if (desiredImport.size() > 0) {
@@ -41,14 +57,12 @@ module.exports = function(file, api, options) {
4157
return;
4258
}
4359

44-
let qunitImports = ensureImport('qunit', 'ember-qunit', 'insertBefore');
45-
qunitImports.find(j.ImportSpecifier).forEach(p => specifiers.add(p.node.imported.name));
46-
47-
qunitImports.get('specifiers').replace(
48-
Array.from(specifiers)
49-
.sort()
50-
.map(s => j.importSpecifier(j.identifier(s)))
51-
);
60+
ensureImportWithSpecifiers({
61+
source: 'qunit',
62+
anchor: 'ember-qunit',
63+
positionMethod: 'insertBefore',
64+
specifiers,
65+
});
5266
}
5367

5468
function updateToNewEmberQUnitImports() {
@@ -97,9 +111,6 @@ module.exports = function(file, api, options) {
97111

98112
function updateEmberTestHelperImports() {
99113
let specifiers = new Set();
100-
let emberTestHelpersImport = root.find(j.ImportDeclaration, {
101-
source: { value: 'ember-test-helpers' },
102-
});
103114

104115
['render', 'clearRender'].forEach(type => {
105116
let usages = findTestHelperUsageOf(root, type);
@@ -108,31 +119,12 @@ module.exports = function(file, api, options) {
108119
}
109120
});
110121

111-
root
112-
.find(j.ImportDeclaration, { source: { value: 'ember-test-helpers/wait' } })
113-
.forEach(p => {
114-
specifiers.add('settled');
115-
return p;
116-
})
117-
.remove();
118-
119122
if (specifiers.size > 0) {
120-
if (emberTestHelpersImport.size() > 0) {
121-
// collect existing imports
122-
emberTestHelpersImport
123-
.find(j.ImportSpecifier)
124-
.forEach(p => specifiers.add(p.node.imported.name))
125-
.remove();
126-
} else {
127-
// Add new `import from 'ember-test-helpers'` node
128-
emberTestHelpersImport = ensureImport('ember-test-helpers', 'ember-qunit');
129-
}
130-
131-
emberTestHelpersImport.get('specifiers').replace(
132-
Array.from(specifiers)
133-
.sort()
134-
.map(s => j.importSpecifier(j.identifier(s)))
135-
);
123+
ensureImportWithSpecifiers({
124+
source: 'ember-test-helpers',
125+
anchor: 'ember-qunit',
126+
specifiers,
127+
});
136128
}
137129
}
138130

@@ -548,10 +540,27 @@ module.exports = function(file, api, options) {
548540
.forEach(replacement);
549541
}
550542

551-
function updateWaitCalls() {
552-
root.find(j.CallExpression, { callee: { name: 'wait' } }).forEach(p => {
553-
p.node.callee.name = 'settled';
543+
function updateWaitUsage() {
544+
let waitImport = root.find(j.ImportDeclaration, {
545+
source: { value: 'ember-test-helpers/wait' },
554546
});
547+
548+
if (waitImport.size() > 0) {
549+
let importedName;
550+
551+
ensureImportWithSpecifiers({
552+
source: 'ember-test-helpers',
553+
anchor: 'ember-qunit',
554+
specifiers: ['settled'],
555+
});
556+
557+
waitImport.find(j.ImportDefaultSpecifier).forEach(p => (importedName = p.node.local.name));
558+
waitImport.remove();
559+
560+
root.find(j.CallExpression, { callee: { name: importedName } }).forEach(p => {
561+
p.node.callee.name = 'settled';
562+
});
563+
}
555564
}
556565

557566
const printOptions = options.printOptions || { quote: 'single' };
@@ -567,8 +576,9 @@ module.exports = function(file, api, options) {
567576
updateInjectCalls();
568577
} else {
569578
updateEmberTestHelperImports();
570-
updateWaitCalls();
571579
}
572580

581+
updateWaitUsage();
582+
573583
return root.toSource(printOptions);
574584
};

0 commit comments

Comments
 (0)