@@ -2,13 +2,15 @@ import { expect, jest } from "@jest/globals";
22
33jest . mock ( '../../src/utils/application.data' ) ;
44jest . mock ( "../../src/utils/logger" ) ;
5+ jest . mock ( "../../src/utils/feature.flag" ) ;
56
67import { Request , request } from "express" ;
78import * as config from "../../src/config" ;
89import * as urlUtils from "../../src/utils/url" ;
910import { getApplicationData } from '../../src/utils/application.data' ;
1011import { APPLICATION_DATA_MOCK } from "../__mocks__/session.mock" ;
1112import { createAndLogErrorRequest , logger } from "../../src/utils/logger" ;
13+ import { isActiveFeature } from "../../src/utils/feature.flag" ;
1214
1315const mockGetApplicationData = getApplicationData as jest . Mock ;
1416const mockCreateAndLogErrorRequest = createAndLogErrorRequest as jest . Mock ;
@@ -391,4 +393,52 @@ describe("Url utils tests", () => {
391393 expect ( previousPage ) . toBeUndefined ( ) ;
392394 } ) ;
393395 } ) ;
396+
397+ describe ( "getBackLinkUrl tests" , ( ) => {
398+ const urlWithEntityIds = "/transaction/:transactionId/submission/:submissionId/entity" ;
399+ const urlWithoutEntityIds = "/entity" ;
400+ const transactionId = "tx123" ;
401+ const submissionId = "sub456" ;
402+ const mockIsActiveFeature = isActiveFeature as jest . Mock ;
403+
404+ beforeEach ( ( ) => {
405+ jest . spyOn ( urlUtils , "getTransactionIdAndSubmissionIdFromOriginalUrl" ) . mockImplementation ( ( ) => ( {
406+ transactionId,
407+ submissionId
408+ } ) ) ;
409+ jest . spyOn ( urlUtils , "getUrlWithTransactionIdAndSubmissionId" ) . mockImplementation ( ( url , tId , sId ) => {
410+ return url . replace ( ":transactionId" , tId ) . replace ( ":submissionId" , sId ) ;
411+ } ) ;
412+ jest . clearAllMocks ( ) ;
413+ } ) ;
414+
415+ test ( "returns url with entity ids when feature flag is enabled and ids are present" , ( ) => {
416+ mockIsActiveFeature . mockReturnValueOnce ( true ) ;
417+ const req = { originalUrl : "/transaction/tx123/submission/sub456" , params : { } , query : { } } as unknown as Request ;
418+ const result = urlUtils . getBackLinkUrl ( { req, urlWithEntityIds, urlWithoutEntityIds } ) ;
419+ expect ( result ) . toBe ( "/transaction/tx123/submission/sub456/entity" ) ;
420+ } ) ;
421+
422+ test ( "returns urlWithoutEntityIds when feature flag is disabled" , ( ) => {
423+ mockIsActiveFeature . mockReturnValueOnce ( false ) ;
424+ const req = { originalUrl : "/transaction/tx123/submission/sub456" , params : { } , query : { } } as unknown as Request ;
425+ const result = urlUtils . getBackLinkUrl ( { req, urlWithEntityIds, urlWithoutEntityIds } ) ;
426+ expect ( result ) . toBe ( urlWithoutEntityIds ) ;
427+ } ) ;
428+
429+ test ( "returns urlWithoutEntityIds when ids are undefined" , ( ) => {
430+ jest . spyOn ( urlUtils , "getTransactionIdAndSubmissionIdFromOriginalUrl" ) . mockReturnValue ( undefined ) ;
431+ const req = { originalUrl : "/no-ids-here" , params : { } , query : { } } as unknown as Request ;
432+ const result = urlUtils . getBackLinkUrl ( { req, urlWithEntityIds, urlWithoutEntityIds } ) ;
433+ expect ( result ) . toBe ( urlWithoutEntityIds ) ;
434+ } ) ;
435+
436+ test ( "returns urlWithoutEntityIds and logs error if exception is thrown" , ( ) => {
437+ jest . spyOn ( urlUtils , "getTransactionIdAndSubmissionIdFromOriginalUrl" ) . mockImplementation ( ( ) => { throw new Error ( "fail" ) ; } ) ;
438+ const req = { originalUrl : "/fail" , params : { } , query : { } } as unknown as Request ;
439+ const result = urlUtils . getBackLinkUrl ( { req, urlWithEntityIds, urlWithoutEntityIds } ) ;
440+ expect ( result ) . toBe ( urlWithoutEntityIds ) ;
441+ expect ( logger . errorRequest ) . toHaveBeenCalled ( ) ;
442+ } ) ;
443+ } ) ;
394444} ) ;
0 commit comments