1+ import { expect } from "chai" ;
2+ import { createVueField , trigger } from "../util" ;
3+
4+ import Vue from "vue" ;
5+ import FieldSwitch from "src/fields/fieldSwitch.vue" ;
6+
7+ Vue . component ( "FieldSwitch" , FieldSwitch ) ;
8+
9+ let el , vm , field ;
10+
11+ function createField ( schema = { } , model = null , disabled = false , options ) {
12+ [ el , vm , field ] = createVueField ( "fieldSwitch" , schema , model , disabled , options ) ;
13+ }
14+
15+ describe ( "FieldSwitch.vue" , ( ) => {
16+
17+ describe ( "check template" , ( ) => {
18+ let schema = {
19+ type : "switch" ,
20+ label : "Status" ,
21+ model : "status"
22+ } ;
23+ let model = { status : true } ;
24+ let input ;
25+
26+ before ( ( ) => {
27+ createField ( schema , model , false ) ;
28+ input = el . querySelector ( "input" ) ;
29+ } ) ;
30+
31+ it ( "should contain a checkbox element" , ( ) => {
32+ expect ( field ) . to . be . exist ;
33+ expect ( field . $el ) . to . be . exist ;
34+
35+ expect ( input ) . to . be . defined ;
36+ expect ( input . type ) . to . be . equal ( "checkbox" ) ;
37+ expect ( input . disabled ) . to . be . false ;
38+ } ) ;
39+
40+ it ( "should contain the value" , ( done ) => {
41+ vm . $nextTick ( ( ) => {
42+ expect ( input . checked ) . to . be . true ;
43+ done ( ) ;
44+ } ) ;
45+ } ) ;
46+
47+ it ( "should contain the default On/Off texts" , ( ) => {
48+ let span = field . $el . querySelector ( "span.label" ) ;
49+ expect ( span . getAttribute ( "data-on" ) ) . to . be . equal ( "On" ) ;
50+ expect ( span . getAttribute ( "data-off" ) ) . to . be . equal ( "Off" ) ;
51+ } ) ;
52+
53+ it ( "should set disabled" , ( done ) => {
54+ field . disabled = true ;
55+ vm . $nextTick ( ( ) => {
56+ expect ( input . disabled ) . to . be . true ;
57+ done ( ) ;
58+ } ) ;
59+ } ) ;
60+
61+ it ( "input value should be the model value after changed" , ( done ) => {
62+ model . status = false ;
63+ vm . $nextTick ( ( ) => {
64+ expect ( input . checked ) . to . be . false ;
65+ done ( ) ;
66+ } ) ;
67+
68+ } ) ;
69+
70+ it ( "model value should be the input value if changed" , ( done ) => {
71+ input . checked = true ;
72+ trigger ( input , "change" ) ;
73+
74+ vm . $nextTick ( ( ) => {
75+ expect ( model . status ) . to . be . true ;
76+ done ( ) ;
77+ } ) ;
78+
79+ } ) ;
80+
81+ } ) ;
82+
83+ describe ( "check template with custom On/Off texts" , ( ) => {
84+ let schema = {
85+ type : "switch" ,
86+ label : "Status" ,
87+ model : "status" ,
88+ textOn : "Yes" ,
89+ textOff : "No"
90+ } ;
91+ let model = { status : true } ;
92+
93+ before ( ( ) => {
94+ createField ( schema , model , false ) ;
95+ } ) ;
96+
97+ it ( "check attributes" , ( ) => {
98+ let span = field . $el . querySelector ( "span.label" ) ;
99+ expect ( span . getAttribute ( "data-on" ) ) . to . be . equal ( "Yes" ) ;
100+ expect ( span . getAttribute ( "data-off" ) ) . to . be . equal ( "No" ) ;
101+ } ) ;
102+
103+ } ) ;
104+
105+ } ) ;
0 commit comments