@@ -16,12 +16,14 @@ import {
1616 setTemplateToSubmitted ,
1717 requestTemplateProof ,
1818 createRoutingConfig ,
19+ submitRoutingConfig ,
1920} from '@utils/form-actions' ;
2021import { getSessionServer } from '@utils/amplify-utils' ;
21- import { TemplateDto } from 'nhs-notify-backend-client' ;
22+ import { RoutingConfig , TemplateDto } from 'nhs-notify-backend-client' ;
2223import { templateApiClient } from 'nhs-notify-backend-client/src/template-api-client' ;
2324import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client' ;
2425import { randomUUID } from 'node:crypto' ;
26+ import { mock } from 'jest-mock-extended' ;
2527
2628const mockedTemplateClient = jest . mocked ( templateApiClient ) ;
2729const mockedRoutingConfigClient = jest . mocked ( routingConfigurationApiClient ) ;
@@ -34,7 +36,7 @@ jest.mock('nhs-notify-backend-client/src/routing-config-api-client');
3436describe ( 'form-actions' , ( ) => {
3537 beforeEach ( ( ) => {
3638 jest . resetAllMocks ( ) ;
37- authIdTokenServerMock . mockResolvedValueOnce ( {
39+ authIdTokenServerMock . mockResolvedValue ( {
3840 accessToken : 'token' ,
3941 clientId : 'client1' ,
4042 } ) ;
@@ -98,7 +100,6 @@ describe('form-actions', () => {
98100 } ) ;
99101
100102 test ( 'createTemplate - should throw error when no token' , async ( ) => {
101- authIdTokenServerMock . mockReset ( ) ;
102103 authIdTokenServerMock . mockResolvedValueOnce ( {
103104 accessToken : undefined ,
104105 clientId : undefined ,
@@ -266,7 +267,6 @@ describe('form-actions', () => {
266267 } ) ;
267268
268269 test ( 'uploadLetterTemplate - should throw error when no token' , async ( ) => {
269- authIdTokenServerMock . mockReset ( ) ;
270270 authIdTokenServerMock . mockResolvedValueOnce ( {
271271 accessToken : undefined ,
272272 clientId : undefined ,
@@ -363,7 +363,6 @@ describe('form-actions', () => {
363363 } ) ;
364364
365365 test ( 'saveTemplate - should throw error when no token' , async ( ) => {
366- authIdTokenServerMock . mockReset ( ) ;
367366 authIdTokenServerMock . mockResolvedValueOnce ( {
368367 accessToken : undefined ,
369368 clientId : undefined ,
@@ -431,7 +430,6 @@ describe('form-actions', () => {
431430 } ) ;
432431
433432 test ( 'getTemplate - should throw error when no token' , async ( ) => {
434- authIdTokenServerMock . mockReset ( ) ;
435433 authIdTokenServerMock . mockResolvedValueOnce ( {
436434 accessToken : undefined ,
437435 clientId : undefined ,
@@ -481,7 +479,6 @@ describe('form-actions', () => {
481479 } ) ;
482480
483481 test ( 'getTemplates - should throw error when no token' , async ( ) => {
484- authIdTokenServerMock . mockReset ( ) ;
485482 authIdTokenServerMock . mockResolvedValueOnce ( {
486483 accessToken : undefined ,
487484 clientId : undefined ,
@@ -574,11 +571,7 @@ describe('form-actions', () => {
574571 } ) ;
575572
576573 test ( 'submitTemplate - should throw error when no token' , async ( ) => {
577- authIdTokenServerMock . mockReset ( ) ;
578- authIdTokenServerMock . mockResolvedValueOnce ( {
579- accessToken : undefined ,
580- clientId : undefined ,
581- } ) ;
574+ authIdTokenServerMock . mockResolvedValueOnce ( { } ) ;
582575
583576 await expect ( setTemplateToSubmitted ( 'id' ) ) . rejects . toThrow (
584577 'Failed to get access token'
@@ -623,11 +616,7 @@ describe('form-actions', () => {
623616 } ) ;
624617
625618 test ( 'deleteTemplate - should throw error when no token' , async ( ) => {
626- authIdTokenServerMock . mockReset ( ) ;
627- authIdTokenServerMock . mockResolvedValueOnce ( {
628- accessToken : undefined ,
629- clientId : undefined ,
630- } ) ;
619+ authIdTokenServerMock . mockResolvedValueOnce ( { } ) ;
631620
632621 await expect ( setTemplateToDeleted ( 'id' ) ) . rejects . toThrow (
633622 'Failed to get access token'
@@ -690,11 +679,7 @@ describe('form-actions', () => {
690679 } ) ;
691680
692681 test ( 'requestTemplateProof - should throw error when no token' , async ( ) => {
693- authIdTokenServerMock . mockReset ( ) ;
694- authIdTokenServerMock . mockResolvedValueOnce ( {
695- accessToken : undefined ,
696- clientId : undefined ,
697- } ) ;
682+ authIdTokenServerMock . mockResolvedValueOnce ( { } ) ;
698683
699684 await expect ( requestTemplateProof ( 'id' ) ) . rejects . toThrow (
700685 'Failed to get access token'
@@ -772,11 +757,7 @@ describe('form-actions', () => {
772757 } ) ;
773758
774759 test ( 'errors when no token' , async ( ) => {
775- authIdTokenServerMock . mockReset ( ) ;
776- authIdTokenServerMock . mockResolvedValueOnce ( {
777- accessToken : undefined ,
778- clientId : undefined ,
779- } ) ;
760+ authIdTokenServerMock . mockResolvedValueOnce ( { } ) ;
780761
781762 await expect (
782763 createRoutingConfig ( {
@@ -824,4 +805,44 @@ describe('form-actions', () => {
824805 ) . rejects . toThrow ( 'Failed to create message plan' ) ;
825806 } ) ;
826807 } ) ;
808+
809+ describe ( 'submitRoutingConfig' , ( ) => {
810+ it ( 'submits the routing config' , async ( ) => {
811+ mockedRoutingConfigClient . submit . mockResolvedValueOnce ( {
812+ data : mock < RoutingConfig > ( ) ,
813+ } ) ;
814+
815+ await submitRoutingConfig ( 'id' ) ;
816+
817+ expect ( mockedRoutingConfigClient . submit ) . toHaveBeenCalledWith (
818+ 'id' ,
819+ 'token'
820+ ) ;
821+ } ) ;
822+
823+ it ( 'throws if there is no token' , async ( ) => {
824+ authIdTokenServerMock . mockResolvedValueOnce ( { } ) ;
825+
826+ await expect ( ( ) => submitRoutingConfig ( 'id' ) ) . rejects . toThrow (
827+ 'Failed to get access token'
828+ ) ;
829+
830+ expect ( mockedRoutingConfigClient . submit ) . not . toHaveBeenCalled ( ) ;
831+ } ) ;
832+
833+ it ( 'throws if the api request returns an error' , async ( ) => {
834+ mockedRoutingConfigClient . submit . mockResolvedValueOnce ( {
835+ error : {
836+ errorMeta : {
837+ code : 400 ,
838+ description : 'Bad request' ,
839+ } ,
840+ } ,
841+ } ) ;
842+
843+ await expect ( ( ) => submitRoutingConfig ( 'id' ) ) . rejects . toThrow (
844+ 'Failed to submit message plan'
845+ ) ;
846+ } ) ;
847+ } ) ;
827848} ) ;
0 commit comments