Skip to content

Commit 947e5df

Browse files
committed
migrate native dom helpers with context argument
1 parent 9865532 commit 947e5df

3 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { find, findAll, visit, click, fillIn } 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+
10+
const foo = find('.foo');
11+
assert.equal(find('.bar', foo).textContent.trim(), 'bar');
12+
assert.equal(find('.bar', find('.foo')).textContent.trim(), 'bar');
13+
assert.equal(find('.bar', '.foo').textContent.trim(), 'bar');
14+
assert.equal(findAll('.bar', foo).length, 2);
15+
assert.equal(findAll('.bar', find('.foo')).length, 2);
16+
assert.equal(findAll('.bar', '.foo').length, 2);
17+
18+
await click('button', foo);
19+
await click('button', { shiftKey: true });
20+
await click('button', foo, { shiftKey: true });
21+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { find, findAll, visit, click, fillIn } from '@ember/test-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+
10+
const foo = find('.foo');
11+
assert.equal(foo.querySelector('.bar').textContent.trim(), 'bar');
12+
assert.equal(find('.foo').querySelector('.bar').textContent.trim(), 'bar');
13+
assert.equal(find('.foo .bar').textContent.trim(), 'bar');
14+
assert.equal(foo.querySelectorAll('.bar').length, 2);
15+
assert.equal(find('.foo').querySelectorAll('.bar').length, 2);
16+
assert.equal(findAll('.foo .bar').length, 2);
17+
18+
await click(foo.querySelector('button'));
19+
await click('button', { shiftKey: true });
20+
await click(foo.querySelector('button'), { shiftKey: true });
21+
});

transforms/native-dom/index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,69 @@ function renameCallee(j, root, name, newName) {
3838
.forEach(({ node }) => node.callee.name = newName);
3939
}
4040

41+
/**
42+
* find(selector, context) => context.querySelector(selector)
43+
* findAll(selector, context) => context.querySelectorAll(selector)
44+
* click(selector, context) => click(context.querySelector(selector))
45+
* click(selector, context, { ...options }) => click(context.querySelector(selector), { ...options })
46+
*/
47+
function migrateHelpersWithContext(j, root, importedName) {
48+
switch (importedName) {
49+
case 'find':
50+
root
51+
.find(j.CallExpression, {
52+
callee: {
53+
name: 'find'
54+
},
55+
arguments: [ {}, {} ]
56+
})
57+
.replaceWith(({node}) => {
58+
let [ selector, context ] = node.arguments;
59+
if (selector.type === 'StringLiteral' && context.type === 'StringLiteral') {
60+
return j.callExpression(j.identifier('find'), [ j.literal(`${context.value} ${selector.value}`) ]);
61+
}
62+
return j.callExpression(j.memberExpression(context, j.identifier('querySelector')), [ selector ])
63+
});
64+
break;
65+
case 'findAll':
66+
root
67+
.find(j.CallExpression, {
68+
callee: {
69+
name: 'findAll'
70+
},
71+
arguments: [ {}, {} ]
72+
})
73+
.replaceWith(({node}) => {
74+
let [ selector, context ] = node.arguments;
75+
if (selector.type === 'StringLiteral' && context.type === 'StringLiteral') {
76+
return j.callExpression(j.identifier('findAll'), [ j.literal(`${context.value} ${selector.value}`) ]);
77+
}
78+
return j.callExpression(j.memberExpression(context, j.identifier('querySelectorAll')), [ selector ])
79+
});
80+
break;
81+
case 'click':
82+
root
83+
.find(j.CallExpression, {
84+
callee: {
85+
name: 'click'
86+
},
87+
arguments: [ {}, {} ] // matches 2 or 3 arguments
88+
})
89+
.replaceWith(({node}) => {
90+
let [ selector, context, options ] = node.arguments;
91+
if (context.type === 'ObjectExpression') {
92+
// click(selector, { ...options })
93+
return node;
94+
}
95+
if (selector.type === 'StringLiteral' && context.type === 'StringLiteral') {
96+
return j.callExpression(j.identifier('click'), [ j.literal(`${context.value} ${selector.value}`) ]);
97+
}
98+
return j.callExpression(j.identifier('click'), [ j.callExpression(j.memberExpression(context, j.identifier('querySelector')), [ selector ]), options ].filter(Boolean));
99+
});
100+
break;
101+
}
102+
}
103+
41104
/**
42105
* Transform imports from ember-native-dom-helpers to @ember/test-helpers
43106
*
@@ -73,6 +136,7 @@ function transform(file, api) {
73136
if (importedName !== mappedName) {
74137
renameCallee(j, root, importedName, mappedName);
75138
}
139+
migrateHelpersWithContext(j, root, importedName);
76140
} else {
77141
newSpecifiers.push(specifier);
78142
}

0 commit comments

Comments
 (0)