@@ -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