Skip to content

Commit 169d733

Browse files
committed
Add tests for empty view
1 parent cbc1015 commit 169d733

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

site/.storybook/preview.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { unstable_HistoryRouter as HistoryRouter } from "react-router-dom"
66
import { dark } from "../src/theme"
77
import "../src/theme/globalFonts"
88

9-
addDecorator((story) =>
9+
addDecorator((story) => (
1010
<ThemeProvider theme={dark}>
11-
<CssBaseline />{story()}</ThemeProvider>
12-
)
11+
<CssBaseline />
12+
{story()}
13+
</ThemeProvider>
14+
))
1315

1416
const history = createMemoryHistory()
1517

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { screen } from "@testing-library/react"
2+
import { rest } from "msw"
3+
import React from "react"
4+
import { MockWorkspace } from "../../testHelpers/entities"
5+
import { history, render } from "../../testHelpers/renderHelpers"
6+
import { server } from "../../testHelpers/server"
7+
import { WorkspacesPage } from "./WorkspacesPage"
8+
import { Language } from "./WorkspacesPageView"
9+
10+
describe("WorkspacesPage", () => {
11+
beforeEach(() => {
12+
history.replace("/workspaces")
13+
})
14+
15+
it("renders an empty workspaces page", async () => {
16+
// Given
17+
server.use(
18+
rest.get("/api/v2/users/me/workspaces", async (req, res, ctx) => {
19+
return res(ctx.status(200), ctx.json([]))
20+
}),
21+
)
22+
23+
// When
24+
render(<WorkspacesPage />)
25+
26+
// Then
27+
await screen.findByText(Language.emptyView)
28+
})
29+
30+
it("renders a filled workspaces page", async () => {
31+
// When
32+
render(<WorkspacesPage />)
33+
34+
// Then
35+
await screen.findByText(MockWorkspace.name)
36+
})
37+
})

site/src/pages/WorkspacesPage/WorkspacesPage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const WorkspacesPage: React.FC = () => {
99
return (
1010
<>
1111
<WorkspacesPageView
12+
loading={workspacesState.hasTag("loading")}
1213
workspaces={workspacesState.context.workspaces}
1314
error={workspacesState.context.getWorkspacesError}
1415
/>

site/src/pages/WorkspacesPage/WorkspacesPageView.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Link } from "@material-ui/core"
21
import Avatar from "@material-ui/core/Avatar"
32
import Button from "@material-ui/core/Button"
3+
import Link from "@material-ui/core/Link"
44
import { makeStyles, Theme } from "@material-ui/core/styles"
55
import Table from "@material-ui/core/Table"
66
import TableBody from "@material-ui/core/TableBody"
@@ -22,10 +22,12 @@ import { firstLetter } from "../../util/firstLetter"
2222
dayjs.extend(relativeTime)
2323

2424
export const Language = {
25-
title: "Workspaces",
25+
createButton: "Create Workspace",
26+
emptyView: "so you can check out your repositories, edit your source code, and build and test your software.",
2627
}
2728

2829
export interface WorkspacesPageViewProps {
30+
loading?: boolean
2931
workspaces?: TypesGen.Workspace[]
3032
error?: unknown
3133
}
@@ -38,7 +40,7 @@ export const WorkspacesPageView: React.FC<WorkspacesPageViewProps> = (props) =>
3840
<Stack spacing={4}>
3941
<Margins>
4042
<div className={styles.actions}>
41-
<Button startIcon={<AddCircleOutline />}>Create Workspace</Button>
43+
<Button startIcon={<AddCircleOutline />}>{Language.createButton}</Button>
4244
</div>
4345
<Table>
4446
<TableHead>
@@ -51,15 +53,15 @@ export const WorkspacesPageView: React.FC<WorkspacesPageViewProps> = (props) =>
5153
</TableRow>
5254
</TableHead>
5355
<TableBody>
54-
{!props.workspaces && (
56+
{!props.loading && !props.workspaces?.length && (
5557
<TableRow>
5658
<TableCell colSpan={999}>
5759
<div className={styles.welcome}>
5860
<span>
5961
<Link component={RouterLink} to="/workspaces/new">
6062
Create a workspace
6163
</Link>
62-
&nbsp;so you can check out your repositories, edit your source code, and build and test your software.
64+
&nbsp;{Language.emptyView}
6365
</span>
6466
</div>
6567
</TableCell>

site/src/testHelpers/handlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export const handlers = [
3131
rest.post("/api/v2/users/me/workspaces", async (req, res, ctx) => {
3232
return res(ctx.status(200), ctx.json(M.MockWorkspace))
3333
}),
34+
rest.get("/api/v2/users/me/workspaces", async (req, res, ctx) => {
35+
return res(ctx.status(200), ctx.json([M.MockWorkspace]))
36+
}),
3437
rest.get("/api/v2/users/me/organizations", (req, res, ctx) => {
3538
return res(ctx.status(200), ctx.json([M.MockOrganization]))
3639
}),

0 commit comments

Comments
 (0)