Skip to content

Commit 2a9602b

Browse files
authored
Merge pull request #5 from simonihmig/e-n-d-h
Add transforms for ember-native-dom-helpers
2 parents 0c1991e + aa1e221 commit 2a9602b

11 files changed

Lines changed: 314 additions & 5 deletions

File tree

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ npm install -g ember-test-helpers-codemod
2424
cd my-ember-app-or-addon
2525
ember-test-helpers-codemod --type=integration tests/integration
2626
ember-test-helpers-codemod --type=acceptance tests/acceptance
27+
ember-test-helpers-codemod --type=native-dom tests
2728
```
2829

2930
## Transformations
@@ -55,10 +56,10 @@ This addon will perform the following transformations suitable for integration t
5556
| `this.$('.foo').each((index, elem) => {...})` | `this.element.querySelectorAll('.foo').forEach((elem, index) => {...})` | `each.js` |
5657

5758

58-
If you want to run only selected transforms on your code, you can just the needed transform:
59+
If you want to run only selected transforms on your code, you can pick just the needed transform:
5960

6061
```bash
61-
jscodeshift -t ../ember-test-helpers-codemod/lib/transforms/click.js tests/integration
62+
jscodeshift -t ../ember-test-helpers-codemod/lib/transforms/integration/click.js tests/integration
6263
```
6364

6465
### Acceptance tests
@@ -84,10 +85,26 @@ These transformations are available for acceptance tests:
8485
| `find('.foo').html('foo')` | `this.element.querySelector('.foo').innerHTML = 'foo'` | `html.js` |
8586
| `find('.foo').each((index, elem) => {...})` | `this.element.querySelectorAll('.foo').forEach((elem, index) => {...})` | `each.js` |
8687

87-
If you want to run only selected transforms on your code, you can just the needed transform:
88+
If you want to run only selected transforms on your code, you can pick just the needed transform:
8889

8990
```bash
90-
jscodeshift -t ../ember-test-helpers-codemod/lib/transforms/click.js tests/integration
91+
jscodeshift -t ../ember-test-helpers-codemod/lib/transforms/acceptance/click.js tests/integration
92+
```
93+
94+
### ember-native-dom-helpers tests
95+
96+
These transformations are available tests based on `ember-native-dom-helpers`:
97+
98+
| Before | After | Transform |
99+
|---------------------------------------|-----------------------------------------------|----------------|
100+
| `find('.foo')` | `this.element.querySelector('.foo')` | `find.js` |
101+
| `findAll('.foo')` | `this.element.querySelectorAll('.foo')` | `find.js` |
102+
| ```import { click, find, findAll, fillIn, focus, blur, triggerEvent, keyEvent, waitFor, waitUntil } from 'ember-native-dom-helpers';``` | ```import { click, find, findAll, fillIn, focus, blur, triggerEvent, triggerKeyEvent, waitFor, waitUntil } from '@ember/test-helpers';``` | `migrate-imports.js` |
103+
104+
If you want to run only selected transforms on your code, you can pick just the needed transform:
105+
106+
```bash
107+
jscodeshift -t ../ember-test-helpers-codemod/lib/transforms/native-dom/find.js tests/integration
91108
```
92109

93110

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { currentURL, currentPath, currentRouteName, find, visit } from 'ember-native-dom-helpers';
2+
import { test } from 'qunit';
3+
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
4+
5+
moduleForAcceptance('click');
6+
7+
test('visiting /foo', async function(assert) {
8+
await visit('/foo');
9+
assert.equal(currentURL(), '/foo');
10+
assert.ok(find('.foo'));
11+
});
12+
13+
test('visiting /bar', async function(assert) {
14+
await visit('/bar');
15+
assert.equal(currentPath(), 'bar');
16+
});
17+
18+
test('visiting /bar', async function(assert) {
19+
await visit('/bar');
20+
assert.equal(currentRouteName(), 'bar.index');
21+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { currentURL, currentRouteName, visit } from '@ember/test-helpers';
2+
import { currentPath } from 'ember-native-dom-helpers';
3+
import { test } from 'qunit';
4+
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
5+
6+
moduleForAcceptance('click');
7+
8+
test('visiting /foo', async function(assert) {
9+
await visit('/foo');
10+
assert.equal(currentURL(), '/foo');
11+
assert.ok(this.element.querySelector('.foo'));
12+
});
13+
14+
test('visiting /bar', async function(assert) {
15+
await visit('/bar');
16+
assert.equal(currentPath(), 'bar');
17+
});
18+
19+
test('visiting /bar', async function(assert) {
20+
await visit('/bar');
21+
assert.equal(currentRouteName(), 'bar.index');
22+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { click, find, findAll, findWithAssert, fillIn, focus, blur, triggerEvent, keyEvent, scrollTo, selectFiles, waitFor, waitUntil } from 'ember-native-dom-helpers';
2+
import { moduleForComponent, test } from 'ember-qunit';
3+
import hbs from 'htmlbars-inline-precompile';
4+
5+
moduleForComponent('foo-bar', 'Integration | Component | foo bar', {
6+
integration: true
7+
});
8+
9+
test('it renders', async function(assert) {
10+
this.render(hbs`{{foo-bar}}`);
11+
12+
await click('.foo', {});
13+
assert.equal(find('.foo').id, 'foo');
14+
await fillIn('.foo input', 'bar');
15+
await blur('.foo input');
16+
assert.equal(find('.foo').textContent.trim(), 'foo');
17+
});
18+
19+
test('it renders again', function(assert) {
20+
this.render(hbs`{{foo-bar}}`);
21+
22+
let selector = '.foo input';
23+
assert.equal(findAll(selector).length, 1);
24+
assert.equal(find(selector).value, 'foo');
25+
assert.ok(find('.foo').classList.contains('selected'));
26+
});
27+
28+
test('and again', async function(assert) {
29+
this.render(hbs`{{foo-bar}}`);
30+
31+
await tap('foo');
32+
let el = findWithAssert('.foo input');
33+
34+
await fillIn(el, value);
35+
await triggerEvent('.foo input', 'change');
36+
await keyEvent('bar', 'keypress', 13, modifiers);
37+
38+
await focus('.foo input');
39+
await blur('.foo input');
40+
41+
assert.ok(findAll('.baz')[1].classList.contains('selected'));
42+
});
43+
44+
test('and yet again', async function(assert) {
45+
this.render(hbs`{{foo-bar}}`);
46+
47+
await scrollTo(document, 10, 20);
48+
await selectFiles('input[type=file]', [new Blob(['texters'], { type: 'plain/text' })]);
49+
await waitUntil(() => find('.foo.active'));
50+
await waitFor('.bar.selected');
51+
assert.ok(true);
52+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { click, fillIn, focus, blur, triggerEvent, triggerKeyEvent, waitFor, waitUntil } from '@ember/test-helpers';
2+
import { findWithAssert, scrollTo, selectFiles } from 'ember-native-dom-helpers';
3+
import { moduleForComponent, test } from 'ember-qunit';
4+
import hbs from 'htmlbars-inline-precompile';
5+
6+
moduleForComponent('foo-bar', 'Integration | Component | foo bar', {
7+
integration: true
8+
});
9+
10+
test('it renders', async function(assert) {
11+
this.render(hbs`{{foo-bar}}`);
12+
13+
await click('.foo', {});
14+
assert.equal(this.element.querySelector('.foo').id, 'foo');
15+
await fillIn('.foo input', 'bar');
16+
await blur('.foo input');
17+
assert.equal(this.element.querySelector('.foo').textContent.trim(), 'foo');
18+
});
19+
20+
test('it renders again', function(assert) {
21+
this.render(hbs`{{foo-bar}}`);
22+
23+
let selector = '.foo input';
24+
assert.equal(this.element.querySelectorAll(selector).length, 1);
25+
assert.equal(this.element.querySelector(selector).value, 'foo');
26+
assert.ok(this.element.querySelector('.foo').classList.contains('selected'));
27+
});
28+
29+
test('and again', async function(assert) {
30+
this.render(hbs`{{foo-bar}}`);
31+
32+
await tap('foo');
33+
let el = findWithAssert('.foo input');
34+
35+
await fillIn(el, value);
36+
await triggerEvent('.foo input', 'change');
37+
await triggerKeyEvent('bar', 'keypress', 13, modifiers);
38+
39+
await focus('.foo input');
40+
await blur('.foo input');
41+
42+
assert.ok(this.element.querySelectorAll('.baz')[1].classList.contains('selected'));
43+
});
44+
45+
test('and yet again', async function(assert) {
46+
this.render(hbs`{{foo-bar}}`);
47+
48+
await scrollTo(document, 10, 20);
49+
await selectFiles('input[type=file]', [new Blob(['texters'], { type: 'plain/text' })]);
50+
await waitUntil(() => this.element.querySelector('.foo.active'));
51+
await waitFor('.bar.selected');
52+
assert.ok(true);
53+
});

__tests__/native-dom-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 testRunner = require('../lib/test-runner');
4+
5+
testRunner('native-dom');

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const integrationPreset = require('./lib/presets/integration');
22
const acceptancePreset = require('./lib/presets/acceptance');
3+
const nativeDomPreset = require('./lib/presets/native-dom');
34

45
module.exports = function(file, api, options) {
56
switch (options.type) {
7+
case 'native-dom':
8+
return nativeDomPreset(file, api, options);
69
case 'acceptance':
710
return acceptancePreset(file, api, options);
811
case 'integration':

lib/presets/native-dom.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const preset = require('./-preset');
2+
3+
module.exports = preset('native-dom');

lib/test-runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ module.exports = function testRunner(type) {
1313
defineTest(__dirname, 'index', { type }, `${type}/${match[1]}`);
1414
}
1515
});
16-
}
16+
};

lib/transforms/native-dom/find.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { createQuerySelectorExpression, createQuerySelectorAllExpression } = require('../../utils');
2+
3+
/**
4+
* Transform find/findAll to this.element.querySelector/-All
5+
*
6+
* @param file
7+
* @param api
8+
* @returns {*|string}
9+
*/
10+
function transform(file, api) {
11+
let source = file.source;
12+
let j = api.jscodeshift;
13+
let root = j(source);
14+
15+
let nativeDomImportStatement = root.find(j.ImportDeclaration, {
16+
source: { value: 'ember-native-dom-helpers' }
17+
});
18+
if (nativeDomImportStatement.length === 0) {
19+
return root.toSource({ quote: 'single' });
20+
}
21+
22+
let oldSpecifiers = nativeDomImportStatement.get('specifiers');
23+
let newSpecifiers = [];
24+
oldSpecifiers.each(({ node: specifier }) => {
25+
let importedName = specifier.imported.name;
26+
switch (importedName) {
27+
case 'find':
28+
root
29+
.find(j.CallExpression, {
30+
callee: {
31+
name: 'find'
32+
}
33+
})
34+
.replaceWith(({ node }) => createQuerySelectorExpression(j, node.arguments));
35+
break;
36+
case 'findAll':
37+
root
38+
.find(j.CallExpression, {
39+
callee: {
40+
name: 'findAll'
41+
}
42+
})
43+
.replaceWith(({ node }) => createQuerySelectorAllExpression(j, node.arguments));
44+
break;
45+
default:
46+
newSpecifiers.push(specifier);
47+
}
48+
});
49+
oldSpecifiers.replace(newSpecifiers);
50+
51+
return root.toSource({ quote: 'single' });
52+
}
53+
54+
module.exports = transform;

0 commit comments

Comments
 (0)