@@ -67,29 +67,14 @@ describe("EnrollmentCodePage", () => {
6767 } )
6868 } )
6969
70- test ( "Redirects to dashboard on successful attachment" , async ( ) => {
70+ test ( "Redirects to dashboard on successful attachment (201 status) " , async ( ) => {
7171 setMockResponse . get ( urls . userMe . get ( ) , {
7272 [ Permission . Authenticated ] : true ,
7373 } )
7474
75- const initialOrg = factories . organizations . organization ( { } )
76- const newOrg = factories . organizations . organization ( { } )
77- const initialMitxUser = factories . user . user ( {
78- b2b_organizations : [ initialOrg ] ,
79- } )
80- const updatedMitxUser = factories . user . user ( {
81- b2b_organizations : [ initialOrg , newOrg ] ,
82- } )
83-
84- // First call returns initial user, subsequent calls return updated user
85- let callCount = 0
86- setMockResponse . get ( b2bUrls . userMe . get ( ) , ( ) => {
87- callCount ++
88- return callCount === 1 ? initialMitxUser : updatedMitxUser
89- } )
90-
9175 const attachUrl = b2bUrls . b2bAttach . b2bAttachView ( "test-code" )
92- setMockResponse . post ( attachUrl , updatedMitxUser )
76+ // 201 status indicates successful attachment to new contract(s)
77+ setMockResponse . post ( attachUrl , { } , { code : 201 } )
9378
9479 renderWithProviders ( < EnrollmentCodePage code = "test-code" /> , {
9580 url : commonUrls . B2B_ATTACH_VIEW ,
@@ -104,47 +89,36 @@ describe("EnrollmentCodePage", () => {
10489 } )
10590 } )
10691
107- test ( "Redirects to dashboard with error when b2b organizations don't change " , async ( ) => {
92+ test ( "Redirects to dashboard when user already attached to all contracts (200 status) " , async ( ) => {
10893 setMockResponse . get ( urls . userMe . get ( ) , {
10994 [ Permission . Authenticated ] : true ,
11095 } )
11196
112- const organization = factories . organizations . organization ( { } )
113- const mitxUser = factories . user . user ( {
114- b2b_organizations : [ organization ] ,
115- } )
116-
117- setMockResponse . get ( b2bUrls . userMe . get ( ) , mitxUser )
118-
119- const attachUrl = b2bUrls . b2bAttach . b2bAttachView ( "invalid-code" )
120- setMockResponse . post ( attachUrl , mitxUser )
97+ const attachUrl = b2bUrls . b2bAttach . b2bAttachView ( "already-used-code" )
98+ // 200 status indicates user already attached to all contracts - still redirect to dashboard without error
99+ setMockResponse . post ( attachUrl , { } , { code : 200 } )
121100
122- renderWithProviders ( < EnrollmentCodePage code = "invalid -code" /> , {
101+ renderWithProviders ( < EnrollmentCodePage code = "already-used -code" /> , {
123102 url : commonUrls . B2B_ATTACH_VIEW ,
124103 } )
125104
126105 await waitFor ( ( ) => {
127106 expect ( makeRequest ) . toHaveBeenCalledWith ( "post" , attachUrl , undefined )
128107 } )
129108
130- expect ( mockPush ) . toHaveBeenCalledWith (
131- commonUrls . DASHBOARD_HOME_ENROLLMENT_ERROR ,
132- )
109+ await waitFor ( ( ) => {
110+ expect ( mockPush ) . toHaveBeenCalledWith ( commonUrls . DASHBOARD_HOME )
111+ } )
133112 } )
134113
135- test ( "Redirects to dashboard with error when user has no organizations initially " , async ( ) => {
114+ test ( "Redirects to dashboard with error for invalid code (404 status) " , async ( ) => {
136115 setMockResponse . get ( urls . userMe . get ( ) , {
137116 [ Permission . Authenticated ] : true ,
138117 } )
139118
140- const mitxUser = factories . user . user ( {
141- b2b_organizations : [ ] ,
142- } )
143-
144- setMockResponse . get ( b2bUrls . userMe . get ( ) , mitxUser )
145-
146119 const attachUrl = b2bUrls . b2bAttach . b2bAttachView ( "invalid-code" )
147- setMockResponse . post ( attachUrl , mitxUser )
120+ // 404 status indicates invalid or expired enrollment code
121+ setMockResponse . post ( attachUrl , { } , { code : 404 } )
148122
149123 renderWithProviders ( < EnrollmentCodePage code = "invalid-code" /> , {
150124 url : commonUrls . B2B_ATTACH_VIEW ,
@@ -154,8 +128,10 @@ describe("EnrollmentCodePage", () => {
154128 expect ( makeRequest ) . toHaveBeenCalledWith ( "post" , attachUrl , undefined )
155129 } )
156130
157- expect ( mockPush ) . toHaveBeenCalledWith (
158- commonUrls . DASHBOARD_HOME_ENROLLMENT_ERROR ,
159- )
131+ await waitFor ( ( ) => {
132+ expect ( mockPush ) . toHaveBeenCalledWith (
133+ commonUrls . DASHBOARD_HOME_ENROLLMENT_ERROR ,
134+ )
135+ } )
160136 } )
161137} )
0 commit comments