1
1
import * as React from "react" ;
2
- import { Stores , Actions } from "simplr-forms-core" ;
2
+ import { Stores , Actions , Contracts as FormsCoreContracts } from "simplr-forms-core" ;
3
3
import * as Sinon from "sinon" ;
4
4
5
5
import { ContainsValidator } from "../../src/validators/index" ;
6
6
import { FormStoreSubscriber } from "../../src/subscribers/form-store-subscriber" ;
7
7
8
+ const { FieldValidationType } = FormsCoreContracts ;
9
+
8
10
class MySubscriber extends FormStoreSubscriber {
9
- protected onValueChanged ( action : Actions . ValueChanged ) {
10
- return new Promise < any > ( ( resolve , reject ) => {
11
- super . onValueChanged ( action ) . then ( resolve ) . catch ( reject ) ;
12
- } ) ;
11
+ public ValidateField (
12
+ fieldId : string ,
13
+ value : FormsCoreContracts . FieldValue ,
14
+ validationType : FormsCoreContracts . FieldValidationType
15
+ ) {
16
+ return super . ValidateField ( fieldId , value , validationType ) ;
13
17
}
14
18
}
15
19
@@ -19,7 +23,7 @@ describe("FormStoreSubscriber", () => {
19
23
sandbox = Sinon . sandbox . create ( ) ;
20
24
} ) ;
21
25
22
- afterEach ( function ( ) {
26
+ afterEach ( ( ) => {
23
27
sandbox . restore ( ) ;
24
28
} ) ;
25
29
@@ -29,12 +33,14 @@ describe("FormStoreSubscriber", () => {
29
33
30
34
expect ( callback . called ) . toEqual ( false ) ;
31
35
36
+ expect ( formStore . listeners ( Actions . FieldRegistered ) . length ) . toBe ( 0 ) ;
32
37
expect ( formStore . listeners ( Actions . PropsChanged ) . length ) . toBe ( 0 ) ;
33
38
expect ( formStore . listeners ( Actions . ValueChanged ) . length ) . toBe ( 0 ) ;
34
39
35
40
new FormStoreSubscriber ( formStore ) ;
36
41
expect ( callback . called ) . toEqual ( true ) ;
37
42
43
+ expect ( formStore . listeners ( Actions . FieldRegistered ) . length ) . toBe ( 1 ) ;
38
44
expect ( formStore . listeners ( Actions . PropsChanged ) . length ) . toBe ( 1 ) ;
39
45
expect ( formStore . listeners ( Actions . ValueChanged ) . length ) . toBe ( 1 ) ;
40
46
} ) ;
@@ -51,82 +57,107 @@ describe("FormStoreSubscriber", () => {
51
57
52
58
} ) ;
53
59
54
- it ( "MUST validate when value changed without an error" , async ( done ) => {
60
+ it ( "MUST validate when specific validationType flag is present without error" , async ( done ) => {
55
61
const fieldId = "field-id" ;
56
- const nextValue = "valid value" ;
62
+ const initialValue = "initial valid value" ;
57
63
const errorMessage = "error message" ;
58
64
59
65
const validatorValidateCallback = sandbox . spy ( ContainsValidator . prototype , "Validate" ) ;
60
- const fieldChildren = [ < ContainsValidator value = "valid" errorMessage = { errorMessage } /> ] ;
66
+ const fieldChildren = [ < ContainsValidator value = "valid" error = { errorMessage } /> ] ;
61
67
const formStore = new Stores . FormStore ( "form-id" ) ;
62
68
const formStoreValidateCallback = sandbox . spy ( Stores . FormStore . prototype , "Validate" ) ;
63
- const onValueChangedCallback = sandbox . spy ( MySubscriber . prototype , "onValueChanged" ) ;
64
- new MySubscriber ( formStore ) ;
65
-
66
- formStore . RegisterField ( fieldId , "initial" , { name : "field-name" , children : fieldChildren } ) ;
67
- formStore . ValueChanged ( fieldId , nextValue ) ;
68
-
69
- const [ onValueChangedPromise ] = onValueChangedCallback . returnValues ;
70
-
71
- try {
72
- expect ( onValueChangedPromise ) . toBeDefined ( ) ;
73
- expect ( onValueChangedPromise . then ) . toBeDefined ( ) ;
74
- } catch ( err ) {
75
- done . fail ( err ) ;
76
- }
69
+ const subscriber = new MySubscriber ( formStore ) ;
77
70
78
- await onValueChangedPromise ;
71
+ const fieldProps : FormsCoreContracts . FieldStateProps = {
72
+ name : "field-name" ,
73
+ children : fieldChildren ,
74
+ validationType : FieldValidationType . OnValueChange
75
+ } ;
79
76
77
+ formStore . RegisterField ( fieldId , initialValue , fieldProps ) ;
80
78
try {
79
+ await subscriber . ValidateField ( fieldId , initialValue , FieldValidationType . OnValueChange ) ;
81
80
expect ( formStoreValidateCallback . called ) . toBe ( true ) ;
82
81
expect ( validatorValidateCallback . called ) . toBe ( true ) ;
83
82
expect ( formStore . GetField ( fieldId ) . Error ) . toBeUndefined ( ) ;
83
+
84
+ done ( ) ;
84
85
} catch ( error ) {
85
86
done . fail ( error ) ;
86
87
}
87
- done ( ) ;
88
88
} ) ;
89
89
90
- it ( "MUST validate when value changed with an error" , async ( done ) => {
90
+ it ( "MUST validate when specific validationType flag is present with error" , async ( done ) => {
91
91
const fieldId = "field-id" ;
92
- const nextValue = "next value" ;
92
+ const initialValue = "initial value" ;
93
93
const errorMessage = "error message" ;
94
94
95
95
const validatorValidateCallback = sandbox . spy ( ContainsValidator . prototype , "Validate" ) ;
96
- const fieldChildren = [ < ContainsValidator value = "ok" errorMessage = { errorMessage } /> ] ;
96
+ const fieldChildren = [ < ContainsValidator value = "valid" error = { errorMessage } /> ] ;
97
97
const formStore = new Stores . FormStore ( "form-id" ) ;
98
98
const formStoreValidateCallback = sandbox . spy ( Stores . FormStore . prototype , "Validate" ) ;
99
- const onValueChangedCallback = sandbox . spy ( MySubscriber . prototype , "onValueChanged" ) ;
100
- new MySubscriber ( formStore ) ;
101
-
102
- formStore . RegisterField ( fieldId , "initial" , { name : "field-name" , children : fieldChildren } ) ;
103
- formStore . ValueChanged ( fieldId , nextValue ) ;
99
+ const subscriber = new MySubscriber ( formStore ) ;
104
100
105
- const [ onValueChangedPromise ] = onValueChangedCallback . returnValues ;
101
+ const fieldProps : FormsCoreContracts . FieldStateProps = {
102
+ name : "field-name" ,
103
+ children : fieldChildren ,
104
+ validationType : FieldValidationType . OnValueChange
105
+ } ;
106
106
107
+ formStore . RegisterField ( fieldId , initialValue , fieldProps ) ;
107
108
try {
108
- expect ( onValueChangedPromise ) . toBeDefined ( ) ;
109
- expect ( onValueChangedPromise . then ) . toBeDefined ( ) ;
110
- expect ( formStore . GetField ( fieldId ) . Validating ) . toBe ( true ) ;
111
- } catch ( err ) {
112
- done . fail ( err ) ;
113
- }
114
-
115
- await onValueChangedPromise ;
116
-
117
- try {
118
- expect ( formStore . GetField ( fieldId ) . Validating ) . toBe ( false ) ;
109
+ await subscriber . ValidateField ( fieldId , initialValue , FieldValidationType . OnValueChange ) ;
119
110
expect ( formStoreValidateCallback . called ) . toBe ( true ) ;
120
111
expect ( validatorValidateCallback . called ) . toBe ( true ) ;
121
112
expect ( formStore . GetField ( fieldId ) . Error ) . toBeDefined ( ) ;
122
- expect ( formStore . GetField ( fieldId ) . Error ! . Message ) . toEqual ( errorMessage ) ;
113
+
114
+ done ( ) ;
123
115
} catch ( error ) {
124
116
done . fail ( error ) ;
125
117
}
126
- done ( ) ;
127
118
} ) ;
128
119
129
- // TODO: OnPropsChanged tests
130
- it ( "validate when props changed without an error" , ( ) => { } ) ;
131
- it ( "validate when props changed with an error" , ( ) => { } ) ;
120
+ it ( "MUST NOT validate when specific validationType flag is missing" , ( ) => {
121
+ const fieldId = "field-id" ;
122
+ const initialValue = "initial value" ;
123
+ const errorMessage = "error message" ;
124
+
125
+ const validatorValidateCallback = sandbox . spy ( ContainsValidator . prototype , "Validate" ) ;
126
+ const fieldChildren = [ < ContainsValidator value = "valid" error = { errorMessage } /> ] ;
127
+ const formStore = new Stores . FormStore ( "form-id" ) ;
128
+ const formStoreValidateCallback = sandbox . spy ( Stores . FormStore . prototype , "Validate" ) ;
129
+ const subscriber = new MySubscriber ( formStore ) ;
130
+
131
+ const fieldProps : FormsCoreContracts . FieldStateProps = {
132
+ name : "field-name" ,
133
+ children : fieldChildren ,
134
+ validationType : FieldValidationType . OnPropsChange
135
+ } ;
136
+
137
+ formStore . RegisterField ( fieldId , initialValue , fieldProps ) ;
138
+ // ValidateField should skip this validation because OnValueChange flag is missing
139
+ subscriber . ValidateField ( fieldId , initialValue , FieldValidationType . OnValueChange ) ;
140
+
141
+ expect ( formStoreValidateCallback . called ) . toBe ( false ) ;
142
+ expect ( validatorValidateCallback . called ) . toBe ( false ) ;
143
+ expect ( formStore . GetField ( fieldId ) . Error ) . toBeUndefined ( ) ;
144
+ } ) ;
145
+
146
+ it ( "MUST NOT validate when field props is undefined" , ( ) => {
147
+ const fieldId = "field-id" ;
148
+ const initialValue = "initial value" ;
149
+
150
+ const validatorValidateCallback = sandbox . spy ( ContainsValidator . prototype , "Validate" ) ;
151
+ const formStore = new Stores . FormStore ( "form-id" ) ;
152
+ const formStoreValidateCallback = sandbox . spy ( Stores . FormStore . prototype , "Validate" ) ;
153
+ const subscriber = new MySubscriber ( formStore ) ;
154
+
155
+ // Validation is skipped because props are undefined
156
+ formStore . RegisterField ( fieldId , initialValue , undefined ) ;
157
+ subscriber . ValidateField ( fieldId , initialValue , FieldValidationType . OnValueChange ) ;
158
+
159
+ expect ( formStoreValidateCallback . called ) . toBe ( false ) ;
160
+ expect ( validatorValidateCallback . called ) . toBe ( false ) ;
161
+ expect ( formStore . GetField ( fieldId ) . Error ) . toBeUndefined ( ) ;
162
+ } ) ;
132
163
} ) ;
0 commit comments