@@ -3,10 +3,16 @@ import {assert} from 'chai';
33import { shallow } from 'enzyme' ;
44import * as td from 'testdouble' ;
55import { Checkbox } from '../../../packages/checkbox/index' ;
6+ import { MDCCheckboxAdapter } from '@material/checkbox/adapter' ;
67import { coerceForTesting } from '../helpers/types' ;
78
89suite ( 'Checkbox' ) ;
910
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+
1016test ( 'creates foundation' , ( ) => {
1117 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
1218 assert . exists ( wrapper . instance ( ) . foundation ) ;
@@ -34,12 +40,29 @@ test('has disabled class when props.disabled is true', () => {
3440 ) ;
3541} ) ;
3642
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+
3752test ( 'native control props.disabled is true when props.disabled is true' , ( ) => {
3853 const wrapper = shallow ( < Checkbox disabled /> ) ;
3954 const nativeControl = wrapper . childAt ( 0 ) ;
4055 assert . isTrue ( nativeControl . props ( ) . disabled ) ;
4156} ) ;
4257
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+
4366test ( 'native control props.checked is true when props.checked is true' , ( ) => {
4467 const wrapper = shallow ( < Checkbox checked /> ) ;
4568 const nativeControl = wrapper . childAt ( 0 ) ;
@@ -48,84 +71,80 @@ test('native control props.checked is true when props.checked is true', () => {
4871
4972test ( '#foundation.handleChange gets called when prop.checked updates' , ( ) => {
5073 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
51- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
74+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => null > ( ) ;
5275 wrapper . setProps ( { checked : true } ) ;
5376 td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
5477} ) ;
5578
5679test ( '#foundation.handleChange gets called when prop.indeterminate updates' , ( ) => {
5780 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
58- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
81+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => null > ( ) ;
5982 wrapper . setProps ( { indeterminate : true } ) ;
6083 td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
6184} ) ;
6285
6386test ( '#foundation.setDisabled gets called when prop.disabled updates' , ( ) => {
6487 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
65- wrapper . instance ( ) . foundation . setDisabled = td . func ( ) ;
88+ wrapper . instance ( ) . foundation . setDisabled = td . func < ( disabled : boolean ) => null > ( ) ;
6689 wrapper . setProps ( { disabled : true } ) ;
6790 td . verify ( wrapper . instance ( ) . foundation . setDisabled ( true ) , { times : 1 } ) ;
6891} ) ;
6992
7093test ( '#componentWillUnmount destroys foundation' , ( ) => {
7194 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
7295 const foundation = wrapper . instance ( ) . foundation ;
73- foundation . destroy = td . func ( ) ;
96+ foundation . destroy = td . func < ( ) => void > ( ) ;
7497 wrapper . unmount ( ) ;
7598 td . verify ( foundation . destroy ( ) , { times : 1 } ) ;
7699} ) ;
77100
78101test ( '#adapter.addClass adds class to state.classList' , ( ) => {
79102 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
80- wrapper . instance ( ) . foundation . adapter_ . addClass ( 'test-class-name' ) ;
103+ getAdapter ( wrapper . instance ( ) ) . addClass ( 'test-class-name' ) ;
81104 assert . isTrue ( wrapper . state ( ) . classList . has ( 'test-class-name' ) ) ;
82105} ) ;
83106
84107test ( '#adapter.removeClass removes class from state.classList' , ( ) => {
85108 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
86109 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' ) ;
88111 assert . isFalse ( wrapper . state ( ) . classList . has ( 'test-class-name' ) ) ;
89112} ) ;
90113
91114test ( '#adapter.isChecked returns state.checked if true' , ( ) => {
92115 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
93116 wrapper . setState ( { checked : true } ) ;
94- assert . isTrue ( wrapper . instance ( ) . foundation . adapter_ . isChecked ( ) ) ;
117+ assert . isTrue ( getAdapter ( wrapper . instance ( ) ) . isChecked ( ) ) ;
95118} ) ;
96119
97120test ( '#adapter.isChecked returns state.checked if false' , ( ) => {
98121 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
99122 wrapper . setState ( { checked : false } ) ;
100- assert . isFalse ( wrapper . instance ( ) . foundation . adapter_ . isChecked ( ) ) ;
123+ assert . isFalse ( getAdapter ( wrapper . instance ( ) ) . isChecked ( ) ) ;
101124} ) ;
102125
103126test ( '#adapter.isIndeterminate returns state.indeterminate if true' , ( ) => {
104127 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
105128 wrapper . setState ( { indeterminate : true } ) ;
106- assert . isTrue ( wrapper . instance ( ) . foundation . adapter_ . isIndeterminate ( ) ) ;
129+ assert . isTrue ( getAdapter ( wrapper . instance ( ) ) . isIndeterminate ( ) ) ;
107130} ) ;
108131
109132test ( '#adapter.isIndeterminate returns state.indeterminate if false' , ( ) => {
110133 const wrapper = shallow < Checkbox > ( < Checkbox /> ) ;
111134 wrapper . setState ( { indeterminate : false } ) ;
112- assert . isFalse ( wrapper . instance ( ) . foundation . adapter_ . isIndeterminate ( ) ) ;
135+ assert . isFalse ( getAdapter ( wrapper . instance ( ) ) . isIndeterminate ( ) ) ;
113136} ) ;
114137
115138test ( '#adapter.setNativeControlAttr sets aria-checked state' , ( ) => {
116139 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' ) ;
121142} ) ;
122143
123144test ( '#adapter.removeNativeControlAttr sets aria-checked state as false' , ( ) => {
124145 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' ) ;
129148 assert . isFalse ( wrapper . state ( ) [ 'aria-checked' ] ) ;
130149} ) ;
131150
@@ -148,7 +167,7 @@ test('calls foundation.handleChange in native control props.onChange', () => {
148167 indeterminate : false ,
149168 } ,
150169 } ;
151- wrapper . instance ( ) . foundation . handleChange = td . func ( ) ;
170+ wrapper . instance ( ) . foundation . handleChange = td . func < ( ) => void > ( ) ;
152171 nativeControl . simulate ( 'change' , mockEvt ) ;
153172 td . verify ( wrapper . instance ( ) . foundation . handleChange ( ) , { times : 1 } ) ;
154173} ) ;
0 commit comments