@@ -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 ) ;
@@ -48,84 +54,80 @@ test('native control props.checked is true when props.checked is true', () => {
48
54
49
55
test ( '#foundation.handleChange gets called when prop.checked updates' , ( ) => {
50
56
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
51
- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
57
+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => null > ( ) ;
52
58
wrapper . setProps ( { checked : true } ) ;
53
59
td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
54
60
} ) ;
55
61
56
62
test ( '#foundation.handleChange gets called when prop.indeterminate updates' , ( ) => {
57
63
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
58
- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
64
+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => null > ( ) ;
59
65
wrapper . setProps ( { indeterminate : true } ) ;
60
66
td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
61
67
} ) ;
62
68
63
69
test ( '#foundation.setDisabled gets called when prop.disabled updates' , ( ) => {
64
70
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
65
- wrapper . instance ( ) . foundation . setDisabled = td . func ( ) ;
71
+ wrapper . instance ( ) . foundation . setDisabled = td . func < ( disabled : boolean ) => null > ( ) ;
66
72
wrapper . setProps ( { disabled : true } ) ;
67
73
td . verify ( wrapper . instance ( ) . foundation . setDisabled ( true ) , { times : 1 } ) ;
68
74
} ) ;
69
75
70
76
test ( '#componentWillUnmount destroys foundation' , ( ) => {
71
77
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
72
78
const foundation = wrapper . instance ( ) . foundation ;
73
- foundation . destroy = td . func ( ) ;
79
+ foundation . destroy = td . func < ( ) => void > ( ) ;
74
80
wrapper . unmount ( ) ;
75
81
td . verify ( foundation . destroy ( ) , { times : 1 } ) ;
76
82
} ) ;
77
83
78
84
test ( '#adapter.addClass adds class to state.classList' , ( ) => {
79
85
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
80
- wrapper . instance ( ) . foundation . adapter_ . addClass ( 'test-class-name' ) ;
86
+ getAdapter ( wrapper . instance ( ) ) . addClass ( 'test-class-name' ) ;
81
87
assert . isTrue ( wrapper . state ( ) . classList . has ( 'test-class-name' ) ) ;
82
88
} ) ;
83
89
84
90
test ( '#adapter.removeClass removes class from state.classList' , ( ) => {
85
91
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
86
92
wrapper . setState ( { classList : new Set ( [ 'test-class-name' ] ) } ) ;
87
- wrapper . instance ( ) . foundation . adapter_ . removeClass ( 'test-class-name' ) ;
93
+ getAdapter ( wrapper . instance ( ) ) . removeClass ( 'test-class-name' ) ;
88
94
assert . isFalse ( wrapper . state ( ) . classList . has ( 'test-class-name' ) ) ;
89
95
} ) ;
90
96
91
97
test ( '#adapter.isChecked returns state.checked if true' , ( ) => {
92
98
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
93
99
wrapper . setState ( { checked : true } ) ;
94
- assert . isTrue ( wrapper . instance ( ) . foundation . adapter_ . isChecked ( ) ) ;
100
+ assert . isTrue ( getAdapter ( wrapper . instance ( ) ) . isChecked ( ) ) ;
95
101
} ) ;
96
102
97
103
test ( '#adapter.isChecked returns state.checked if false' , ( ) => {
98
104
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
99
105
wrapper . setState ( { checked : false } ) ;
100
- assert . isFalse ( wrapper . instance ( ) . foundation . adapter_ . isChecked ( ) ) ;
106
+ assert . isFalse ( getAdapter ( wrapper . instance ( ) ) . isChecked ( ) ) ;
101
107
} ) ;
102
108
103
109
test ( '#adapter.isIndeterminate returns state.indeterminate if true' , ( ) => {
104
110
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
105
111
wrapper . setState ( { indeterminate : true } ) ;
106
- assert . isTrue ( wrapper . instance ( ) . foundation . adapter_ . isIndeterminate ( ) ) ;
112
+ assert . isTrue ( getAdapter ( wrapper . instance ( ) ) . isIndeterminate ( ) ) ;
107
113
} ) ;
108
114
109
115
test ( '#adapter.isIndeterminate returns state.indeterminate if false' , ( ) => {
110
116
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
111
117
wrapper . setState ( { indeterminate : false } ) ;
112
- assert . isFalse ( wrapper . instance ( ) . foundation . adapter_ . isIndeterminate ( ) ) ;
118
+ assert . isFalse ( getAdapter ( wrapper . instance ( ) ) . isIndeterminate ( ) ) ;
113
119
} ) ;
114
120
115
121
test ( '#adapter.setNativeControlAttr sets aria-checked state' , ( ) => {
116
122
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
117
- wrapper
118
- . instance ( )
119
- . foundation . adapter_ . setNativeControlAttr ( 'aria-checked' , true ) ;
120
- assert . isTrue ( wrapper . state ( ) [ 'aria-checked' ] ) ;
123
+ getAdapter ( wrapper . instance ( ) ) . setNativeControlAttr ( 'aria-checked' , 'true' ) ;
124
+ assert . equal ( wrapper . state ( ) [ 'aria-checked' ] , 'true' ) ;
121
125
} ) ;
122
126
123
127
test ( '#adapter.removeNativeControlAttr sets aria-checked state as false' , ( ) => {
124
128
const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
125
- wrapper . setState ( { 'aria-checked' : true } ) ;
126
- wrapper
127
- . instance ( )
128
- . foundation . adapter_ . removeNativeControlAttr ( 'aria-checked' ) ;
129
+ wrapper . setState ( { 'aria-checked' : 'true' } ) ;
130
+ getAdapter ( wrapper . instance ( ) ) . removeNativeControlAttr ( 'aria-checked' ) ;
129
131
assert . isFalse ( wrapper . state ( ) [ 'aria-checked' ] ) ;
130
132
} ) ;
131
133
@@ -148,7 +150,7 @@ test('calls foundation.handleChange in native control props.onChange', () => {
148
150
indeterminate : false ,
149
151
} ,
150
152
} ;
151
- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
153
+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => void > ( ) ;
152
154
nativeControl . simulate ( 'change' , mockEvt ) ;
153
155
td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
154
156
} ) ;
0 commit comments