-
Notifications
You must be signed in to change notification settings - Fork 3
/
action-modifiers.js
118 lines (115 loc) · 3.83 KB
/
action-modifiers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const actionModifiers = require('../action-modifiers');
const { parse, transform } = require('ember-template-recast');
function codeshift(input, plugin) {
return plugin(
{
path: 'filename.hbs',
source: input,
},
{
parse,
visit(ast, callback) {
const results = transform(ast, callback);
return results && results.code;
},
}
);
}
const TESTS = [
[
'string action name',
`<button {{action "foo" bar}}>button</button>`,
`<button {{on "click" (prevent-default (fn (action "foo") bar))}}>button</button>`
],
[
'on= option',
`<button {{action "foo" bar on="hover"}}>button</button>`,
`<button {{on "hover" (prevent-default (fn (action "foo") bar))}}>button</button>`
],
[
'@arg',
`<button {{action @bar baz}}>button</button>`,
`<button {{on "click" (prevent-default (fn @bar baz))}}>button</button>`
],
[
'this.',
`<button {{action this.quux}}>button</button>`,
`<button {{on "click" (prevent-default this.quux)}}>button</button>`
],
[
'two modifiers',
`<button {{action bar}} {{action baz on="hover"}}>button</button>`,
`<button {{on "click" (prevent-default bar)}} {{on "hover" (prevent-default baz)}}>button</button>`
],
[
'preventDefault=false',
`<button {{action foo preventDefault=false}}>button</button>`,
`<button {{on "click" foo}}>button</button>`,
],
[
'bubbles=false',
`<button {{action foo bubbles=false}}>button</button>`,
`<button {{on "click" (stop-propagation (prevent-default foo))}}>button</button>`,
],
[
'bubbles=true',
`<button {{action foo bubbles=true}}>button</button>`,
`<button {{on "click" (prevent-default foo)}}>button</button>`,
],
[
'preventDefault and bubbles',
`<button {{action foo preventDefault=false bubbles=false}}>button</button>`,
`<button {{on "click" (stop-propagation foo)}}>button</button>`,
],
[
'value=',
`<button {{action foo value="target.value"}}>button</button>`,
`<button {{on "click" (prevent-default (action foo value="target.value"))}}>button</button>`,
],
[
'target=',
`<button {{action foo target=someService}}>button</button>`,
`<button {{on "click" (prevent-default (action foo target=someService))}}>button</button>`,
],
[
'allowedKeys=',
`<button {{action foo allowedKeys="alt"}}>button</button>`,
`<button {{on "click" (prevent-default (action foo allowedKeys="alt"))}}>button</button>`,
],
[
'value && target',
`<button {{action "foo" value="target.value" target=someService}}>button</button>`,
`<button {{on "click" (prevent-default (action "foo" value="target.value" target=someService))}}>button</button>`,
],
[
'preventDefault=this.dynamicValue',
`<button {{action foo preventDefault=this.dynamicValue}}>button</button>`,
`<button {{action foo preventDefault=this.dynamicValue}}>button</button>`
],
[
'bubbles=this.dynamicValue',
`<button {{action foo bubbles=this.dynamicValue}}>button</button>`,
`<button {{action foo bubbles=this.dynamicValue}}>button</button>`
],
[
'mut',
`<button {{action (action (mut this.enabled) false)}}>button</button>`,
`<button {{on "click" (prevent-default (fn (mut this.enabled) false))}}>button</button>`
],
[
'closure actions',
`<button {{action (action "foo" false)}}>button</button>`,
`<button {{on "click" (prevent-default (fn (action "foo") false))}}>button</button>`
],
[
'idempotent',
`<button {{on "click" (prevent-default (fn (action "foo") false))}}>button</button>`,
`<button {{on "click" (prevent-default (fn (action "foo") false))}}>button</button>`
]
];
TESTS.forEach(([name, input, expectedOutput]) => {
it(name, () => {
const output = codeshift(input, actionModifiers);
expect((output || '').trim()).toEqual(expectedOutput.trim());
});
});