11import * as React from "react" ;
22
3+ import { FormStore } from "@simplr/react-forms/stores" ;
4+
35import * as Validation from "../src/utils/validation" ;
46import { ContainsValidator } from "../src/validators/index" ;
7+ import { ValidationFieldErrorTemplate } from "../src/contracts" ;
8+ import * as Sinon from "sinon" ;
9+ import { FormError } from "@simplr/react-forms/contracts" ;
10+
11+ it ( "Validates value without errors" , async done => {
12+ try {
13+ const errorMessage = "error message" ;
14+ const validValue = "p-ok" ;
15+ const fieldId = "field-id" ;
16+ const formStore = new FormStore ( "form-id" ) ;
17+
18+ const children = [
19+ < ContainsValidator value = "ok" error = { errorMessage } /> ,
20+ < input type = "text" />
21+ ] ;
22+ const validationPromise = Validation . ValidateField ( children , validValue , fieldId , formStore ) ;
23+ let validationResponse ;
24+
25+ validationResponse = await validationPromise ;
26+ expect ( validationResponse ) . toBeUndefined ( ) ;
27+
28+ done ( ) ;
29+ } catch ( error ) {
30+ done . fail ( error ) ;
31+ }
32+ } ) ;
533
6- it ( "Validate value without errors " , async ( done ) => {
34+ it ( "Validates value with error " , async done => {
735 const errorMessage = "error message" ;
8- const validValue = "p-ok" ;
36+ const invalidValue = "invalid-value" ;
37+ const fieldId = "field-id" ;
38+ const formStore = new FormStore ( "form-id" ) ;
39+
940 const children = [
1041 < ContainsValidator value = "ok" error = { errorMessage } /> ,
1142 < input type = "text" />
1243 ] ;
13- const validationPromise = Validation . ValidateField ( children , validValue ) ;
14- let validationResponse ;
44+ const validationPromise = Validation . ValidateField ( children , invalidValue , fieldId , formStore ) ;
45+ let validationResponse : FormError | undefined = undefined ;
1546
1647 try {
17- validationResponse = await validationPromise ;
48+ await validationPromise ;
1849 } catch ( error ) {
19- done . fail ( error ) ;
50+ validationResponse = error ;
2051 }
2152
2253 try {
23- expect ( validationResponse ) . toBeUndefined ( ) ;
54+ expect ( validationResponse ) . toBeDefined ( ) ;
55+ expect ( validationResponse ! . Message ) . toBe ( errorMessage ) ;
2456 done ( ) ;
2557 } catch ( error ) {
2658 done . fail ( error ) ;
2759 }
2860} ) ;
2961
30- it ( "Validate value with error" , async ( done ) => {
31- const errorMessage = "error message" ;
32- const invvalidValue = "invalid-value" ;
62+ it ( "Validates value with error template" , async done => {
63+ const invalidValue = "invalid-value" ;
64+ const fieldId = "field-id" ;
65+ const formStore = new FormStore ( "form-id" ) ;
66+ const errorTemplate : ValidationFieldErrorTemplate = ( tFieldId , tFormStore ) =>
67+ `${ tFieldId } error template` ;
68+ const spyCallback = Sinon . spy ( errorTemplate ) ;
69+
3370 const children = [
34- < ContainsValidator value = "ok" error = { errorMessage } /> ,
71+ < ContainsValidator value = "ok" error = { spyCallback } /> ,
3572 < input type = "text" />
3673 ] ;
37- const validationPromise = Validation . ValidateField ( children , invvalidValue ) ;
38- let validationResponse ;
74+ const validationPromise = Validation . ValidateField ( children , invalidValue , fieldId , formStore ) ;
75+ let validationResponse : FormError | undefined = undefined ;
3976
4077 try {
4178 await validationPromise ;
@@ -44,7 +81,9 @@ it("Validate value with error", async (done) => {
4481 }
4582
4683 try {
47- expect ( validationResponse ) . toBe ( errorMessage ) ;
84+ expect ( spyCallback . called ) . toBe ( true ) ;
85+ expect ( validationResponse ) . toBeDefined ( ) ;
86+ expect ( validationResponse ! . Message ) . toBe ( errorTemplate ( fieldId , formStore ) ) ;
4887 done ( ) ;
4988 } catch ( error ) {
5089 done . fail ( error ) ;
0 commit comments