11import { render , screen , waitFor } from "@testing-library/react" ;
22import userEvent from "@testing-library/user-event" ;
3- import { RJSFSchema , UiSchema } from "@rjsf/utils" ;
43import StartReportForm from "@reporting/src/app/components/report/StartReportForm" ;
54import { actionHandler } from "@bciers/actions" ;
65import { useRouter } from "next/navigation" ;
76import expectComboBox from "@bciers/testConfig/helpers/expectComboBox" ;
87import { selectComboboxOption } from "@bciers/testConfig/helpers/selectComboboxOption" ;
8+ import { createStartReportSchemas } from "@reporting/src/data/jsonSchema/report/startReport" ;
9+ import getPreviousReportableOperations from "@reporting/src/app/utils/getPreviousReportableOperations" ;
910
1011vi . mock ( "@bciers/actions" , ( ) => ( {
1112 actionHandler : vi . fn ( ) ,
@@ -15,53 +16,41 @@ vi.mock("next/navigation", () => ({
1516 useRouter : vi . fn ( ) ,
1617} ) ) ;
1718
19+ vi . mock ( "@reporting/src/app/utils/getPreviousReportableOperations" , ( ) => ( {
20+ default : vi . fn ( ) ,
21+ } ) ) ;
22+
1823const mockPush = vi . fn ( ) ;
1924const mockBack = vi . fn ( ) ;
2025
2126const mockActionHandler = vi . mocked ( actionHandler ) ;
2227const mockUseRouter = vi . mocked ( useRouter ) ;
28+ const mockGetPreviousReportableOperations = vi . mocked (
29+ getPreviousReportableOperations ,
30+ ) ;
2331
24- const schema : RJSFSchema = {
25- type : "object" ,
26- required : [ "reporting_year" , "operation_id" , "registration_purpose" ] ,
27- properties : {
28- reporting_year : {
29- type : "number" ,
30- title : "Reporting year" ,
31- enum : [ 2023 ] ,
32- } ,
33- operation_id : {
34- type : "string" ,
35- title : "Operation" ,
36- enum : [ "operation-1" ] ,
37- } ,
38- registration_purpose : {
39- type : "string" ,
40- title : "Registration purpose" ,
41- enum : [ "Reporting Operation" ] ,
42- } ,
43- } ,
44- } ;
45-
46- const uiSchema : UiSchema = { } ;
32+ const renderForm = async ( ) => {
33+ const { schema, uiSchema } = await createStartReportSchemas ( ) ;
4734
48- const renderForm = ( ) => {
4935 render ( < StartReportForm schema = { schema } uiSchema = { uiSchema } /> ) ;
5036} ;
5137
5238const expectForm = ( ) => {
53- expectComboBox ( / R e p o r t i n g y e a r / i) ;
54- expectComboBox ( / O p e r a t i o n / i) ;
55- expectComboBox ( / R e g i s t r a t i o n p u r p o s e / i) ;
39+ expectComboBox ( / S e l e c t r e p o r t i n g y e a r / i) ;
40+ expectComboBox ( / S e l e c t o p e r a t i o n / i) ;
41+ expectComboBox ( / S e l e c t t h e r e g i s t r a t i o n p u r p o s e / i) ;
5642
5743 expect ( screen . getByRole ( "button" , { name : / s t a r t / i } ) ) . toBeVisible ( ) ;
5844 expect ( screen . getByRole ( "button" , { name : / c a n c e l / i } ) ) . toBeVisible ( ) ;
5945} ;
6046
6147const fillForm = async ( ) => {
62- await selectComboboxOption ( / R e p o r t i n g y e a r / i, "2023" ) ;
63- await selectComboboxOption ( / O p e r a t i o n / i, "operation-1" ) ;
64- await selectComboboxOption ( / R e g i s t r a t i o n p u r p o s e / i, "Reporting Operation" ) ;
48+ await selectComboboxOption ( / S e l e c t r e p o r t i n g y e a r / i, "2023" ) ;
49+ await selectComboboxOption ( / S e l e c t o p e r a t i o n / i, "Operation 1" ) ;
50+ await selectComboboxOption (
51+ / S e l e c t t h e r e g i s t r a t i o n p u r p o s e / i,
52+ "Reporting Operation" ,
53+ ) ;
6554} ;
6655
6756describe ( "StartReportForm" , ( ) => {
@@ -70,21 +59,28 @@ describe("StartReportForm", () => {
7059
7160 mockUseRouter . mockReturnValue ( {
7261 push : mockPush ,
73- back : mockBack ,
7462 } as unknown as ReturnType < typeof useRouter > ) ;
63+
64+ mockGetPreviousReportableOperations . mockResolvedValue ( [
65+ {
66+ reporting_year : 2023 ,
67+ operation_id : "operation-1" ,
68+ operation_name : "Operation 1" ,
69+ registration_purposes : [ "Reporting Operation" ] ,
70+ } ,
71+ ] ) ;
7572 } ) ;
7673
77- it ( "renders the form" , ( ) => {
78- renderForm ( ) ;
74+ it ( "renders the generated start report form" , async ( ) => {
75+ await renderForm ( ) ;
7976
8077 expectForm ( ) ;
8178 } ) ;
8279
8380 it ( "submits the selected report data and redirects to review operation information" , async ( ) => {
8481 mockActionHandler . mockResolvedValue ( 123 ) ;
8582
86- renderForm ( ) ;
87-
83+ await renderForm ( ) ;
8884 await fillForm ( ) ;
8985
9086 await userEvent . click ( screen . getByRole ( "button" , { name : / s t a r t / i } ) ) ;
@@ -125,14 +121,12 @@ describe("StartReportForm", () => {
125121 } ,
126122 } ) ;
127123
128- renderForm ( ) ;
129-
124+ await renderForm ( ) ;
130125 await fillForm ( ) ;
131126
132127 await userEvent . click ( screen . getByRole ( "button" , { name : / s t a r t / i } ) ) ;
133128
134129 expect ( await screen . findByText ( "Unable to create report." ) ) . toBeVisible ( ) ;
135-
136130 expect ( mockPush ) . not . toHaveBeenCalled ( ) ;
137131 } ) ;
138132
@@ -141,22 +135,21 @@ describe("StartReportForm", () => {
141135 error : "Unable to create report." ,
142136 } ) ;
143137
144- renderForm ( ) ;
145-
138+ await renderForm ( ) ;
146139 await fillForm ( ) ;
147140
148141 await userEvent . click ( screen . getByRole ( "button" , { name : / s t a r t / i } ) ) ;
149142
150143 expect ( await screen . findByText ( "Unable to create report." ) ) . toBeVisible ( ) ;
151-
152144 expect ( mockPush ) . not . toHaveBeenCalled ( ) ;
153145 } ) ;
154146
155- it ( "goes back when Cancel is clicked" , async ( ) => {
156- renderForm ( ) ;
147+ it ( "returns to the previous reports page when Cancel is clicked" , async ( ) => {
148+ await renderForm ( ) ;
157149
158150 await userEvent . click ( screen . getByRole ( "button" , { name : / c a n c e l / i } ) ) ;
159151
160- expect ( mockBack ) . toHaveBeenCalledOnce ( ) ;
152+ expect ( mockPush ) . toHaveBeenCalledWith ( "/reports/previous-years" ) ;
153+ expect ( mockBack ) . not . toHaveBeenCalled ( ) ;
161154 } ) ;
162155} ) ;
0 commit comments