-
-
Notifications
You must be signed in to change notification settings - Fork 631
/
getElementType-test.js
79 lines (65 loc) · 3.07 KB
/
getElementType-test.js
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
import expect from 'expect';
import getElementType from '../../../src/util/getElementType';
import JSXElementMock from '../../../__mocks__/JSXElementMock';
import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock';
describe('getElementType', () => {
describe('no settings in context', () => {
const elementType = getElementType({ settings: {} });
it('should return the exact tag name for a DOM element', () => {
expect(elementType(JSXElementMock('input').openingElement)).toBe('input');
});
it('should return the exact tag name for a custom element', () => {
expect(elementType(JSXElementMock('CustomInput').openingElement)).toBe('CustomInput');
});
it('should return the exact tag name for names that are in Object.prototype', () => {
expect(elementType(JSXElementMock('toString').openingElement)).toBe('toString');
});
it('should return the default tag name provided', () => {
expect(elementType(JSXElementMock('span', [JSXAttributeMock('as', 'h1')]).openingElement)).toBe('span');
});
});
describe('components settings in context', () => {
const elementType = getElementType({
settings: {
'jsx-a11y': {
components: {
CustomInput: 'input',
},
},
},
});
it('should return the exact tag name for a DOM element', () => {
expect(elementType(JSXElementMock('input').openingElement)).toBe('input');
});
it('should return the mapped tag name for a custom element', () => {
expect(elementType(JSXElementMock('CustomInput').openingElement)).toBe('input');
});
it('should return the exact tag name for a custom element not in the components map', () => {
expect(elementType(JSXElementMock('CityInput').openingElement)).toBe('CityInput');
});
it('should return the default tag name since not polymorphicPropName was provided', () => {
expect(elementType(JSXElementMock('span', [JSXAttributeMock('as', 'h1')]).openingElement)).toBe('span');
});
});
describe('polymorphicPropName settings in context', () => {
const elementType = getElementType({
settings: {
'jsx-a11y': {
polymorphicPropName: 'asChild',
components: {
CustomButton: 'button',
},
},
},
});
it('should return the tag name provided by the polymorphic prop, "asChild", defined in the settings', () => {
expect(elementType(JSXElementMock('span', [JSXAttributeMock('asChild', 'h1')]).openingElement)).toBe('h1');
});
it('should return the tag name provided by the polymorphic prop, "asChild", defined in the settings instead of the component mapping tag', () => {
expect(elementType(JSXElementMock('CustomButton', [JSXAttributeMock('asChild', 'a')]).openingElement)).toBe('a');
});
it('should return the tag name provided by the componnet mapping if the polymorphic prop, "asChild", defined in the settings is not set', () => {
expect(elementType(JSXElementMock('CustomButton', [JSXAttributeMock('as', 'a')]).openingElement)).toBe('button');
});
});
});