Skip to content

Commit 1bb7669

Browse files
committed
Add optional transform for find() to this.element.querySelector()
1 parent 895947c commit 1bb7669

7 files changed

Lines changed: 48 additions & 8 deletions

File tree

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ This addon will perform the following transformations suitable for integration t
6060
If you want to run only selected transforms on your code, you can pick just the needed transform:
6161

6262
```bash
63-
jscodeshift -t ../ember-test-helpers-codemod/lib/transforms/integration/click.js tests/integration
63+
jscodeshift -t path/to/ember-test-helpers-codemod/lib/transforms/integration/click.js tests/integration
6464
```
6565

6666
### Acceptance tests
@@ -104,7 +104,24 @@ These transformations are available for tests based on `ember-native-dom-helpers
104104
If you want to run only selected transforms on your code, you can pick just the needed transform:
105105

106106
```bash
107-
jscodeshift -t ../ember-test-helpers-codemod/lib/transforms/native-dom/find.js tests/integration
107+
jscodeshift -t path/to/ember-test-helpers-codemod/lib/transforms/native-dom/find.js tests/integration
108108
```
109109

110+
### Replace find/findAll
110111

112+
By default this codemod will use the `find()` and `findAll()` helpers from `@ember/test-helpers` where required.
113+
If you want to use the native query functions `this.element.querySelector()` / `this.element.querySelectorAll()` instead,
114+
you can use the `find.js` transform after you have run the other transformations:
115+
116+
```bash
117+
jscodeshift -t path/to/ember-test-helpers-codemod/lib/transforms/find.js tests
118+
```
119+
120+
| Before | After | Transform |
121+
|----------------------|-----------------------------------------|----------------|
122+
| `find('.foo')` | `this.element.querySelector('.foo')` | `find.js` |
123+
| `findAll('.foo')` | `this.element.querySelectorAll('.foo')` | `find.js` |
124+
125+
Note that this will require all instances of `find`/`findAll` to have the correct `this` context, otherwise you will run
126+
into `Cannot read property 'querySelector' of undefined` exceptions, as `this.element` will not be defined. This can
127+
happen outside of the main `test` function, for example inside of custom test helper functions.

__testfixtures__/find.input.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { find, findAll } from '@ember/test-helpers';
2+
3+
test('it renders', function(assert) {
4+
assert.equal(find('.foo').textContent.trim(), 'bar');
5+
assert.equal(findAll('.bar').length, 2);
6+
});

__testfixtures__/find.output.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
test('it renders', function(assert) {
3+
assert.equal(this.element.querySelector('.foo').textContent.trim(), 'bar');
4+
assert.equal(this.element.querySelectorAll('.bar').length, 2);
5+
});

__tests__/find-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
const defineTest = require('jscodeshift/dist/testUtils').defineTest;
4+
5+
defineTest(__dirname, 'index', { type: 'find' }, 'find');

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const integrationPreset = require('./lib/presets/integration');
22
const acceptancePreset = require('./lib/presets/acceptance');
33
const nativeDomPreset = require('./lib/presets/native-dom');
4+
const findTransform = require('./lib/transforms/find');
45

56
module.exports = function(file, api, options) {
67
switch (options.type) {
8+
case 'find':
9+
return findTransform(file, api, options);
710
case 'native-dom':
811
return nativeDomPreset(file, api, options);
912
case 'acceptance':

lib/test-runner.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
const fs = require('fs');
44
const defineTest = require('jscodeshift/dist/testUtils').defineTest;
55

6-
76
module.exports = function testRunner(type) {
87
let fixturesPath = `${__dirname}/../__testfixtures__/${type}`;
98

lib/transforms/find.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ function transform(file, api) {
1212
let j = api.jscodeshift;
1313
let root = j(source);
1414

15-
let nativeDomImportStatement = root.find(j.ImportDeclaration, {
16-
source: { value: 'ember-native-dom-helpers' }
15+
let importStatement = root.find(j.ImportDeclaration, {
16+
source: { value: '@ember/test-helpers' }
1717
});
18-
if (nativeDomImportStatement.length === 0) {
18+
if (importStatement.length === 0) {
1919
return root.toSource({ quote: 'single' });
2020
}
2121

22-
let oldSpecifiers = nativeDomImportStatement.get('specifiers');
22+
let oldSpecifiers = importStatement.get('specifiers');
2323
let newSpecifiers = [];
2424
oldSpecifiers.each(({ node: specifier }) => {
2525
let importedName = specifier.imported.name;
@@ -46,7 +46,12 @@ function transform(file, api) {
4646
newSpecifiers.push(specifier);
4747
}
4848
});
49-
oldSpecifiers.replace(newSpecifiers);
49+
50+
if (newSpecifiers.length > 0) {
51+
oldSpecifiers.replace(newSpecifiers);
52+
} else {
53+
importStatement.remove();
54+
}
5055

5156
return root.toSource({ quote: 'single' });
5257
}

0 commit comments

Comments
 (0)