forked from mui/material-ui
/
mergeSlotProps.test.ts
166 lines (137 loc) · 3.97 KB
/
mergeSlotProps.test.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import * as React from 'react';
import { expect } from 'chai';
import mergeSlotProps from './mergeSlotProps';
import { EventHandlers } from './types';
describe('mergeSlotProps', () => {
it('overrides the internal props with the external ones', () => {
const getSlotProps = () => ({
prop1: 'internal',
prop2: 'internal',
prop3: 'internal',
prop4: 'internal',
});
const additionalProps = {
prop1: 'additional',
prop2: 'additional',
prop3: 'additional',
};
const externalForwardedProps = {
prop1: 'externalForwarded',
prop2: 'externalForwarded',
};
const externalSlotProps = {
prop1: 'externalSlot',
};
const merged = mergeSlotProps({
getSlotProps,
additionalProps,
externalForwardedProps,
externalSlotProps,
});
expect(merged.props.prop1).to.equal('externalSlot');
expect(merged.props.prop2).to.equal('externalForwarded');
expect(merged.props.prop3).to.equal('additional');
expect(merged.props.prop4).to.equal('internal');
});
it('joins all the class names', () => {
const getSlotProps = () => ({
className: 'internal',
});
const additionalProps = {
className: 'additional',
};
const externalForwardedProps = {
className: 'externalForwarded',
};
const externalSlotProps = {
className: 'externalSlot',
};
const className = ['class1', 'class2'];
const merged = mergeSlotProps({
getSlotProps,
additionalProps,
externalForwardedProps,
externalSlotProps,
className,
});
expect(merged.props.className).to.contain('class1');
expect(merged.props.className).to.contain('class2');
expect(merged.props.className).to.contain('internal');
expect(merged.props.className).to.contain('additional');
expect(merged.props.className).to.contain('externalForwarded');
expect(merged.props.className).to.contain('externalSlot');
});
it('merges the style props', () => {
const getSlotProps = () => ({
style: {
fontSize: '12px',
textAlign: 'center' as const,
},
});
const additionalProps = {
style: {
fontSize: '14px',
color: 'red',
},
};
const externalForwardedProps = {
style: {
fontWeight: 500,
},
};
const externalSlotProps = {
style: {
textDecoration: 'underline',
},
};
const merged = mergeSlotProps({
getSlotProps,
additionalProps,
externalForwardedProps,
externalSlotProps,
});
expect(merged.props.style).to.deep.equal({
textAlign: 'center',
color: 'red',
fontSize: '14px',
fontWeight: 500,
textDecoration: 'underline',
});
});
it('returns the ref returned from the getSlotProps function', () => {
const ref = React.createRef();
const getSlotProps = () => ({
ref,
});
const merged = mergeSlotProps({
getSlotProps,
});
expect(merged.internalRef).to.equal(ref);
});
it('does not require any parameters', () => {
const merged = mergeSlotProps({});
expect(merged.props).to.deep.equal({});
});
it('passes the external event handlers to the getSlotProps function (if defined)', () => {
const externalClickHandler = () => {};
const externalMouseOverHandler = () => {};
const getSlotProps = (eventHandlers: EventHandlers) => {
expect(eventHandlers.onClick).to.equal(externalClickHandler);
expect(eventHandlers.onMouseOver).to.equal(externalMouseOverHandler);
return {};
};
const externalForwardedProps = {
onClick: externalClickHandler,
};
const externalSlotProps = {
onMouseOver: externalMouseOverHandler,
};
const merged = mergeSlotProps({
getSlotProps,
externalForwardedProps,
externalSlotProps,
});
expect(Object.keys(merged.props)).not.to.contain('onClick');
expect(Object.keys(merged.props)).not.to.contain('onMouseOver');
});
});