@@ -3,9 +3,15 @@ import {assert} from 'chai';
3
3
import { shallow , mount } from 'enzyme' ;
4
4
import * as td from 'testdouble' ;
5
5
import Switch from '../../../packages/switch/index' ;
6
+ import { coerceForTesting } from '../helpers/types' ;
6
7
7
8
suite ( 'Switch' ) ;
8
9
10
+ const getAdapter = ( instance : Switch ) => {
11
+ // @ts -ignore
12
+ return instance . foundation . adapter_ ;
13
+ } ;
14
+
9
15
test ( 'creates foundation' , ( ) => {
10
16
const wrapper = shallow < Switch > ( < Switch /> ) ;
11
17
assert . exists ( wrapper . instance ( ) . foundation ) ;
@@ -47,42 +53,42 @@ test('has checked class when props.checked is true', () => {
47
53
48
54
test ( '#foundation.setChecked gets called when prop.checked updates' , ( ) => {
49
55
const wrapper = shallow < Switch > ( < Switch /> ) ;
50
- wrapper . instance ( ) . foundation . setChecked = td . func ( ) ;
56
+ wrapper . instance ( ) . foundation . setChecked = td . func < ( setChecked : boolean ) => null > ( ) ;
51
57
wrapper . setProps ( { checked : true } ) ;
52
58
td . verify ( wrapper . instance ( ) . foundation . setChecked ( true ) , { times : 1 } ) ;
53
59
} ) ;
54
60
55
61
test ( '#foundation.setDisabled gets called when prop.disabled updates' , ( ) => {
56
62
const wrapper = shallow < Switch > ( < Switch /> ) ;
57
- wrapper . instance ( ) . foundation . setDisabled = td . func ( ) ;
63
+ wrapper . instance ( ) . foundation . setDisabled = td . func < ( disabled : boolean ) => null > ( ) ;
58
64
wrapper . setProps ( { disabled : true } ) ;
59
65
td . verify ( wrapper . instance ( ) . foundation . setDisabled ( true ) , { times : 1 } ) ;
60
66
} ) ;
61
67
62
68
test ( '#componentWillUnmount destroys foundation' , ( ) => {
63
69
const wrapper = shallow < Switch > ( < Switch /> ) ;
64
70
const foundation = wrapper . instance ( ) . foundation ;
65
- foundation . destroy = td . func ( ) ;
71
+ foundation . destroy = td . func < ( ) => null > ( ) ;
66
72
wrapper . unmount ( ) ;
67
73
td . verify ( foundation . destroy ( ) , { times : 1 } ) ;
68
74
} ) ;
69
75
70
76
test ( '#adapter.addClass adds class to state.classList' , ( ) => {
71
77
const wrapper = shallow < Switch > ( < Switch /> ) ;
72
- wrapper . instance ( ) . foundation . adapter_ . addClass ( 'test-class-name' ) ;
78
+ getAdapter ( wrapper . instance ( ) ) . addClass ( 'test-class-name' ) ;
73
79
assert . isTrue ( wrapper . state ( ) . classList . has ( 'test-class-name' ) ) ;
74
80
} ) ;
75
81
76
82
test ( '#adapter.removeClass removes class from state.classList' , ( ) => {
77
83
const wrapper = shallow < Switch > ( < Switch /> ) ;
78
84
wrapper . setState ( { classList : new Set ( [ 'test-class-name' ] ) } ) ;
79
- wrapper . instance ( ) . foundation . adapter_ . removeClass ( 'test-class-name' ) ;
85
+ getAdapter ( wrapper . instance ( ) ) . removeClass ( 'test-class-name' ) ;
80
86
assert . isFalse ( wrapper . state ( ) . classList . has ( 'test-class-name' ) ) ;
81
87
} ) ;
82
88
83
89
test ( '#adapter.setNativeControlChecked updates state.nativeControlChecked' , ( ) => {
84
90
const wrapper = shallow < Switch > ( < Switch /> ) ;
85
- wrapper . instance ( ) . foundation . adapter_ . setNativeControlChecked ( true ) ;
91
+ getAdapter ( wrapper . instance ( ) ) . setNativeControlChecked ( true ) ;
86
92
assert . isTrue ( wrapper . state ( ) . nativeControlChecked ) ;
87
93
} ) ;
88
94
@@ -94,7 +100,7 @@ test('#state.nativeControlChecked updates NativeControl', () => {
94
100
95
101
test ( '#adapter.setNativeControlDisabled updates state.nativeControlDisabled' , ( ) => {
96
102
const wrapper = shallow < Switch > ( < Switch /> ) ;
97
- wrapper . instance ( ) . foundation . adapter_ . setNativeControlDisabled ( true ) ;
103
+ getAdapter ( wrapper . instance ( ) ) . setNativeControlDisabled ( true ) ;
98
104
assert . isTrue ( wrapper . state ( ) . nativeControlDisabled ) ;
99
105
} ) ;
100
106
@@ -116,12 +122,17 @@ test('calls foundation.handleChange in NativeControl props.onChange', () => {
116
122
const onChange = td . func ( ) as React . ChangeEventHandler < HTMLInputElement > ;
117
123
const wrapper = mount < Switch > ( < Switch onChange = { onChange } /> ) ;
118
124
const nativeControl = wrapper . find ( '.mdc-switch__native-control' ) ;
119
- const mockEvt = {
125
+ const mockEvt = coerceForTesting < React . ChangeEvent < HTMLInputElement > > ( {
120
126
target : {
121
127
checked : true ,
122
128
} ,
123
- } as React . ChangeEvent < HTMLInputElement > ;
124
- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
129
+ nativeEvent : {
130
+ target : {
131
+ checked : true ,
132
+ } ,
133
+ } ,
134
+ } ) ;
135
+ wrapper . instance ( ) . foundation . handleChange = td . func < ( evt : Event ) => null > ( ) ;
125
136
nativeControl . props ( ) . onChange ! ( mockEvt ) ;
126
- td . verify ( wrapper . instance ( ) . foundation . handleChange ( mockEvt ) , { times : 1 } ) ;
137
+ td . verify ( wrapper . instance ( ) . foundation . handleChange ( mockEvt . nativeEvent ) , { times : 1 } ) ;
127
138
} ) ;
0 commit comments