Skip to content

Commit 07d2752

Browse files
committed
Add more tests for find*() functions
1 parent bf4513c commit 07d2752

2 files changed

Lines changed: 102 additions & 2 deletions

File tree

lib/__tests__/find-properties.js

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const j = require('jscodeshift').withParser('ts');
22

3-
const { findTagName, findElementId } = require('../transform');
3+
const {
4+
findTagName,
5+
findElementId,
6+
findAttributeBindings,
7+
findClassNames,
8+
findClassNameBindings,
9+
} = require('../transform');
410

511
describe('findTagName()', () => {
612
const TESTS = [
@@ -51,3 +57,90 @@ describe('findElementId()', () => {
5157
});
5258
}
5359
});
60+
61+
describe('findAttributeBindings()', () => {
62+
const TESTS = [
63+
['', new Map()],
64+
["attributeBindings: ['foo']", new Map([['foo', 'foo']])],
65+
["attributeBindings: ['foo:bar', 'BAZ']", new Map([['foo', 'bar'], ['BAZ', 'BAZ']])],
66+
["attributeBindings: ''", /Unexpected `attributeBindings` value: ''/],
67+
['attributeBindings: 5', /Unexpected `attributeBindings` value: 5/],
68+
['attributeBindings: foo', /Unexpected `attributeBindings` value: foo/],
69+
['attributeBindings: [42]', /Unexpected `attributeBindings` value: \[42]/],
70+
];
71+
72+
for (let [input, expected] of TESTS) {
73+
test(input || 'empty', () => {
74+
let props = j(`export default {${input}};`)
75+
.find(j.ExportDefaultDeclaration)
76+
.get('declaration', 'properties');
77+
78+
if (expected instanceof RegExp) {
79+
expect(() => findAttributeBindings(props)).toThrow(expected);
80+
} else {
81+
expect(findAttributeBindings(props)).toEqual(expected);
82+
}
83+
});
84+
}
85+
});
86+
87+
describe('findClassNames()', () => {
88+
const TESTS = [
89+
['', []],
90+
["classNames: ['foo']", ['foo']],
91+
["classNames: ['foo', 'bar']", ['foo', 'bar']],
92+
["classNames: ['foo', 'bar', 'BAZ']", ['foo', 'bar', 'BAZ']],
93+
["classNames: ''", /Unexpected `classNames` value: ''/],
94+
['classNames: 5', /Unexpected `classNames` value: 5/],
95+
['classNames: foo', /Unexpected `classNames` value: foo/],
96+
['classNames: [42]', /Unexpected `classNames` value: \[42]/],
97+
];
98+
99+
for (let [input, expected] of TESTS) {
100+
test(input || 'empty', () => {
101+
let props = j(`export default {${input}};`)
102+
.find(j.ExportDefaultDeclaration)
103+
.get('declaration', 'properties');
104+
105+
if (expected instanceof RegExp) {
106+
expect(() => findClassNames(props)).toThrow(expected);
107+
} else {
108+
expect(findClassNames(props)).toEqual(expected);
109+
}
110+
});
111+
}
112+
});
113+
114+
describe('findClassNameBindings()', () => {
115+
const TESTS = [
116+
['', new Map()],
117+
["classNameBindings: ['foo']", new Map([['foo', ['foo', null]]])],
118+
["classNameBindings: ['fooBar']", new Map([['fooBar', ['foo-bar', null]]])],
119+
["classNameBindings: ['FOO']", new Map([['FOO', ['foo', null]]])],
120+
["classNameBindings: ['foo:bar']", new Map([['foo', ['bar', null]]])],
121+
["classNameBindings: ['foo:bar:baz']", new Map([['foo', ['bar', 'baz']]])],
122+
["classNameBindings: ['foo::baz']", new Map([['foo', [null, 'baz']]])],
123+
[
124+
"classNameBindings: ['foo', 'bar']",
125+
new Map([['foo', ['foo', null]], ['bar', ['bar', null]]]),
126+
],
127+
["classNameBindings: ''", /Unexpected `classNameBindings` value: ''/],
128+
['classNameBindings: 5', /Unexpected `classNameBindings` value: 5/],
129+
['classNameBindings: foo', /Unexpected `classNameBindings` value: foo/],
130+
['classNameBindings: [42]', /Unexpected `classNameBindings` value: \[42]/],
131+
];
132+
133+
for (let [input, expected] of TESTS) {
134+
test(input || 'empty', () => {
135+
let props = j(`export default {${input}};`)
136+
.find(j.ExportDefaultDeclaration)
137+
.get('declaration', 'properties');
138+
139+
if (expected instanceof RegExp) {
140+
expect(() => findClassNameBindings(props)).toThrow(expected);
141+
} else {
142+
expect(findClassNameBindings(props)).toEqual(expected);
143+
}
144+
});
145+
}
146+
});

lib/transform.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,11 @@ function findClassNameBindings(properties) {
249249
return classNameBindings;
250250
}
251251

252-
module.exports = { transform, findTagName, findElementId };
252+
module.exports = {
253+
transform,
254+
findTagName,
255+
findElementId,
256+
findAttributeBindings,
257+
findClassNames,
258+
findClassNameBindings,
259+
};

0 commit comments

Comments
 (0)