Skip to content

Commit 07a1866

Browse files
author
Zhan Wang
committed
add transform to replace deprecated this.render() with render() from '@ember/test-helpers' package
1 parent 9b72eea commit 07a1866

7 files changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# this-render-migration
2+
This codemod transform is to replace deprecated this.render() with render() from '@ember/test-helpers' package
3+
4+
## Usage
5+
6+
```
7+
npx ember-test-helpers-codemod this-render-migration path/of/files/ or/some**/*glob.js
8+
9+
# or
10+
11+
yarn global add ember-test-helpers-codemod
12+
ember-test-helpers-codemod this-render-migration path/of/files/ or/some**/*glob.js
13+
```
14+
15+
## Input / Output
16+
17+
<!--FIXTURES_TOC_START-->
18+
* [basic](#basic)
19+
* [has-no-ember-test-helpers-import](#has-no-ember-test-helpers-import)
20+
<!--FIXTURES_TOC_END-->
21+
22+
<!--FIXTURES_CONTENT_START-->
23+
---
24+
<a id="basic">**basic**</a>
25+
26+
**Input** (<small>[basic.input.js](transforms/this-render-migration/__testfixtures__/basic.input.js)</small>):
27+
```js
28+
import { click } from '@ember/test-helpers';
29+
30+
test('It handles switching selected option on click and fires onSelect event', async function(assert) {
31+
this.onSelectMock = this.sandbox.stub();
32+
await this.render(hbs`
33+
<Common::TimeCommitmentSelector @timeCommitmentOptions={{timeCommitmentOptionsMock}} @onSelect={{onSelectMock}}>
34+
</Common::TimeCommitmentSelector>
35+
`);
36+
})
37+
38+
```
39+
40+
**Output** (<small>[basic.input.js](transforms/this-render-migration/__testfixtures__/basic.output.js)</small>):
41+
```js
42+
import { click, render } from '@ember/test-helpers';
43+
44+
test('It handles switching selected option on click and fires onSelect event', async function(assert) {
45+
this.onSelectMock = this.sandbox.stub();
46+
await render(hbs`
47+
<Common::TimeCommitmentSelector @timeCommitmentOptions={{timeCommitmentOptionsMock}} @onSelect={{onSelectMock}}>
48+
</Common::TimeCommitmentSelector>
49+
`);
50+
})
51+
```
52+
---
53+
<a id="has-no-ember-test-helpers-import">**has-no-ember-test-helpers-import**</a>
54+
55+
**Input** (<small>[has-no-ember-test-helpers-import.input.js](transforms/this-render-migration/__testfixtures__/has-no-ember-test-helpers-import.input.js)</small>):
56+
```js
57+
test('It handles switching selected option on click and fires onSelect event', async function(assert) {
58+
this.onSelectMock = this.sandbox.stub();
59+
await this.render(hbs`
60+
<Common::TimeCommitmentSelector @timeCommitmentOptions={{timeCommitmentOptionsMock}} @onSelect={{onSelectMock}}>
61+
</Common::TimeCommitmentSelector>
62+
`);
63+
})
64+
65+
```
66+
67+
**Output** (<small>[has-no-ember-test-helpers-import.input.js](transforms/this-render-migration/__testfixtures__/has-no-ember-test-helpers-import.output.js)</small>):
68+
```js
69+
import { render } from '@ember/test-helpers';
70+
test('It handles switching selected option on click and fires onSelect event', async function(assert) {
71+
this.onSelectMock = this.sandbox.stub();
72+
await render(hbs`
73+
<Common::TimeCommitmentSelector @timeCommitmentOptions={{timeCommitmentOptionsMock}} @onSelect={{onSelectMock}}>
74+
</Common::TimeCommitmentSelector>
75+
`);
76+
})
77+
78+
```
79+
<!--FIXTURE_CONTENT_END-->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { click } from '@ember/test-helpers';
2+
3+
test('It handles switching selected option on click and fires onSelect event', async function(assert) {
4+
this.onSelectMock = this.sandbox.stub();
5+
await this.render(hbs`
6+
<Common::TimeCommitmentSelector @timeCommitmentOptions={{timeCommitmentOptionsMock}} @onSelect={{onSelectMock}}>
7+
</Common::TimeCommitmentSelector>
8+
`);
9+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { click, render } from '@ember/test-helpers';
2+
3+
test('It handles switching selected option on click and fires onSelect event', async function(assert) {
4+
this.onSelectMock = this.sandbox.stub();
5+
await render(hbs`
6+
<Common::TimeCommitmentSelector @timeCommitmentOptions={{timeCommitmentOptionsMock}} @onSelect={{onSelectMock}}>
7+
</Common::TimeCommitmentSelector>
8+
`);
9+
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test('It handles switching selected option on click and fires onSelect event', async function(assert) {
2+
this.onSelectMock = this.sandbox.stub();
3+
await this.render(hbs`
4+
<Common::TimeCommitmentSelector @timeCommitmentOptions={{timeCommitmentOptionsMock}} @onSelect={{onSelectMock}}>
5+
</Common::TimeCommitmentSelector>
6+
`);
7+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { render } from '@ember/test-helpers';
2+
test('It handles switching selected option on click and fires onSelect event', async function(assert) {
3+
this.onSelectMock = this.sandbox.stub();
4+
await render(hbs`
5+
<Common::TimeCommitmentSelector @timeCommitmentOptions={{timeCommitmentOptionsMock}} @onSelect={{onSelectMock}}>
6+
</Common::TimeCommitmentSelector>
7+
`);
8+
})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
const { getParser } = require('codemod-cli').jscodeshift;
4+
const { addImportStatement, writeImportStatements } = require('../utils');
5+
6+
module.exports = function transformer(file, api) {
7+
const j = getParser(api);
8+
const root = j(file.source);
9+
10+
/**
11+
* Replace deprecated this.render() with render() from '@ember/test-helpers' package
12+
*/
13+
function transform() {
14+
root.find(j.ExpressionStatement, {
15+
expression: {
16+
type: 'AwaitExpression',
17+
argument: {
18+
type: 'CallExpression',
19+
callee: {
20+
type: 'MemberExpression',
21+
object: {
22+
type: 'ThisExpression'
23+
},
24+
property: {
25+
type: 'Identifier',
26+
name: 'render'
27+
},
28+
},
29+
},
30+
},
31+
}).replaceWith(path => {
32+
let oldCallExpressionArguments = path.node.expression.argument.arguments;
33+
34+
return j.expressionStatement(
35+
j.awaitExpression(
36+
j.callExpression(
37+
j.identifier('render'),
38+
oldCallExpressionArguments
39+
)
40+
)
41+
)
42+
})
43+
44+
let newImports = ['render'];
45+
46+
addImportStatement(newImports);
47+
writeImportStatements(j, root);
48+
}
49+
50+
transform();
51+
52+
return root.toSource({
53+
quote: 'single',
54+
trailingComma: true,
55+
});
56+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const { runTransformTest } = require('codemod-cli');
4+
5+
runTransformTest({
6+
type: 'jscodeshift',
7+
name: 'this-render-migration',
8+
});

0 commit comments

Comments
 (0)