Skip to content

Commit efe2258

Browse files
committed
fix(Switch): Removed extra props
1 parent 55f27da commit efe2258

File tree

2 files changed

+60
-28
lines changed

2 files changed

+60
-28
lines changed

src/components/Switch/Switch.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { objectMapper } from '@bluebase/component-mapper';
66
import { styles } from './styles';
77
import { withPropsStyles } from '../../withPropsStyles';
88

9-
10-
119
const map = {
1210
// If color is primary, secondary or default set as is
1311
color: ({ color }: SwitchProps) => {
@@ -26,23 +24,30 @@ const map = {
2624
if (onValueChange) {
2725
onValueChange(event.target.value, checked);
2826
}
29-
}
27+
},
3028

29+
checked: 'checked',
30+
classes: 'classes',
31+
disabled: 'disabled',
32+
id: 'id',
33+
label: 'label',
34+
labelPlacement: 'labelPlacement',
35+
name: 'name',
36+
value: 'value',
3137
};
3238

3339
export const Switch = withPropsStyles(styles)((props: SwitchProps) => {
34-
35-
const newProps = objectMapper(props, map, { rest: true, ignore: ['onValueChange'] });
40+
const newProps = objectMapper(props, map);
3641

3742
const { label, labelPlacement, classes, ...common } = newProps;
43+
3844
if (classes.color) {
3945
delete common.color;
4046
}
4147

4248
const { label: labelClass, labelPlacementStart, ...switchClasses } = classes;
4349

44-
const node = (<MUISwitch classes={switchClasses} {...common} />
45-
);
50+
const node = <MUISwitch classes={switchClasses} {...common} />;
4651

4752
if (!label) {
4853
return node;
@@ -51,6 +56,7 @@ export const Switch = withPropsStyles(styles)((props: SwitchProps) => {
5156
return (
5257
<FormControlLabel
5358
{...common}
59+
value={common.value}
5460
label={label}
5561
labelPlacement={labelPlacement}
5662
control={node}

src/components/Switch/__tests__/Switch.test.tsx

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { styles } from '../styles';
88
import { waitForElement } from 'enzyme-async-helpers';
99

1010
describe('Switch', () => {
11-
1211
it('should set the checked to true when checked is true', async () => {
1312
const component = mount(
1413
<BlueBaseApp plugins={[Plugin]}>
@@ -18,11 +17,15 @@ describe('Switch', () => {
1817
await waitForElement(component, Switch);
1918

2019
// expect(component).toMatchSnapshot();
21-
expect(component.find('Switch').first().prop('checked')).toEqual(true);
20+
expect(
21+
component
22+
.find('Switch')
23+
.first()
24+
.prop('checked')
25+
).toEqual(true);
2226
});
2327

2428
it('should have different colors in dark mode', () => {
25-
2629
const theme = createMuiTheme({ palette: { type: 'dark' } });
2730

2831
const classes = styles({ color: 'red' }, theme);
@@ -38,7 +41,12 @@ describe('Switch', () => {
3841
);
3942
await waitForElement(component, Switch);
4043

41-
expect(component.find('Switch').first().prop('checked')).toEqual(false);
44+
expect(
45+
component
46+
.find('Switch')
47+
.first()
48+
.prop('checked')
49+
).toEqual(false);
4250
});
4351

4452
it('should pass the color as is when set to "primary"', async () => {
@@ -49,7 +57,12 @@ describe('Switch', () => {
4957
);
5058
await waitForElement(component, Switch);
5159

52-
expect(component.find('Switch').first().prop('color')).toEqual('primary');
60+
expect(
61+
component
62+
.find('Switch')
63+
.first()
64+
.prop('color')
65+
).toEqual('primary');
5366
});
5467

5568
it('should pass the color as is when set to "secondary"', async () => {
@@ -60,7 +73,12 @@ describe('Switch', () => {
6073
);
6174
await waitForElement(component, Switch);
6275

63-
expect(component.find('Switch').first().prop('color')).toEqual('secondary');
76+
expect(
77+
component
78+
.find('Switch')
79+
.first()
80+
.prop('color')
81+
).toEqual('secondary');
6482
});
6583

6684
it('should pass the color as is when set to "default"', async () => {
@@ -71,7 +89,12 @@ describe('Switch', () => {
7189
);
7290
await waitForElement(component, Switch);
7391

74-
expect(component.find('Switch').first().prop('color')).toEqual('default');
92+
expect(
93+
component
94+
.find('Switch')
95+
.first()
96+
.prop('color')
97+
).toEqual('default');
7598
});
7699

77100
it('should set the color prop to undefined and create classes for custom colors', async () => {
@@ -81,9 +104,13 @@ describe('Switch', () => {
81104
</BlueBaseApp>
82105
);
83106
await waitForElement(component, Switch);
84-
// expect(component).toMatchSnapshot();
85-
expect(component.find('WithStyles(Switch)').first().prop('classes')).toBeTruthy();
86-
expect(component.find('WithStyles(Switch)').first().prop('color')).toEqual('red');
107+
108+
expect(
109+
component
110+
.find('WithStyles(Component)')
111+
.first()
112+
.prop('color')
113+
).toEqual('red');
87114
});
88115

89116
it('should set the label component', async () => {
@@ -95,15 +122,18 @@ describe('Switch', () => {
95122
await waitForElement(component, Switch);
96123
// expect(component).toMatchSnapshot();
97124
expect(component.find('FormControlLabel').length).toBeGreaterThan(0);
98-
expect(component.find('FormControlLabel').first().prop('label')).toEqual('Foo');
125+
expect(
126+
component
127+
.find('FormControlLabel')
128+
.first()
129+
.prop('label')
130+
).toEqual('Foo');
99131
});
100132

101133
it('should map onValueChange fn to onChange fn', async () => {
102-
103134
const cb = jest.fn();
104135
const component = mount(
105136
<BlueBaseApp plugins={[Plugin]}>
106-
107137
<Switch label="Foo" onValueChange={cb} />
108138
</BlueBaseApp>
109139
);
@@ -119,11 +149,9 @@ describe('Switch', () => {
119149
});
120150

121151
it('should map onValueChange fn to onChange fn with value', async () => {
122-
123152
const cb = jest.fn();
124153
const component = mount(
125154
<BlueBaseApp plugins={[Plugin]}>
126-
127155
<Switch label="Foo" value="foo" onValueChange={cb} />
128156
</BlueBaseApp>
129157
);
@@ -140,27 +168,25 @@ describe('Switch', () => {
140168
});
141169

142170
it('should pass onChange as is if available', async () => {
143-
144171
const cb = jest.fn();
145172

146173
const SWITCH = Switch as any;
147174

148175
const component = mount(
149176
<BlueBaseApp plugins={[Plugin]}>
150-
151177
<SWITCH label="Foo" onChange={cb} />
152178
</BlueBaseApp>
153179
);
154180
await waitForElement(component, SWITCH);
155-
const onChange = component.find('Switch').first().prop('onChange') as any;
181+
const onChange = component
182+
.find('Switch')
183+
.first()
184+
.prop('onChange') as any;
156185

157186
onChange('foo', true);
158187

159188
// expect(component).toMatchSnapshot();
160189
expect(cb).toBeCalledTimes(1);
161190
expect(cb).toBeCalledWith('foo', true);
162191
});
163-
164192
});
165-
166-

0 commit comments

Comments
 (0)