-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathget-element-type.mjs
73 lines (63 loc) · 2.34 KB
/
get-element-type.mjs
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
import {expect} from 'chai'
import {getElementType} from '../../lib/utils/get-element-type.js'
import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js'
import {describe, it} from 'mocha'
function mockSetting(componentSetting = {}) {
return {
settings: {
github: {
components: componentSetting,
polymorphicPropName: 'as',
},
},
}
}
describe('getElementType', function () {
it('returns raw element type', function () {
const node = mockJSXOpeningElement('a')
expect(getElementType({}, node)).to.equal('a')
})
it('returns polymorphic element type', function () {
const node = mockJSXOpeningElement('Link', [mockJSXAttribute('as', 'button')])
const setting = mockSetting({
Link: 'a',
})
expect(getElementType(setting, node)).to.equal('button')
})
it('returns raw type if no default or matching prop setting', function () {
const setting = mockSetting({})
const node = mockJSXOpeningElement('Link')
expect(getElementType(setting, node)).to.equal('Link')
})
it('returns default type if no polymorphic prop is passed in', function () {
const setting = mockSetting({
Link: 'a',
})
const node = mockJSXOpeningElement('Link')
expect(getElementType(setting, node)).to.equal('a')
})
it('if rendered as another component check its default type', function () {
const setting = mockSetting({
Link: 'a',
Button: 'button',
})
const node = mockJSXOpeningElement('Link', [mockJSXAttribute('as', 'Button')])
expect(getElementType(setting, node)).to.equal('button')
})
it('returns raw type when polymorphic prop is set to non-literal expression', function () {
// <Box as={isNavigationOpen ? 'generic' : 'navigation'} />
const node = mockJSXOpeningElement('Box', [
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'),
])
expect(getElementType({}, node)).to.equal('Box')
})
it('returns raw type when polymorphic prop is set to non-literal expression even with component setting', function () {
const setting = mockSetting({
Box: 'div',
})
const node = mockJSXOpeningElement('Box', [
mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'),
])
expect(getElementType(setting, node)).to.equal('Box')
})
})