Skip to content

Commit 2354086

Browse files
committed
Add test for flattenComponents, bump eslint-plugin-github
1 parent 839f40e commit 2354086

File tree

5 files changed

+98
-35
lines changed

5 files changed

+98
-35
lines changed

package-lock.json

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"prettier": "@github/prettier-config",
3636
"dependencies": {
3737
"@styled-system/props": "^5.1.5",
38-
"eslint-plugin-github": "^4.9.1",
38+
"eslint-plugin-github": "^4.9.2",
3939
"eslint-plugin-jsx-a11y": "^6.7.1",
4040
"eslint-traverse": "^1.0.0",
4141
"lodash": "^4.17.21",

src/configs/components.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const components = {
1+
const flattenComponents = require('../utils/flatten-components');
2+
3+
const components = flattenComponents({
24
Button: 'button',
35
IconButton: 'button',
46
Link: 'a',
57
Spinner: 'svg',
6-
Octicon: 'svg',
78
ToggleSwitch: 'button',
89
Radio: 'input',
910
TextInput: {
@@ -18,23 +19,6 @@ const components = {
1819
Link: 'a',
1920
self: 'nav',
2021
},
21-
}
22-
23-
const flattenComponents = (componentObj) => {
24-
let result = {};
25-
26-
Object.keys(componentObj).forEach((key) => {
27-
if (typeof componentObj[key] === 'object') {
28-
const test = Object.keys(componentObj[key]).forEach((item) => {
29-
result = { ...result, [`${key}${item !== 'self' ? `.${item}` : ''}`]: componentObj[key][item] };
30-
});
31-
} else {
32-
result = {...result, [key]: componentObj[key]}
33-
}
34-
});
35-
36-
return result;
37-
}
38-
22+
})
3923

40-
module.exports = flattenComponents(components);
24+
module.exports = components;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const {flattenComponents} = require('../flatten-components')
2+
3+
const mockComponents = function(passedObj) {
4+
return {
5+
Button: 'button',
6+
Link: 'a',
7+
Spinner: 'svg',
8+
Radio: 'input',
9+
TextInput: {
10+
Action: 'button',
11+
self: 'input'
12+
},
13+
...passedObj
14+
}
15+
}
16+
17+
describe('getElementType', function() {
18+
it('flattens passed object 1-level deep', function() {
19+
const result = flattenComponents(mockComponents())
20+
21+
const expectedResult = {
22+
Button: 'button',
23+
Link: 'a',
24+
Spinner: 'svg',
25+
Radio: 'input',
26+
TextInput: 'input',
27+
'TextInput.Action': 'button'
28+
}
29+
30+
expect(result).toEqual(expectedResult)
31+
})
32+
33+
it('ignores objects nested deeper than 1-level', function() {
34+
const result = flattenComponents(
35+
mockComponents({
36+
Select: {
37+
Items: {
38+
self: 'div'
39+
},
40+
Option: 'option',
41+
self: 'select'
42+
}
43+
})
44+
)
45+
46+
const expectedResult = {
47+
Button: 'button',
48+
Link: 'a',
49+
Spinner: 'svg',
50+
Radio: 'input',
51+
TextInput: 'input',
52+
'TextInput.Action': 'button',
53+
'Select.Items': {
54+
self: 'div'
55+
},
56+
Select: 'select',
57+
'Select.Option': 'option'
58+
}
59+
60+
expect(result).toEqual(expectedResult)
61+
})
62+
})

src/utils/flatten-components.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function flattenComponents(componentObj) {
2+
let result = {};
3+
4+
Object.keys(componentObj).forEach((key) => {
5+
if (typeof componentObj[key] === 'object') {
6+
const test = Object.keys(componentObj[key]).forEach((item) => {
7+
result = { ...result, [`${key}${item !== 'self' ? `.${item}` : ''}`]: componentObj[key][item] };
8+
});
9+
} else {
10+
result = {...result, [key]: componentObj[key]}
11+
}
12+
});
13+
14+
return result;
15+
}
16+
17+
exports.flattenComponents = flattenComponents

0 commit comments

Comments
 (0)