Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle1morel committed Dec 19, 2024
1 parent a2e2dc4 commit 92409dc
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 90 deletions.
2 changes: 1 addition & 1 deletion app/src/controllers/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ const controller = {
trackingId: null,
status: x.status ?? PermitStatus.NEW,
needed: x.needed ?? PermitNeeded.UNDER_INVESTIGATION,
statusLastVerified: x.statusLastVerified,
statusLastVerified: null,
issuedPermitId: null,
authStatus: x.authStatus ?? PermitAuthorizationStatus.NONE,
submittedDate: null,
Expand Down
28 changes: 12 additions & 16 deletions app/tests/unit/controllers/submission.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,20 +467,19 @@ describe('createSubmission', () => {
permitTypeId: 1,
trackingId: '123',
status: PermitStatus.APPLIED,
statusLastVerified: now
submittedDate: now
},
{
permitTypeId: 3,
trackingId: '456',
status: PermitStatus.APPLIED,
statusLastVerified: now
submittedDate: now
}
],
investigatePermits: [
{
permitTypeId: 12,
needed: PermitNeeded.UNDER_INVESTIGATION,
statusLastVerified: now
needed: PermitNeeded.UNDER_INVESTIGATION
}
]
},
Expand All @@ -506,7 +505,7 @@ describe('createSubmission', () => {
activityId: '00000000',
trackingId: '123',
status: PermitStatus.APPLIED,
statusLastVerified: now
submittedDate: now
})
);
expect(createPermitSpy).toHaveBeenNthCalledWith(
Expand All @@ -516,16 +515,15 @@ describe('createSubmission', () => {
activityId: '00000000',
trackingId: '456',
status: PermitStatus.APPLIED,
statusLastVerified: now
submittedDate: now
})
);
expect(createPermitSpy).toHaveBeenNthCalledWith(
3,
expect.objectContaining({
permitTypeId: 12,
activityId: '00000000',
needed: PermitNeeded.UNDER_INVESTIGATION,
statusLastVerified: now
needed: PermitNeeded.UNDER_INVESTIGATION
})
);
});
Expand Down Expand Up @@ -809,20 +807,19 @@ describe('submitDraft', () => {
permitTypeId: 1,
trackingId: '123',
status: PermitStatus.APPLIED,
statusLastVerified: now
submittedDate: now
},
{
permitTypeId: 3,
trackingId: '456',
status: PermitStatus.APPLIED,
statusLastVerified: now
submittedDate: now
}
],
investigatePermits: [
{
permitTypeId: 12,
needed: PermitNeeded.UNDER_INVESTIGATION,
statusLastVerified: now
needed: PermitNeeded.UNDER_INVESTIGATION
}
]
},
Expand All @@ -849,7 +846,7 @@ describe('submitDraft', () => {
activityId: '00000000',
trackingId: '123',
status: PermitStatus.APPLIED,
statusLastVerified: now
submittedDate: now
})
);
expect(createPermitSpy).toHaveBeenNthCalledWith(
Expand All @@ -859,16 +856,15 @@ describe('submitDraft', () => {
activityId: '00000000',
trackingId: '456',
status: PermitStatus.APPLIED,
statusLastVerified: now
submittedDate: now
})
);
expect(createPermitSpy).toHaveBeenNthCalledWith(
3,
expect.objectContaining({
permitTypeId: 12,
activityId: '00000000',
needed: PermitNeeded.UNDER_INVESTIGATION,
statusLastVerified: now
needed: PermitNeeded.UNDER_INVESTIGATION
})
);
});
Expand Down
14 changes: 7 additions & 7 deletions app/tests/unit/validators/appliedPermits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('appliedPermitsSchema', () => {
const appliedPermits = {
permitTypeId: '123AC!',
status: PermitStatus.NEW,
statusLastVerified: '2021-01-01',
submittedDate: '2021-01-01',
trackingId: 'test'
};
const result = appliedPermit.validate(appliedPermits);
Expand All @@ -16,7 +16,7 @@ describe('appliedPermitsSchema', () => {
it('should not accept null for permitTypeId', () => {
const appliedPermits = {
status: PermitStatus.APPLIED,
statusLastVerified: '2021-01-01',
submittedDate: '2021-01-01',
trackingId: 'test tracking id'
};
const result = appliedPermit.validate(appliedPermits);
Expand All @@ -27,7 +27,7 @@ describe('appliedPermitsSchema', () => {
const appliedPermits = {
permitTypeId: 123,
status: PermitStatus.COMPLETED,
statusLastVerified: '2021-01-01',
submittedDate: '2021-01-01',
trackingId: 'test'
};
const result = appliedPermit.validate(appliedPermits);
Expand All @@ -38,18 +38,18 @@ describe('appliedPermitsSchema', () => {
const appliedPermits = {
permitTypeId: 123,
status: 'Test',
statusLastVerified: '2021-01-01',
submittedDate: '2021-01-01',
trackingId: 'test'
};
const result = appliedPermit.validate(appliedPermits);
expect(result.error).toBeDefined();
});

it('should only accept a valid date for statusLastVerified', () => {
it('should only accept a valid date for submitted date', () => {
const appliedPermits = {
permitTypeId: 123,
status: PermitStatus.APPLIED,
statusLastVerified: 'not-a-date',
submittedDate: 'not-a-date',
trackingId: 'test'
};
const result = appliedPermit.validate(appliedPermits);
Expand All @@ -60,7 +60,7 @@ describe('appliedPermitsSchema', () => {
const appliedPermits = {
permitTypeId: 123,
status: PermitStatus.APPLIED,
statusLastVerified: new Date(Date.now() + 1000).toISOString(),
submittedDate: new Date(Date.now() + 1000).toISOString(),
trackingId: 'test'
};
const result = appliedPermit.validate(appliedPermits);
Expand Down
49 changes: 40 additions & 9 deletions frontend/src/components/housing/enquiry/EnquiryIntakeForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { contactValidator } from '@/validators';
import type { Ref } from 'vue';
import type { IInputEvent } from '@/interfaces';
import type { Submission } from '@/types';
import { omit } from '@/utils/utils';
// Props
const { enquiryId = undefined } = defineProps<{
Expand All @@ -49,7 +50,7 @@ const validationErrors: Ref<string[]> = ref([]);
// Form validation schema
const formSchema = object({
contacts: array().of(object(contactValidator)),
...contactValidator,
[IntakeFormCategory.BASIC]: object({
isRelated: string().required().oneOf(YES_NO_LIST).label('Related to existing application'),
relatedActivityId: string().when('isRelated', {
Expand Down Expand Up @@ -130,7 +131,12 @@ async function loadEnquiry() {
initialFormValues.value = {
activityId: response?.activityId,
enquiryId: response?.enquiryId,
contacts: response?.contacts,
contactFirstName: response?.contacts[0].firstName,
contactLastName: response?.contacts[0].lastName,
contactPhoneNumber: response?.contacts[0].phoneNumber,
contactEmail: response?.contacts[0].email,
contactApplicantRelationship: response?.contacts[0].contactApplicantRelationship,
contactPreference: response?.contacts[0].contactPreference,
basic: {
isRelated: response?.isRelated,
relatedActivityId: response?.relatedActivityId,
Expand Down Expand Up @@ -170,7 +176,32 @@ async function onSubmit(data: any) {
}
}
enquiryResponse = await enquiryService.createEnquiry(data);
// Convert contact fields into contacts array object then remove form keys from data
const enquiryData = omit(
{
...data,
contacts: [
{
firstName: data.contactFirstName,
lastName: data.contactLastName,
phoneNumber: data.contactPhoneNumber,
email: data.contactEmail,
contactApplicantRelationship: data.contactApplicantRelationship,
contactPreference: data.contactPreference
}
]
},
[
'contactFirstName',
'contactLastName',
'contactPhoneNumber',
'contactEmail',
'contactApplicantRelationship',
'contactPreference'
]
);
enquiryResponse = await enquiryService.createEnquiry(enquiryData);
if (enquiryResponse.data.activityId && enquiryResponse.data.enquiryId) {
formRef.value?.setFieldValue('activityId', enquiryResponse.data.activityId);
Expand Down Expand Up @@ -258,44 +289,44 @@ onBeforeMount(async () => {
<div class="formgrid grid">
<InputText
class="col-6"
:name="`contacts[0].firstName`"
name="contactFirstName"
label="First name"
:bold="false"
:disabled="!editable"
/>
<InputText
class="col-6"
:name="`contacts[0].lastName`"
name="contactLastName"
label="Last name"
:bold="false"
:disabled="!editable"
/>
<InputMask
class="col-6"
:name="`contacts[0].phoneNumber`"
name="contactPhoneNumber"
mask="(999) 999-9999"
label="Phone number"
:bold="false"
:disabled="!editable"
/>
<InputText
class="col-6"
:name="`contacts[0].email`"
name="contactEmail"
label="Email"
:bold="false"
:disabled="!editable"
/>
<Dropdown
class="col-6"
:name="`contacts[0].contactApplicantRelationship`"
name="contactApplicantRelationship"
label="Relationship to project"
:bold="false"
:disabled="!editable"
:options="PROJECT_RELATIONSHIP_LIST"
/>
<Dropdown
class="col-6"
:name="`contacts[0].contactPreference`"
:name="`contactPreference`"
label="Preferred contact method"
:bold="false"
:disabled="!editable"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ export const submissionIntakeSchema = object({
[IntakeFormCategory.APPLIED_PERMITS]: array().of(
object({
permitTypeId: number().required().label('Permit type'),
statusLastVerified: mixed()
submittedDate: mixed()
.test(
'verified-date',
'Verified date must be valid or empty',
'submitted-date',
'Submitted date must be valid or empty',
(val) => val instanceof Date || val === undefined
)
.label('Last verified date'),
.label('Submitted date'),
trackingId: string().max(255).nullable().label('Tracking ID')
})
),
Expand Down
49 changes: 24 additions & 25 deletions frontend/tests/unit/components/shasIntake/EnquiryIntakeForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ getActivityIds.mockResolvedValue({ data: ['someActivityid'] } as AxiosResponse);
getSubmissions.mockResolvedValue({ data: [{ activityId: 'someActivityid' }] } as AxiosResponse);

interface FormValues {
contacts: Array<Contact>;
contactFirstName: string;
contactLastName: string;
contactEmail: string;
contactPhoneNumber: string;
contactApplicantRelationship: ProjectRelationship;
contactPreference: ContactPreference;
basic: {
isRelated?: string;
relatedActivityId?: string;
Expand All @@ -51,18 +56,12 @@ interface FormValues {

function basicValidFormValues(): FormValues {
return {
contacts: [
{
contactId: '0',
userId: undefined,
firstName: 'testFirst',
lastName: 'testLast',
email: '[email protected]',
phoneNumber: '1234567890',
contactApplicantRelationship: ProjectRelationship.OWNER,
contactPreference: ContactPreference.EMAIL
}
],
contactFirstName: 'testFirst',
contactLastName: 'testLast',
contactEmail: '[email protected]',
contactPhoneNumber: '1234567890',
contactApplicantRelationship: ProjectRelationship.OWNER,
contactPreference: ContactPreference.EMAIL,
basic: {
isRelated: 'Yes',
enquiryDescription: 'test description',
Expand Down Expand Up @@ -127,12 +126,12 @@ describe('EnquiryIntakeForm', () => {
const wrapper = mount(EnquiryIntakeForm, wrapperSettings());
await flushPromises();

const firstNameInput = wrapper.get('[name="contacts[0].firstName"]');
const lastNameInput = wrapper.get('[name="contacts[0].lastName"]');
const phoneInput = wrapper.get('[name="contacts[0].phoneNumber"]');
const emailInput = wrapper.get('[name="contacts[0].email"]');
const relationsInput = wrapper.get('[name="contacts[0].contactApplicantRelationship"]');
const contactInput = wrapper.get('[name="contacts[0].contactPreference"]');
const firstNameInput = wrapper.get('[name="contactFirstName"]');
const lastNameInput = wrapper.get('[name="contactLastName"]');
const phoneInput = wrapper.get('[name="contactPhoneNumber"]');
const emailInput = wrapper.get('[name="contactEmail"]');
const relationsInput = wrapper.get('[name="contactApplicantRelationship"]');
const contactInput = wrapper.get('[name="contactPreference"]');
const relatedInput = wrapper.findAll('[name="basic.isRelated"]');

expect(firstNameInput.isVisible()).toBeTruthy();
Expand Down Expand Up @@ -287,12 +286,12 @@ describe('EnquiryIntakeForm', () => {
...basicValidFormValues()
};

modifiedFormValues[IntakeFormCategory.CONTACTS][0].email = 'bad@email';
modifiedFormValues.contactEmail = 'bad@email';
formRef.setValues(modifiedFormValues);

const result = await formRef?.validate();
expect(Object.keys(result.errors).length).toBe(1);
expect(result.errors[`${[IntakeFormCategory.CONTACTS]}[0].email`]).toBeTruthy();
expect(result.errors['contactEmail']).toBeTruthy();
});

it('generates missing first and last name missing error', async () => {
Expand All @@ -305,15 +304,15 @@ describe('EnquiryIntakeForm', () => {
...basicValidFormValues()
};

modifiedFormValues[IntakeFormCategory.CONTACTS][0].firstName = '';
modifiedFormValues[IntakeFormCategory.CONTACTS][0].lastName = '';
modifiedFormValues.contactFirstName = '';
modifiedFormValues.contactLastName = '';

formRef.setValues(modifiedFormValues);

const result = await formRef?.validate();
expect(Object.keys(result.errors).length).toBe(2);
expect(result.errors[`${[IntakeFormCategory.CONTACTS]}[0].firstName`]).toBeTruthy();
expect(result.errors[`${[IntakeFormCategory.CONTACTS]}[0].lastName`]).toBeTruthy();
expect(result.errors['contactFirstName']).toBeTruthy();
expect(result.errors['contactLastName']).toBeTruthy();
});

it('generates errors for isRelated', async () => {
Expand Down
Loading

0 comments on commit 92409dc

Please sign in to comment.