1+ import { mount , VueWrapper } from "@vue/test-utils" ;
2+ import EmptyApp from "./components/EmptyApp.vue" ;
3+ import { defineComponent } from "vue" ;
4+ import { FormField , Form } from "./../../src/index" ;
5+ import STORE from "../../src/config/store" ;
6+
7+ const name = "color"
8+ const cssElementInputCheckbox = '.element-input-checkbox'
9+ function defineCheckboxRadio ( ) {
10+ return defineComponent ( {
11+ template : `<div><form-field type = "single-checkbox" name = "${ name } " label = "Select color" required/></div>` ,
12+ components : { FormField}
13+ } )
14+ }
15+ function defaultMount ( component = defineCheckboxRadio ( ) ) {
16+ return mount ( EmptyApp , {
17+ slots : {
18+ default : component
19+ } ,
20+ attachTo : document . body
21+ } )
22+ }
23+
24+ describe ( "Input single checkbox" , ( ) => {
25+ let app : VueWrapper < any >
26+ let form : Form
27+ beforeEach ( ( ) => {
28+ app = defaultMount ( ) ;
29+ form = ( app . vm as any ) . form
30+ } )
31+
32+ test ( "Single radio show display the label" , ( ) => {
33+ expect ( app . text ( ) ) . toBe ( "Select color" )
34+ } )
35+ test ( "should change value after click" , async ( ) => {
36+ await app . find ( cssElementInputCheckbox ) . trigger ( 'click' )
37+ expect ( form . getValueByName ( name ) ) . toBe ( true )
38+
39+ await app . find ( cssElementInputCheckbox ) . trigger ( 'click' )
40+ expect ( form . getValueByName ( name ) ) . toBe ( false )
41+
42+ await app . find ( cssElementInputCheckbox ) . trigger ( 'click' )
43+ expect ( form . getValueByName ( name ) ) . toBe ( true )
44+ } )
45+ test ( "Should show error" , async ( ) => {
46+ form . validate ( ) ;
47+ await app . vm . $nextTick ( )
48+ expect ( app . find ( '.element-input-checkbox-button_error' ) . exists ( ) ) . toBe ( true ) ;
49+ expect ( app . text ( ) ) . toBe ( "Select color" + STORE . requiredMessage )
50+ } )
51+ test ( "Single radio should not trigger on click when stay in disabled status" , async ( ) => {
52+ form . setValues ( {
53+ [ name ] : true
54+ } )
55+ form . disable ( ) ;
56+ await app . vm . $nextTick ( )
57+ expect ( app . find ( '.element-input-checkbox-button_disabled' ) . exists ( ) ) . toBe ( true ) ;
58+
59+ await app . find ( cssElementInputCheckbox ) . trigger ( 'click' )
60+ expect ( form . getValueByName ( name ) ) . toBe ( true )
61+ } )
62+
63+ test ( "Press space should toggle value" , async ( ) => {
64+
65+ const input = app . find ( cssElementInputCheckbox ) ;
66+
67+ await input . trigger ( 'keyup.space' )
68+ expect ( form . getValueByName ( name ) ) . toBe ( true )
69+
70+ await input . trigger ( 'keyup.space' )
71+ expect ( form . getValueByName ( name ) ) . toBe ( false )
72+
73+ await input . trigger ( 'keyup.space' )
74+ expect ( form . getValueByName ( name ) ) . toBe ( true )
75+
76+ } )
77+ test ( "Press enter should toggle value" , async ( ) => {
78+ const input = app . find ( cssElementInputCheckbox ) ;
79+
80+ await input . trigger ( 'keyup.enter' )
81+ expect ( form . getValueByName ( name ) ) . toBe ( true )
82+
83+ await input . trigger ( 'keyup.enter' )
84+ expect ( form . getValueByName ( name ) ) . toBe ( false )
85+
86+ await input . trigger ( 'keyup.enter' )
87+ expect ( form . getValueByName ( name ) ) . toBe ( true )
88+ } )
89+ test ( "If values was provided, modelValue should be or first or second value" , async ( ) => {
90+ const app = defaultMount ( defineComponent ( {
91+ template : `<div><form-field
92+ type = "single-checkbox" name = "${ name } " label = "Select color" required
93+ :values = "['good', 'bad']"
94+ /></div>` ,
95+ components : { FormField}
96+ } ) )
97+ const form = ( app . vm as any ) . form
98+
99+ const input = app . get ( cssElementInputCheckbox )
100+
101+ await input . trigger ( 'keyup.enter' )
102+ expect ( form . getValueByName ( name ) ) . toBe ( "good" )
103+
104+ await input . trigger ( 'keyup.enter' )
105+ expect ( form . getValueByName ( name ) ) . toBe ( "bad" )
106+
107+ await input . trigger ( 'keyup.enter' )
108+ expect ( form . getValueByName ( name ) ) . toBe ( "good" )
109+ } )
110+ } )
0 commit comments