Skip to content

Commit 00c8315

Browse files
authored
Merge pull request #43 from zhanwang626/this-render-migration
add transform to replace deprecated this.render()
2 parents 8a5bc2c + 75751c3 commit 00c8315

7 files changed

Lines changed: 166 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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.CallExpression, {
15+
callee: {
16+
type: 'MemberExpression',
17+
object: {
18+
type: 'ThisExpression'
19+
},
20+
property: {
21+
type: 'Identifier',
22+
name: 'render'
23+
},
24+
},
25+
}).replaceWith(path => {
26+
let oldCallExpressionArguments = path.node.arguments;
27+
28+
return j.callExpression(
29+
j.identifier('render'),
30+
oldCallExpressionArguments
31+
)
32+
})
33+
34+
let newImports = ['render'];
35+
36+
addImportStatement(newImports);
37+
writeImportStatements(j, root);
38+
}
39+
40+
transform();
41+
42+
return root.toSource({
43+
quote: 'single',
44+
trailingComma: true,
45+
});
46+
}
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)