1+ import { describe , expect , it } from '@jest/globals' ;
2+ import Checkbox from './checkbox' ;
3+ import CheckboxConditionsHandler from './checkboxConditionsHandler' ;
4+ import CheckboxConditionValidator from './checkboxConditionValidator' ;
5+
6+ describe ( 'Checkbox' , ( ) => {
7+ const fakeCheckBoxHandler = {
8+ init : jest . fn ( ) ,
9+ dispatchUpdateEvent ( condition : any ) : boolean { return true ; } ,
10+ addValueChangeListener ( field : FieldInterface ) : void { } ,
11+ getConditions ( ) : Condition [ ] { return [ ] ; } ,
12+ getIsDisabled ( ) : boolean { return false ; } ,
13+ validate ( ) : void { }
14+ } as unknown as CheckboxConditionsHandler ;
15+ const fakeCheckBoxValidator = {
16+ init : jest . fn ( ) ,
17+ validate ( condition : Condition ) : boolean { return true ; }
18+ } as unknown as CheckboxConditionValidator ;
19+ const field = document . createElement ( 'div' ) ;
20+ field . innerHTML = `<input type="checkbox" name="testField" value="1" checked><input type="checkbox" name="testField2" value="2">` ;
21+ const choices = field . querySelectorAll ( 'input[type="checkbox"]' ) as NodeListOf < HTMLInputElement > ;
22+
23+ const checkBox = new Checkbox (
24+ field ,
25+ choices ,
26+ 'testField' ,
27+ fakeCheckBoxValidator ,
28+ fakeCheckBoxHandler
29+ )
30+
31+ it ( 'init() initializes the conditions handler and validator' , ( ) => {
32+ checkBox . init ( { } as any ) ;
33+ expect ( fakeCheckBoxHandler . init ) . toHaveBeenCalledWith ( checkBox , { } as any ) ;
34+ expect ( fakeCheckBoxValidator . init ) . toHaveBeenCalledWith ( checkBox ) ;
35+ } ) ;
36+
37+ it ( 'getName() returns field name' , ( ) => {
38+ expect ( checkBox . getName ( ) ) . toBe ( 'testField' ) ;
39+ } ) ;
40+
41+ it ( 'getConditionsHandler() returns the conditions handler' , ( ) => {
42+ expect ( checkBox . getConditionsHandler ( ) ) . toBe ( fakeCheckBoxHandler ) ;
43+ } ) ;
44+
45+ it ( 'getConditionValidator() returns the validation handler' , ( ) => {
46+ expect ( checkBox . getConditionValidator ( ) ) . toBe ( fakeCheckBoxValidator ) ;
47+ } ) ;
48+
49+ it ( 'getChoices() returns the available choices' , ( ) => {
50+ expect ( checkBox . getChoices ( ) ) . toBe ( choices ) ;
51+ } ) ;
52+
53+ it ( 'getField() returns the available choices' , ( ) => {
54+ expect ( checkBox . getField ( ) ) . toBe ( field ) ;
55+ } ) ;
56+
57+ it ( 'getSelectedChoices() returns selected choices values' , ( ) => {
58+ expect ( checkBox . getSelectedChoices ( ) ) . toStrictEqual ( [ "1" ] ) ;
59+ } ) ;
60+ } ) ;
0 commit comments