This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
validate-patient.test.ts
109 lines (89 loc) · 3.52 KB
/
validate-patient.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { addDays } from 'date-fns'
import validatePatient, { PatientValidationError } from '../../../patients/util/validate-patient'
import Patient from '../../../shared/model/Patient'
const patient: Patient = {
id: 'abc123',
sex: 'Male',
givenName: 'John',
dateOfBirth: '01/11/1988',
isApproximateDateOfBirth: false,
code: 'abc123',
index: '1',
carePlans: [],
careGoals: [],
bloodType: 'A+',
visits: [],
rev: 'asd',
createdAt: '01/01/2020',
updatedAt: '01/01/2020',
phoneNumbers: [],
emails: [],
addresses: [],
}
describe('validate patient', () => {
describe('PatientValidationError class', () => {
it('should count the amount of errors', () => {
const error = new PatientValidationError()
expect(error.count).toEqual(0)
error.fieldErrors.givenName = 'patient.errors.patientGivenNameFeedback'
error.fieldErrors.dateOfBirth = 'patient.errors.patientDateOfBirthFeedback'
error.fieldErrors.suffix = 'patient.errors.patientNumInSuffixFeedback'
expect(error.count).toEqual(3)
error.fieldErrors.prefix = 'patient.errors.patientNumInPrefixFeedback'
error.fieldErrors.familyName = 'patient.errors.patientNumInFamilyNameFeedback'
error.fieldErrors.preferredLanguage = 'patient.errors.patientNumInPreferredLanguageFeedback'
error.fieldErrors.emails = ['patient.errors.invalidEmail']
error.fieldErrors.phoneNumbers = ['patient.errors.invalidPhoneNumber']
expect(error.count).toEqual(8)
})
})
it('returns null when patient is valid', () => {
const error = validatePatient({ ...patient })
expect(error).toEqual(null)
})
it('should validate the patient required fields', () => {
const error = validatePatient({ ...patient, givenName: '' })
expect(error?.fieldErrors.givenName).toEqual('patient.errors.patientGivenNameFeedback')
expect(error?.count).toEqual(1)
})
it('should validate that the patient birthday is not a future date', () => {
const error = validatePatient({
...patient,
dateOfBirth: addDays(new Date(), 4).toISOString(),
})
expect(error?.fieldErrors.dateOfBirth).toEqual('patient.errors.patientDateOfBirthFeedback')
expect(error?.count).toEqual(1)
})
it('should validate that the patient phone number is a valid phone number', () => {
const error = validatePatient({
...patient,
phoneNumbers: [{ id: 'abc', value: 'not a phone number' }],
})
expect(error?.fieldErrors.phoneNumbers).toEqual(['patient.errors.invalidPhoneNumber'])
expect(error?.count).toEqual(1)
})
it('should validate that the patient email is a valid email', () => {
const error = validatePatient({
...patient,
emails: [{ id: 'abc', value: 'not a phone number' }],
})
expect(error?.fieldErrors.emails).toEqual(['patient.errors.invalidEmail'])
expect(error?.count).toEqual(1)
})
it('should validate fields that should only contian alpha characters', () => {
const error = validatePatient({
...patient,
suffix: 'A123',
familyName: 'B456',
prefix: 'C987',
preferredLanguage: 'D321',
})
expect(error?.fieldErrors.suffix).toEqual('patient.errors.patientNumInSuffixFeedback')
expect(error?.fieldErrors.familyName).toEqual('patient.errors.patientNumInFamilyNameFeedback')
expect(error?.fieldErrors.prefix).toEqual('patient.errors.patientNumInPrefixFeedback')
expect(error?.fieldErrors.preferredLanguage).toEqual(
'patient.errors.patientNumInPreferredLanguageFeedback',
)
expect(error?.count).toEqual(4)
})
})