@@ -3,10 +3,16 @@ import {assert} from 'chai';
3
3
import { shallow } from 'enzyme' ;
4
4
import * as td from 'testdouble' ;
5
5
import { Checkbox } from '../../../packages/checkbox/index' ;
6
+ import { MDCCheckboxAdapter } from '@material/checkbox/adapter' ;
6
7
import { coerceForTesting } from '../helpers/types' ;
7
8
8
9
suite ( 'Checkbox' ) ;
9
10
11
+ const getAdapter = ( instance : Checkbox ) : MDCCheckboxAdapter => {
12
+ // @ts -ignore adapter_ is a protected property, we need to override it
13
+ return instance . foundation . adapter_ ;
14
+ } ;
15
+
10
16
test ( 'creates foundation' , ( ) => {
11
17
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
12
18
assert . exists ( wrapper . instance ( ) . foundation ) ;
@@ -34,12 +40,29 @@ test('has disabled class when props.disabled is true', () => {
34
40
) ;
35
41
} ) ;
36
42
43
+ test ( 'has disabled class when foundation calls setDisabled is true' , ( ) => {
44
+ const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
45
+ getAdapter ( wrapper . instance ( ) ) . setNativeControlDisabled ( true ) ;
46
+ wrapper . update ( ) ;
47
+ assert . isTrue (
48
+ wrapper . find ( '.mdc-checkbox' ) . hasClass ( 'mdc-checkbox--disabled' )
49
+ ) ;
50
+ } ) ;
51
+
37
52
test ( 'native control props.disabled is true when props.disabled is true' , ( ) => {
38
53
const wrapper = shallow ( < Checkbox disabled /> ) ;
39
54
const nativeControl = wrapper . childAt ( 0 ) ;
40
55
assert . isTrue ( nativeControl . props ( ) . disabled ) ;
41
56
} ) ;
42
57
58
+ test ( 'native control props.disabled when foundation calls setDisabled is true' , ( ) => {
59
+ const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
60
+ getAdapter ( wrapper . instance ( ) ) . setNativeControlDisabled ( true ) ;
61
+ wrapper . update ( ) ;
62
+ const nativeControl = wrapper . childAt ( 0 ) ;
63
+ assert . isTrue ( nativeControl . props ( ) . disabled ) ;
64
+ } ) ;
65
+
43
66
test ( 'native control props.checked is true when props.checked is true' , ( ) => {
44
67
const wrapper = shallow ( < Checkbox checked /> ) ;
45
68
const nativeControl = wrapper . childAt ( 0 ) ;
@@ -48,84 +71,80 @@ test('native control props.checked is true when props.checked is true', () => {
48
71
49
72
test ( '#foundation.handleChange gets called when prop.checked updates' , ( ) => {
50
73
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
51
- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
74
+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => null > ( ) ;
52
75
wrapper . setProps ( { checked : true } ) ;
53
76
td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
54
77
} ) ;
55
78
56
79
test ( '#foundation.handleChange gets called when prop.indeterminate updates' , ( ) => {
57
80
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
58
- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
81
+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => null > ( ) ;
59
82
wrapper . setProps ( { indeterminate : true } ) ;
60
83
td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
61
84
} ) ;
62
85
63
86
test ( '#foundation.setDisabled gets called when prop.disabled updates' , ( ) => {
64
87
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
65
- wrapper . instance ( ) . foundation . setDisabled = td . func ( ) ;
88
+ wrapper . instance ( ) . foundation . setDisabled = td . func < ( disabled : boolean ) => null > ( ) ;
66
89
wrapper . setProps ( { disabled : true } ) ;
67
90
td . verify ( wrapper . instance ( ) . foundation . setDisabled ( true ) , { times : 1 } ) ;
68
91
} ) ;
69
92
70
93
test ( '#componentWillUnmount destroys foundation' , ( ) => {
71
94
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
72
95
const foundation = wrapper . instance ( ) . foundation ;
73
- foundation . destroy = td . func ( ) ;
96
+ foundation . destroy = td . func < ( ) => void > ( ) ;
74
97
wrapper . unmount ( ) ;
75
98
td . verify ( foundation . destroy ( ) , { times : 1 } ) ;
76
99
} ) ;
77
100
78
101
test ( '#adapter.addClass adds class to state.classList' , ( ) => {
79
102
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
80
- wrapper . instance ( ) . foundation . adapter_ . addClass ( 'test-class-name' ) ;
103
+ getAdapter ( wrapper . instance ( ) ) . addClass ( 'test-class-name' ) ;
81
104
assert . isTrue ( wrapper . state ( ) . classList . has ( 'test-class-name' ) ) ;
82
105
} ) ;
83
106
84
107
test ( '#adapter.removeClass removes class from state.classList' , ( ) => {
85
108
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
86
109
wrapper . setState ( { classList : new Set ( [ 'test-class-name' ] ) } ) ;
87
- wrapper . instance ( ) . foundation . adapter_ . removeClass ( 'test-class-name' ) ;
110
+ getAdapter ( wrapper . instance ( ) ) . removeClass ( 'test-class-name' ) ;
88
111
assert . isFalse ( wrapper . state ( ) . classList . has ( 'test-class-name' ) ) ;
89
112
} ) ;
90
113
91
114
test ( '#adapter.isChecked returns state.checked if true' , ( ) => {
92
115
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
93
116
wrapper . setState ( { checked : true } ) ;
94
- assert . isTrue ( wrapper . instance ( ) . foundation . adapter_ . isChecked ( ) ) ;
117
+ assert . isTrue ( getAdapter ( wrapper . instance ( ) ) . isChecked ( ) ) ;
95
118
} ) ;
96
119
97
120
test ( '#adapter.isChecked returns state.checked if false' , ( ) => {
98
121
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
99
122
wrapper . setState ( { checked : false } ) ;
100
- assert . isFalse ( wrapper . instance ( ) . foundation . adapter_ . isChecked ( ) ) ;
123
+ assert . isFalse ( getAdapter ( wrapper . instance ( ) ) . isChecked ( ) ) ;
101
124
} ) ;
102
125
103
126
test ( '#adapter.isIndeterminate returns state.indeterminate if true' , ( ) => {
104
127
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
105
128
wrapper . setState ( { indeterminate : true } ) ;
106
- assert . isTrue ( wrapper . instance ( ) . foundation . adapter_ . isIndeterminate ( ) ) ;
129
+ assert . isTrue ( getAdapter ( wrapper . instance ( ) ) . isIndeterminate ( ) ) ;
107
130
} ) ;
108
131
109
132
test ( '#adapter.isIndeterminate returns state.indeterminate if false' , ( ) => {
110
133
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
111
134
wrapper . setState ( { indeterminate : false } ) ;
112
- assert . isFalse ( wrapper . instance ( ) . foundation . adapter_ . isIndeterminate ( ) ) ;
135
+ assert . isFalse ( getAdapter ( wrapper . instance ( ) ) . isIndeterminate ( ) ) ;
113
136
} ) ;
114
137
115
138
test ( '#adapter.setNativeControlAttr sets aria-checked state' , ( ) => {
116
139
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
117
- wrapper
118
- . instance ( )
119
- . foundation . adapter_ . setNativeControlAttr ( 'aria-checked' , true ) ;
120
- assert . isTrue ( wrapper . state ( ) [ 'aria-checked' ] ) ;
140
+ getAdapter ( wrapper . instance ( ) ) . setNativeControlAttr ( 'aria-checked' , 'true' ) ;
141
+ assert . equal ( wrapper . state ( ) [ 'aria-checked' ] , 'true' ) ;
121
142
} ) ;
122
143
123
144
test ( '#adapter.removeNativeControlAttr sets aria-checked state as false' , ( ) => {
124
145
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
125
- wrapper . setState ( { 'aria-checked' : true } ) ;
126
- wrapper
127
- . instance ( )
128
- . foundation . adapter_ . removeNativeControlAttr ( 'aria-checked' ) ;
146
+ wrapper . setState ( { 'aria-checked' : 'true' } ) ;
147
+ getAdapter ( wrapper . instance ( ) ) . removeNativeControlAttr ( 'aria-checked' ) ;
129
148
assert . isFalse ( wrapper . state ( ) [ 'aria-checked' ] ) ;
130
149
} ) ;
131
150
@@ -148,7 +167,7 @@ test('calls foundation.handleChange in native control props.onChange', () => {
148
167
indeterminate : false ,
149
168
} ,
150
169
} ;
151
- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
170
+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => void > ( ) ;
152
171
nativeControl . simulate ( 'change' , mockEvt ) ;
153
172
td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
154
173
} ) ;
0 commit comments