Skip to content

Commit ac41bfd

Browse files
authored
Merge pull request #22 from rwjblue/subject-in-before-each
Support `this.render` in `beforeEach`.
2 parents 398977a + e74bc9e commit ac41bfd

3 files changed

Lines changed: 63 additions & 13 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,27 @@ test('it happens', function() {
4646

4747
test('it happens over and over', function() {
4848
});
49+
50+
moduleForComponent('foo-bar', 'Integration | Component | FooBar', {
51+
integration: true,
52+
53+
beforeEach() {
54+
this.render(hbs`derp`);
55+
},
56+
});
57+
58+
test('can make assertion', function (assert) {
59+
assert.equal(this._element.textContent, 'derp');
60+
});
61+
62+
moduleForComponent('foo-bar', 'Integration | Component | FooBar', {
63+
integration: true,
64+
65+
foo() {
66+
this.render(hbs`derp`);
67+
},
68+
});
69+
70+
test('can use render in custom method', function (assert) {
71+
assert.equal(this._element.textContent, 'derp');
72+
});

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,29 @@ module('Unit | Component | FooBar', function(hooks) {
4747
test('it happens over and over', function() {
4848
});
4949
});
50+
51+
module('Integration | Component | FooBar', function(hooks) {
52+
setupRenderingTest(hooks);
53+
54+
hooks.beforeEach(async function() {
55+
await render(hbs`derp`);
56+
});
57+
58+
test('can make assertion', function (assert) {
59+
assert.equal(this.element.textContent, 'derp');
60+
});
61+
});
62+
63+
module('Integration | Component | FooBar', function(hooks) {
64+
setupRenderingTest(hooks);
65+
66+
hooks.beforeEach(function() {
67+
this.foo = async function() {
68+
await render(hbs`derp`);
69+
};
70+
});
71+
72+
test('can use render in custom method', function (assert) {
73+
assert.equal(this.element.textContent, 'derp');
74+
});
75+
});

ember-qunit-codemod.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ module.exports = function(file, api, options) {
176176
);
177177

178178
if (options) {
179-
let customMethodBeforeEachBody;
179+
let customMethodBeforeEachBody, customMethodBeforeEachExpression;
180180

181181
options.properties.forEach(property => {
182182
if (isLifecycleHook(property)) {
@@ -188,13 +188,13 @@ module.exports = function(file, api, options) {
188188

189189
// preserve any comments that were present
190190
lifecycleStatement.comments = property.comments;
191+
processExpressionForRenderingTest(lifecycleStatement);
191192

192193
callback.body.body.push(lifecycleStatement);
193194
} else if (isMethod(property)) {
194195
if (!customMethodBeforeEachBody) {
195196
customMethodBeforeEachBody = j.blockStatement([]);
196-
197-
let beforeEachInvocation = j.expressionStatement(
197+
customMethodBeforeEachExpression = j.expressionStatement(
198198
j.callExpression(
199199
j.memberExpression(j.identifier('hooks'), j.identifier('beforeEach')),
200200
[
@@ -209,7 +209,7 @@ module.exports = function(file, api, options) {
209209
)
210210
);
211211

212-
callback.body.body.push(beforeEachInvocation);
212+
callback.body.body.push(customMethodBeforeEachExpression);
213213
}
214214

215215
let methodAssignment = j.expressionStatement(
@@ -226,19 +226,17 @@ module.exports = function(file, api, options) {
226226
customMethodBeforeEachBody.body.push(methodAssignment);
227227
}
228228
});
229+
230+
if (customMethodBeforeEachExpression) {
231+
processExpressionForRenderingTest(customMethodBeforeEachExpression);
232+
}
229233
}
230234

231235
return [moduleInvocation, callback.body.body, setupType, subject, hasCustomSubject];
232236
}
233237

234-
function processRenderingTest(testExpression) {
235-
let isTest = j.match(testExpression, { expression: { callee: { name: 'test' } } });
236-
if (!isTest) {
237-
return;
238-
}
239-
238+
function processExpressionForRenderingTest(testExpression) {
240239
// mark the test function as an async function
241-
testExpression.expression.arguments[1].async = true;
242240
let testExpressionCollection = j(testExpression);
243241

244242
// Transform to await render() or await clearRender()
@@ -250,6 +248,7 @@ module.exports = function(file, api, options) {
250248
j.callExpression(j.identifier(type), expression.node.arguments)
251249
);
252250
expression.replace(awaitExpression);
251+
p.scope.node.async = true;
253252
});
254253
});
255254

@@ -356,8 +355,9 @@ module.exports = function(file, api, options) {
356355
} else if (currentModuleCallbackBody) {
357356
currentModuleCallbackBody.push(expression);
358357

359-
if (currentTestType === 'setupRenderingTest') {
360-
processRenderingTest(expression);
358+
let isTest = j.match(expression, { expression: { callee: { name: 'test' } } });
359+
if (isTest && currentTestType === 'setupRenderingTest') {
360+
processExpressionForRenderingTest(expression);
361361
} else if (currentTestType === 'setupTest' && !currentHasCustomSubject) {
362362
processSubject(expression, currentSubject);
363363
}

0 commit comments

Comments
 (0)