Skip to content

Commit 970ddba

Browse files
authored
Test participant validation schema 482 (#492)
* Yup visit schema validation test script. * Participant schema seems pretty thorough.
1 parent a139d77 commit 970ddba

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

frontend/src/validation/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ const visitSchema = Yup.object().shape({
109109
const participantSchema = Yup.object().shape({
110110
first_name: Yup.string()
111111
.required()
112+
.min(2)
112113
.max(50),
113114
last_name: Yup.string()
114115
.required()
116+
.min(2)
115117
.max(100),
116118
date_of_birth: Yup.date()
117119
.required()
@@ -132,7 +134,7 @@ const participantSchema = Yup.object().shape({
132134
.notRequired()
133135
.when("is_insured", {
134136
is: false,
135-
then: Yup.string().matches(/^\d+$/, { excludeEmptyString: true }),
137+
then: Yup.string().matches(/^\s*$/, { excludeEmptyString: true }),
136138
otherwise: Yup.string().matches(/^\d+$/),
137139
}),
138140
})
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { validateForm, PARTICIPANT_SCHEMA } from "../src/validation/index"
2+
3+
//SEP_ID is alphanumeric going forward
4+
let MOCK_SCHEMA = {
5+
first_name: "ted",
6+
last_name: "nougat",
7+
date_of_birth: new Date("1989-07-13"),
8+
pp_id: "12345",
9+
sep_id: "1234",
10+
maiden_name: "Haroldson",
11+
is_insured: true,
12+
insurer: "7",
13+
}
14+
15+
describe("Participant Schema Validation", () => {
16+
it("should not return any errors initially", async () => {
17+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
18+
expect(validationErrors.length).toEqual(0)
19+
})
20+
21+
it("participant needs a first name", async () => {
22+
MOCK_SCHEMA.first_name = null
23+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
24+
expect(validationErrors[0].name).toEqual("first_name")
25+
expect(validationErrors.length).toEqual(1)
26+
})
27+
28+
it("participant first name should be a minimum of 2 characters long", async () => {
29+
MOCK_SCHEMA.first_name = "t"
30+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
31+
expect(validationErrors[0].name).toEqual("first_name")
32+
expect(validationErrors.length).toEqual(1)
33+
MOCK_SCHEMA.first_name = "ted"
34+
})
35+
36+
it("participant needs a last name", async () => {
37+
MOCK_SCHEMA.last_name = null
38+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
39+
expect(validationErrors[0].name).toEqual("last_name")
40+
expect(validationErrors.length).toEqual(1)
41+
})
42+
43+
it("participant last name should be a minimum of 2 characters long", async () => {
44+
MOCK_SCHEMA.last_name = "n"
45+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
46+
expect(validationErrors[0].name).toEqual("last_name")
47+
expect(validationErrors.length).toEqual(1)
48+
MOCK_SCHEMA.last_name = "nougat"
49+
})
50+
51+
it("should return an error when date of birth is earlier than 12/31/1899, 31/12/1899 for our Euro friends", async () => {
52+
MOCK_SCHEMA.date_of_birth = new Date("1899-12-30")
53+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
54+
expect(validationErrors[0].name).toEqual("date_of_birth")
55+
expect(validationErrors.length).toEqual(1)
56+
})
57+
58+
it("should return an error when date of birth is in the future", async () => {
59+
MOCK_SCHEMA.date_of_birth = new Date("2100-01-01")
60+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
61+
expect(validationErrors[0].name).toEqual("date_of_birth")
62+
expect(validationErrors.length).toEqual(1)
63+
MOCK_SCHEMA.date_of_birth = new Date("1989-07-13")
64+
})
65+
66+
it("should return an error when ppid is null", async () => {
67+
MOCK_SCHEMA.pp_id = null
68+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
69+
expect(validationErrors[0].name).toEqual("pp_id")
70+
expect(validationErrors.length).toEqual(1)
71+
})
72+
73+
it("should return an error when ppid is not string", async () => {
74+
MOCK_SCHEMA.pp_id = 12345
75+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
76+
expect(validationErrors[0].name).toEqual("pp_id")
77+
expect(validationErrors.length).toEqual(1)
78+
})
79+
80+
it("should return an error when ppid is less than 4 characters long", async () => {
81+
MOCK_SCHEMA.pp_id = "123"
82+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
83+
expect(validationErrors[0].name).toEqual("pp_id")
84+
expect(validationErrors.length).toEqual(1)
85+
MOCK_SCHEMA.pp_id = "12345"
86+
})
87+
88+
it("should return an error when sep_id is null", async () => {
89+
MOCK_SCHEMA.sep_id = null
90+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
91+
expect(validationErrors[0].name).toEqual("sep_id")
92+
expect(validationErrors.length).toEqual(1)
93+
})
94+
95+
it("should return an error when sep_id is not string", async () => {
96+
MOCK_SCHEMA.sep_id = 12345
97+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
98+
expect(validationErrors[0].name).toEqual("sep_id")
99+
expect(validationErrors.length).toEqual(1)
100+
MOCK_SCHEMA.sep_id = "1234"
101+
})
102+
103+
it("should return an error when is_insured is not bool", async () => {
104+
MOCK_SCHEMA.is_insured = "true"
105+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
106+
expect(validationErrors[0].name).toEqual("is_insured")
107+
expect(validationErrors.length).toEqual(1)
108+
})
109+
110+
it("should return an error when is_insured is true and insurer is not string", async () => {
111+
MOCK_SCHEMA.is_insured = true
112+
MOCK_SCHEMA.insurer = 8
113+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
114+
expect(validationErrors[0].name).toEqual("insurer")
115+
expect(validationErrors.length).toEqual(1)
116+
})
117+
118+
it("should return an error when is_insured is true and insurer is a string without digits", async () => {
119+
MOCK_SCHEMA.insurer = "a"
120+
const validationErrors = await validateForm(MOCK_SCHEMA, PARTICIPANT_SCHEMA)
121+
expect(validationErrors[0].name).toEqual("insurer")
122+
expect(validationErrors.length).toEqual(1)
123+
})
124+
})

0 commit comments

Comments
 (0)