-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathget-element-type.js
More file actions
85 lines (75 loc) · 2.25 KB
/
get-element-type.js
File metadata and controls
85 lines (75 loc) · 2.25 KB
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
const {getElementType} = require('../../lib/utils/get-element-type')
const {mockJSXAttribute, mockJSXOpeningElement} = require('./helpers')
const mocha = require('mocha')
const describe = mocha.describe
const it = mocha.it
const expect = require('chai').expect
function mockSetting(componentSetting = {}) {
return {
settings: {
github: {
components: componentSetting,
},
},
}
}
describe('getElementType', function () {
it('returns raw element type', function () {
const node = mockJSXOpeningElement('a')
expect(getElementType({}, node)).to.equal('a')
})
it('returns element type from default if set', function () {
const node = mockJSXOpeningElement('Link', [mockJSXAttribute('as', 'summary')])
const setting = mockSetting({
Link: {
default: 'button',
},
})
expect(getElementType(setting, node)).to.equal('button')
})
it('returns element type from matching props setting if set', function () {
const setting = mockSetting({
Link: {
default: 'a',
props: {
as: {summary: 'summary'},
},
},
})
const node = mockJSXOpeningElement('Link', [mockJSXAttribute('as', 'summary')])
expect(getElementType(setting, node)).to.equal('summary')
})
it('returns raw type if no default or matching prop setting', function () {
const setting = mockSetting({
Link: {
props: {
as: {summary: 'summary'},
},
},
})
const node = mockJSXOpeningElement('Link', [mockJSXAttribute('as', 'p')])
expect(getElementType(setting, node)).to.equal('Link')
})
it('allows undefined prop to be mapped to a type', function () {
const setting = mockSetting({
Link: {
props: {
as: {undefined: 'a'},
},
},
})
const node = mockJSXOpeningElement('Link')
expect(getElementType(setting, node)).to.equal('a')
})
it('returns raw type if prop does not match props setting and no default type', function () {
const setting = mockSetting({
Link: {
props: {
as: {undefined: 'a'},
},
},
})
const node = mockJSXOpeningElement('Link', [mockJSXAttribute('as', 'p')])
expect(getElementType(setting, node)).to.equal('Link')
})
})