Skip to content

Commit d4c4742

Browse files
committed
Add tests
1 parent 790decc commit d4c4742

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/**
2+
* @fileoverview Check if there are no side effects inside computed properties
3+
* @author 2017 Armano
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
const RuleTester = require('eslint').RuleTester
12+
const rule = require('../../../lib/rules/no-async-in-computed-properties')
13+
14+
// ------------------------------------------------------------------------------
15+
// Tests
16+
// ------------------------------------------------------------------------------
17+
18+
const ruleTester = new RuleTester()
19+
20+
ruleTester.run('no-async-in-computed-properties', rule, {
21+
22+
valid: [
23+
{
24+
filename: 'test.vue',
25+
code: `
26+
export default {
27+
computed: {
28+
foo: function () {
29+
var bar = 0
30+
try {
31+
bar = bar / 0
32+
} catch (e) {
33+
return e
34+
} finally {
35+
return bar
36+
}
37+
}
38+
}
39+
}
40+
`,
41+
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
42+
}
43+
],
44+
45+
invalid: [
46+
{
47+
filename: 'test.vue',
48+
code: `
49+
export default {
50+
computed: {
51+
foo: async function () {
52+
return await someFunc()
53+
}
54+
}
55+
}
56+
`,
57+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
58+
errors: [
59+
'Unexpected await in "foo" computed property.',
60+
'Unexpected async in "foo" computed property.'
61+
]
62+
},
63+
{
64+
filename: 'test.vue',
65+
code: `
66+
export default {
67+
computed: {
68+
foo: async function () {
69+
return new Promise((resolve, reject) => {}) }
70+
}
71+
}
72+
}
73+
`,
74+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
75+
errors: [
76+
'Unexpected asynchronous action in "foo" computed property.',
77+
'Unexpected async in "foo" computed property.'
78+
]
79+
},
80+
{
81+
filename: 'test.vue',
82+
code: `
83+
export default {
84+
computed: {
85+
foo: function () {
86+
return bar.then(response => {})
87+
}
88+
}
89+
}
90+
`,
91+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
92+
errors: ['Unexpected asynchronous action in "foo" computed property.']
93+
},
94+
{
95+
filename: 'test.vue',
96+
code: `
97+
export default {
98+
computed: {
99+
foo: function () {
100+
return bar.catch(e => {})
101+
}
102+
}
103+
}
104+
`,
105+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
106+
errors: ['Unexpected asynchronous action in "foo" computed property.']
107+
},
108+
{
109+
filename: 'test.vue',
110+
code: `
111+
export default {
112+
computed: {
113+
foo: function () {
114+
return Promise.all([])
115+
}
116+
}
117+
}
118+
`,
119+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
120+
errors: ['Unexpected asynchronous action in "foo" computed property.']
121+
},
122+
{
123+
filename: 'test.vue',
124+
code: `
125+
export default {
126+
computed: {
127+
foo: function () {
128+
return bar.finally(res => {})
129+
}
130+
}
131+
}
132+
`,
133+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
134+
errors: ['Unexpected asynchronous action in "foo" computed property.']
135+
},
136+
{
137+
filename: 'test.vue',
138+
code: `
139+
export default {
140+
computed: {
141+
foo: function () {
142+
return Promise.race([])
143+
}
144+
}
145+
}
146+
`,
147+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
148+
errors: ['Unexpected asynchronous action in "foo" computed property.']
149+
},
150+
{
151+
filename: 'test.vue',
152+
code: `
153+
export default {
154+
computed: {
155+
foo: function () {
156+
return Promise.reject([])
157+
}
158+
}
159+
}
160+
`,
161+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
162+
errors: ['Unexpected asynchronous action in "foo" computed property.']
163+
},
164+
{
165+
filename: 'test.vue',
166+
code: `
167+
export default {
168+
computed: {
169+
foo: function () {
170+
return Promise.resolve([])
171+
}
172+
}
173+
}
174+
`,
175+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
176+
errors: ['Unexpected asynchronous action in "foo" computed property.']
177+
},
178+
{
179+
filename: 'test.vue',
180+
code: `
181+
export default {
182+
computed: {
183+
foo: {
184+
get () {
185+
return Promise.resolve([])
186+
}
187+
}
188+
}
189+
}
190+
`,
191+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
192+
errors: ['Unexpected asynchronous action in "foo" computed property.']
193+
}
194+
]
195+
})

0 commit comments

Comments
 (0)