Skip to content

Commit c70c8a9

Browse files
committed
Add tests for findTagName() and findElementId()
1 parent 491cfa2 commit c70c8a9

5 files changed

Lines changed: 2034 additions & 48 deletions

File tree

.eslintrc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ module.exports = {
1414
'no-console': 'off',
1515
'prettier/prettier': 'error',
1616
},
17+
overrides: [{
18+
files: '**/__tests__/**/*.js',
19+
env: {
20+
jest: true,
21+
},
22+
}]
1723
};

lib/__tests__/find-properties.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const j = require('jscodeshift').withParser('ts');
2+
3+
const { findTagName, findElementId } = require('../transform');
4+
5+
describe('findTagName()', () => {
6+
const TESTS = [
7+
['', 'div'],
8+
["tagName: ''", ''],
9+
["tagName: 'div'", 'div'],
10+
["tagName: 'span'", 'span'],
11+
["tagName: 'svg'", 'svg'],
12+
['tagName: 5', /Unexpected `tagName` value: 5/],
13+
['tagName: foo', /Unexpected `tagName` value: foo/],
14+
];
15+
16+
for (let [input, expected] of TESTS) {
17+
test(input || 'empty', () => {
18+
let props = j(`export default {${input}};`)
19+
.find(j.ExportDefaultDeclaration)
20+
.get('declaration', 'properties');
21+
22+
if (expected instanceof RegExp) {
23+
expect(() => findTagName(props)).toThrow(expected);
24+
} else {
25+
expect(findTagName(props)).toEqual(expected);
26+
}
27+
});
28+
}
29+
});
30+
31+
describe('findElementId()', () => {
32+
const TESTS = [
33+
['', null],
34+
["elementId: ''", ''],
35+
["elementId: 'foo'", 'foo'],
36+
['elementId: 5', /Unexpected `elementId` value: 5/],
37+
['elementId: foo', /Unexpected `elementId` value: foo/],
38+
];
39+
40+
for (let [input, expected] of TESTS) {
41+
test(input || 'empty', () => {
42+
let props = j(`export default {${input}};`)
43+
.find(j.ExportDefaultDeclaration)
44+
.get('declaration', 'properties');
45+
46+
if (expected instanceof RegExp) {
47+
expect(() => findElementId(props)).toThrow(expected);
48+
} else {
49+
expect(findElementId(props)).toEqual(expected);
50+
}
51+
});
52+
}
53+
});

lib/transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,4 @@ function findClassNameBindings(properties) {
255255
return classNameBindings;
256256
}
257257

258-
module.exports = { transform };
258+
module.exports = { transform, findTagName, findElementId };

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"scripts": {
1313
"changelog": "lerna-changelog",
14-
"lint": "eslint . --cache"
14+
"lint": "eslint . --cache",
15+
"test": "jest"
1516
},
1617
"dependencies": {
1718
"chalk": "^2.4.2",
@@ -26,6 +27,7 @@
2627
"eslint-config-prettier": "^6.1.0",
2728
"eslint-plugin-node": "^9.1.0",
2829
"eslint-plugin-prettier": "^3.1.0",
30+
"jest": "^24.9.0",
2931
"lerna-changelog": "^0.8.2",
3032
"prettier": "1.18.2"
3133
},

0 commit comments

Comments
 (0)