Skip to content

Commit d8b6e4b

Browse files
jkachelgumaerc
authored andcommitted
Adding more tests, fixing a few things, making the design a little bit less terrible
1 parent 3f92e5b commit d8b6e4b

File tree

5 files changed

+63
-73
lines changed

5 files changed

+63
-73
lines changed

frontends/api/src/mitxonline/test-utils/urls.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ const organization = {
4646
`${API_BASE_URL}/api/v0/b2b/organizations/${organizationSlug}/`,
4747
}
4848

49+
const b2bAttach = {
50+
b2bAttachView: (code: string) =>
51+
`${API_BASE_URL}/api/v0/b2b/attach/${code}/`,
52+
}
53+
4954
export {
5055
b2b,
56+
b2bAttach,
5157
currentUser,
5258
enrollment,
5359
programs,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from "react"
2+
import { renderWithProviders, setMockResponse } from "@/test-utils"
3+
import { urls } from "api/test-utils"
4+
import { urls as b2bUrls } from "api/mitxonline-test-utils"
5+
import * as commonUrls from "@/common/urls"
6+
import { Permission } from "api/hooks/user"
7+
import B2BAttachPage from "./B2BAttachPage"
8+
9+
const oldWindowLocation = window.location
10+
11+
beforeAll(() => {
12+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13+
delete (window as any).location
14+
15+
window.location = Object.defineProperties({} as Location, {
16+
...Object.getOwnPropertyDescriptors(oldWindowLocation),
17+
assign: {
18+
configurable: true,
19+
value: jest.fn(),
20+
},
21+
})
22+
})
23+
24+
afterAll(() => {
25+
window.location = oldWindowLocation
26+
})
27+
28+
describe("B2BAttachPage", () => {
29+
test("Renders when logged in", async () => {
30+
setMockResponse.get(urls.userMe.get(), {
31+
[Permission.Authenticated]: true,
32+
})
33+
34+
setMockResponse.post(b2bUrls.b2bAttach.b2bAttachView("test-code"), [])
35+
36+
renderWithProviders(<B2BAttachPage code="test-code" />, {
37+
url: commonUrls.B2B_ATTACH_VIEW,
38+
})
39+
})
40+
})

frontends/main/src/app-pages/B2BAttachPage/B2BAttachPage.tsx

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
"use client"
22
import React from "react"
3-
import { Breadcrumbs, Container, Typography } from "ol-components"
3+
import { styled, Breadcrumbs, Container, Typography } from "ol-components"
44
import * as urls from "@/common/urls"
55
import { useB2BAttachMutation } from "api/mitxonline-hooks/organizations"
66

77
type B2BAttachPageProps = {
88
code: string;
99
};
1010

11-
const B2BAttachPage: React.FC<B2BAttachPageProps> = ({ code }) => {
12-
// This is an interstitial. The user sits here until we validate the code,
13-
// then we toss them to the dashboard.
11+
const InterstitialMessage = styled(Typography)(({ theme }) => ({
12+
...theme.typography.body1,
13+
textAlign: "center",
14+
}))
1415

16+
const B2BAttachPage: React.FC<B2BAttachPageProps> = ({ code }) => {
1517
const { mutate: attach, isSuccess } = useB2BAttachMutation(code)
1618

17-
React.useEffect(() => {
18-
// Trigger the attach mutation when the component mounts
19-
console.log("attempting to attach code", code)
20-
attach(code)
21-
}, [attach, code])
19+
React.useEffect(() => attach(code), [attach, code])
2220

2321
if (isSuccess) {
2422
//window.history.replaceState(null, "", urls.DASHBOARD_HOME);
@@ -32,13 +30,9 @@ const B2BAttachPage: React.FC<B2BAttachPageProps> = ({ code }) => {
3230
current="Use Enrollment Code"
3331
/>
3432

35-
<Typography component="h1" variant="h3">
36-
Please Wait
37-
</Typography>
38-
39-
<Typography>
40-
{isSuccess ? <>Your enrollment code {code} has been validated.</> : <>Please wait while we validate your enrollment code {code}</>}
41-
</Typography>
33+
<InterstitialMessage>
34+
Validating code "{code}"...
35+
</InterstitialMessage>
4236
</Container>
4337
)
4438
}

frontends/main/src/app-pages/B2BAttachPage/CartPage.test.tsx

Lines changed: 0 additions & 55 deletions
This file was deleted.

frontends/main/src/app/attach/[code]/page.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import React from "react"
22
import { Metadata } from "next"
33
import { standardizeMetadata } from "@/common/metadata"
44
import invariant from "tiny-invariant"
5-
5+
import { Permission } from "api/hooks/user"
6+
import RestrictedRoute from "@/components/RestrictedRoute/RestrictedRoute"
67
import type { PageParams } from "@/app/types"
78
import B2BAttachPage from "@/app-pages/B2BAttachPage/B2BAttachPage"
89

@@ -15,7 +16,11 @@ const Page: React.FC<PageParams<object, { code: string }>> = async ({
1516
}) => {
1617
const resolved = await params
1718
invariant(resolved?.code, "code is required")
18-
return <B2BAttachPage code={resolved?.code} />
19+
return (
20+
<RestrictedRoute requires={Permission.Authenticated}>
21+
<B2BAttachPage code={resolved?.code} />
22+
</RestrictedRoute>
23+
)
1924
}
2025

2126
export default Page

0 commit comments

Comments
 (0)