Skip to content

Commit

Permalink
Updated integration test on bulk update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben committed Jan 28, 2025
1 parent f5a45e4 commit fb18dd5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 48 deletions.
27 changes: 0 additions & 27 deletions services/121-service/test/helpers/assert.helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MessageTemplateEntity } from '@121-service/src/notifications/message-template/message-template.entity';
import { RegistrationStatusEnum } from '@121-service/src/registration/enum/registration-status.enum';
import { RegistrationEntity } from '@121-service/src/registration/registration.entity';
import { LocalizedString } from '@121-service/src/shared/types/localized-string.type';

export function processMessagePlaceholders(
messageTemplates: MessageTemplateEntity[],
Expand All @@ -21,29 +20,3 @@ export function processMessagePlaceholders(

return processedTemplate;
}

export function assertRegistrationBulkUpdate(
patchData: Record<
string,
string | undefined | boolean | number | null | LocalizedString
>,
updatedRegistration: Record<
string,
string | undefined | boolean | number | null
>,
originalRegistration: Record<
string,
string | undefined | boolean | number | null
>,
): void {
for (const key in patchData) {
expect(JSON.stringify(updatedRegistration[key])).toBe(
JSON.stringify(patchData[key]),
);
}
for (const key in originalRegistration) {
if (patchData[key] === undefined && key !== 'name') {
expect(updatedRegistration[key]).toStrictEqual(originalRegistration[key]);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SeedScript } from '@121-service/src/scripts/enum/seed-script.enum';
import { waitFor } from '@121-service/src/utils/waitFor.helper';
import { assertRegistrationBulkUpdate } from '@121-service/test/helpers/assert.helper';
import { patchProgram } from '@121-service/test/helpers/program.helper';
import {
bulkUpdateRegistrationsCSV,
Expand All @@ -12,6 +11,22 @@ import {
resetDB,
} from '@121-service/test/helpers/utility.helper';

function filterUnchangedProperties(
originalData: Record<string, unknown>,
patchData: Record<string, unknown>,
): Record<string, any> {
const unchangedProperties: Record<string, unknown> = {};

for (const key of Object.keys(originalData)) {
// registration.name is a special case as it is a derived property so it cannot be patched
if (!(key in patchData) && key !== 'name') {
unchangedProperties[key] = originalData[key];
}
}

return unchangedProperties;
}

describe('Update attribute of multiple PAs via Bulk update', () => {
const programIdOcw = 3;

Expand All @@ -34,7 +49,7 @@ describe('Update attribute of multiple PAs via Bulk update', () => {
fullName: 'updated name1',
addressStreet: 'newStreet1',
addressHouseNumber: '2',
addressHouseNumberAddition: '',
addressHouseNumberAddition: null,
preferredLanguage: 'ar',
paymentAmountMultiplier: 2,
whatsappPhoneNumber: '14155238880',
Expand Down Expand Up @@ -96,16 +111,25 @@ describe('Update attribute of multiple PAs via Bulk update', () => {
const pa2AfterPatch = searchByReferenceIdAfterPatchPa2.body.data[0];

// Assert
assertRegistrationBulkUpdate(
registrationDataThatWillChangePa1,
pa1AfterPatch,
// Explicit assertions for pa1 using patch data
expect(pa1AfterPatch).toMatchObject(registrationDataThatWillChangePa1);

// Explicit assertions for pa2 using patch data
expect(pa2AfterPatch).toMatchObject(registrationDataThatWillChangePa2);

// Ensure unchanged fields remain the same
const dataThatStaysTheSamePa1 = filterUnchangedProperties(
pa1BeforePatch,
registrationDataThatWillChangePa1,
);
assertRegistrationBulkUpdate(
registrationDataThatWillChangePa2,
pa2AfterPatch,

const dataThatStaysTheSamePa2 = filterUnchangedProperties(
pa2BeforePatch,
registrationDataThatWillChangePa2,
);

expect(pa1AfterPatch).toMatchObject(dataThatStaysTheSamePa1);
expect(pa2AfterPatch).toMatchObject(dataThatStaysTheSamePa2);
});

it('Should bulk update chosen FSP and validate changed records', async () => {
Expand Down Expand Up @@ -173,24 +197,33 @@ describe('Update attribute of multiple PAs via Bulk update', () => {
const pa2AfterPatch = searchByReferenceIdAfterPatchPa2.body.data[0];

// Assert
assertRegistrationBulkUpdate(
registrationDataThatWillChangePa1,
pa1AfterPatch,
// Explicit assertions for pa1 using patch data
expect(pa1AfterPatch).toMatchObject(registrationDataThatWillChangePa1);

// Explicit assertions for pa2 using patch data
expect(pa2AfterPatch).toMatchObject(registrationDataThatWillChangePa2);

// Ensure unchanged fields remain the same
const dataThatStaysTheSamePa1 = filterUnchangedProperties(
pa1BeforePatch,
registrationDataThatWillChangePa1,
);
assertRegistrationBulkUpdate(
registrationDataThatWillChangePa2,
pa2AfterPatch,

const dataThatStaysTheSamePa2 = filterUnchangedProperties(
pa2BeforePatch,
registrationDataThatWillChangePa2,
);

expect(pa1AfterPatch).toMatchObject(dataThatStaysTheSamePa1);
expect(pa2AfterPatch).toMatchObject(dataThatStaysTheSamePa2);
});

it('Should bulk update if phoneNumber column is empty and program is configured as allowing empty phone number', async () => {
const registrationDataThatWillChangePa1 = {
fullName: 'updated name1',
addressStreet: 'newStreet1',
addressHouseNumber: '2',
addressHouseNumberAddition: '',
addressHouseNumberAddition: null,
preferredLanguage: 'ar',
paymentAmountMultiplier: 2,
phoneNumber: '14155238880',
Expand Down Expand Up @@ -255,15 +288,24 @@ describe('Update attribute of multiple PAs via Bulk update', () => {
const pa2AfterPatch = searchByReferenceIdAfterPatchPa2.body.data[0];

// Assert
assertRegistrationBulkUpdate(
registrationDataThatWillChangePa1,
pa1AfterPatch,
// Explicit assertions for pa1 using patch data
expect(pa1AfterPatch).toMatchObject(registrationDataThatWillChangePa1);

// Explicit assertions for pa2 using patch data
expect(pa2AfterPatch).toMatchObject(registrationDataThatWillChangePa2);

const dataThatStaysTheSamePa1 = filterUnchangedProperties(
pa1BeforePatch,
registrationDataThatWillChangePa1,
);
assertRegistrationBulkUpdate(
registrationDataThatWillChangePa2,
pa2AfterPatch,

const dataThatStaysTheSamePa2 = filterUnchangedProperties(
pa2BeforePatch,
registrationDataThatWillChangePa2,
);

// Ensure unchanged fields remain the same
expect(pa1AfterPatch).toMatchObject(dataThatStaysTheSamePa1);
expect(pa2AfterPatch).toMatchObject(dataThatStaysTheSamePa2);
});
});

0 comments on commit fb18dd5

Please sign in to comment.