Skip to content

Commit f3ab69c

Browse files
committed
split form-actions
1 parent 2792a7f commit f3ab69c

File tree

7 files changed

+279
-250
lines changed

7 files changed

+279
-250
lines changed

frontend/src/__tests__/app/message-plans/page.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @jest-environment node
33
*/
4-
import { getRoutingConfigs, countRoutingConfigs } from '@utils/form-actions';
4+
import { getRoutingConfigs, countRoutingConfigs } from '@utils/message-plans';
55
import {
66
type MessagePlansProps,
77
MessagePlans,
@@ -11,7 +11,7 @@ import MessagePlansPage, { generateMetadata } from '@app/message-plans/page';
1111
import { ReactElement } from 'react';
1212
import { RoutingConfig } from 'nhs-notify-backend-client';
1313

14-
jest.mock('@utils/form-actions');
14+
jest.mock('@utils/message-plans');
1515
jest.mock('@utils/server-features');
1616

1717
const countRoutingConfigsMock = jest.mocked(countRoutingConfigs);

frontend/src/__tests__/utils/form-actions.test.ts

Lines changed: 1 addition & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,13 @@ import {
1515
setTemplateToDeleted,
1616
setTemplateToSubmitted,
1717
requestTemplateProof,
18-
getRoutingConfigs,
19-
countRoutingConfigs,
2018
} from '@utils/form-actions';
2119
import { getSessionServer } from '@utils/amplify-utils';
22-
import { RoutingConfig, TemplateDto } from 'nhs-notify-backend-client';
20+
import { TemplateDto } from 'nhs-notify-backend-client';
2321
import { templateApiClient } from 'nhs-notify-backend-client/src/template-api-client';
24-
import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client';
2522

2623
const mockedTemplateClient = jest.mocked(templateApiClient);
2724
const authIdTokenServerMock = jest.mocked(getSessionServer);
28-
const routingConfigurationApiClientMock = jest.mocked(
29-
routingConfigurationApiClient
30-
);
3125

3226
jest.mock('@utils/amplify-utils');
3327
jest.mock('nhs-notify-backend-client/src/template-api-client');
@@ -703,193 +697,4 @@ describe('form-actions', () => {
703697
);
704698
});
705699
});
706-
707-
describe('getRoutingConfigs', () => {
708-
test('should throw error when no token', async () => {
709-
authIdTokenServerMock.mockReset();
710-
authIdTokenServerMock.mockResolvedValueOnce({
711-
accessToken: undefined,
712-
clientId: undefined,
713-
});
714-
715-
await expect(getRoutingConfigs()).rejects.toThrow(
716-
'Failed to get access token'
717-
);
718-
});
719-
720-
test('should return empty array when calling the API fails', async () => {
721-
routingConfigurationApiClientMock.list.mockResolvedValueOnce({
722-
error: {
723-
errorMeta: { code: 400, description: 'Bad request' },
724-
},
725-
});
726-
727-
const response = await getRoutingConfigs();
728-
729-
expect(response.length).toBe(0);
730-
});
731-
732-
test('should return a list of routing configs - order by createdAt and then id', async () => {
733-
const fields = {
734-
status: 'DRAFT',
735-
name: 'Routing config',
736-
updatedAt: '2021-01-01T00:00:00.000Z',
737-
campaignId: 'campaignId',
738-
clientId: 'clientId',
739-
cascade: [
740-
{
741-
channel: 'EMAIL',
742-
channelType: 'primary',
743-
defaultTemplateId: 'id',
744-
cascadeGroups: ['standard'],
745-
},
746-
],
747-
cascadeGroupOverrides: [{ name: 'standard' }],
748-
} satisfies Omit<RoutingConfig, 'id' | 'createdAt'>;
749-
750-
const routingConfigs = [
751-
{
752-
...fields,
753-
id: 'a487ed49-e2f7-4871-ac8d-0c6c682c71f5',
754-
createdAt: '2022-01-01T00:00:00.000Z',
755-
},
756-
{
757-
...fields,
758-
id: '8f5157fe-72d7-4a9c-818f-77c128ec8197',
759-
createdAt: '2020-01-01T00:00:00.000Z',
760-
},
761-
{
762-
...fields,
763-
id: '9be9d25f-81d8-422a-a85c-2fa9019cde1e',
764-
createdAt: '2021-01-01T00:00:00.000Z',
765-
},
766-
{
767-
...fields,
768-
id: '1cfdd62d-9eca-4f15-9772-1937d4524c37',
769-
createdAt: '2021-01-01T00:00:00.000Z',
770-
},
771-
{
772-
...fields,
773-
id: '18da6158-07ef-455c-9c31-1a4d78a133cf',
774-
createdAt: '2021-01-01T00:00:00.000Z',
775-
},
776-
{
777-
...fields,
778-
id: '87fb5cbf-708d-49c3-9360-3e37efdc5278',
779-
createdAt: '2021-01-01T00:00:00.000Z',
780-
},
781-
{
782-
...fields,
783-
id: '0d6408fd-57ea-42f2-aae1-ed9614b67068',
784-
createdAt: '2021-01-01T00:00:00.000Z',
785-
},
786-
];
787-
788-
// a48... is the newest, 8f5... is the oldest.
789-
// the others all have the same createdAt.
790-
const expectedOrder = [
791-
'a487ed49-e2f7-4871-ac8d-0c6c682c71f5',
792-
'0d6408fd-57ea-42f2-aae1-ed9614b67068',
793-
'18da6158-07ef-455c-9c31-1a4d78a133cf',
794-
'1cfdd62d-9eca-4f15-9772-1937d4524c37',
795-
'87fb5cbf-708d-49c3-9360-3e37efdc5278',
796-
'9be9d25f-81d8-422a-a85c-2fa9019cde1e',
797-
'8f5157fe-72d7-4a9c-818f-77c128ec8197',
798-
];
799-
800-
routingConfigurationApiClientMock.list.mockResolvedValueOnce({
801-
data: routingConfigs,
802-
});
803-
804-
const response = await getRoutingConfigs();
805-
806-
const actualOrder = [];
807-
for (const routingConfig of response) {
808-
actualOrder.push(routingConfig.id);
809-
}
810-
811-
expect(actualOrder).toEqual(expectedOrder);
812-
});
813-
814-
test('invalid routing configs are not listed', async () => {
815-
routingConfigurationApiClientMock.list.mockResolvedValueOnce({
816-
data: [
817-
{
818-
status: 'DRAFT',
819-
name: 'Routing config',
820-
updatedAt: '2021-01-01T00:00:00.000Z',
821-
campaignId: 'campaignId',
822-
clientId: 'clientId',
823-
cascade: [],
824-
cascadeGroupOverrides: [{ name: 'standard' }],
825-
id: 'a487ed49-e2f7-4871-ac8d-0c6c682c71f5',
826-
createdAt: '2022-01-01T00:00:00.000Z',
827-
},
828-
],
829-
});
830-
831-
const response = await getRoutingConfigs();
832-
833-
expect(response).toEqual([]);
834-
});
835-
});
836-
837-
describe('countRoutingConfigs', () => {
838-
test('should throw error when no token', async () => {
839-
authIdTokenServerMock.mockReset();
840-
authIdTokenServerMock.mockResolvedValueOnce({
841-
accessToken: undefined,
842-
clientId: undefined,
843-
});
844-
845-
await expect(countRoutingConfigs('DRAFT')).rejects.toThrow(
846-
'Failed to get access token'
847-
);
848-
});
849-
850-
test('should return 0 when calling the API fails', async () => {
851-
routingConfigurationApiClientMock.count.mockResolvedValueOnce({
852-
error: {
853-
errorMeta: { code: 400, description: 'Bad request' },
854-
},
855-
});
856-
857-
const response = await countRoutingConfigs('DRAFT');
858-
859-
expect(response).toBe(0);
860-
});
861-
862-
test('should return count of routing configurations for status', async () => {
863-
// Note: we're doing this here because we call `getSessionServer` twice
864-
// and it's only mocked-out once by default.
865-
authIdTokenServerMock.mockResolvedValue({
866-
accessToken: 'token',
867-
clientId: 'client1',
868-
});
869-
870-
routingConfigurationApiClientMock.count
871-
.mockResolvedValueOnce({
872-
data: { count: 1 },
873-
})
874-
.mockResolvedValueOnce({
875-
data: { count: 5 },
876-
});
877-
878-
const draftCount = await countRoutingConfigs('DRAFT');
879-
const completedCount = await countRoutingConfigs('COMPLETED');
880-
881-
expect(draftCount).toEqual(1);
882-
expect(routingConfigurationApiClientMock.count).toHaveBeenNthCalledWith(
883-
1,
884-
'token',
885-
'DRAFT'
886-
);
887-
expect(completedCount).toEqual(5);
888-
expect(routingConfigurationApiClientMock.count).toHaveBeenNthCalledWith(
889-
2,
890-
'token',
891-
'COMPLETED'
892-
);
893-
});
894-
});
895700
});

0 commit comments

Comments
 (0)