Skip to content

Commit 15637bd

Browse files
committed
Add tests
1 parent 584d3a3 commit 15637bd

File tree

6 files changed

+85
-15
lines changed

6 files changed

+85
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { screen } from "@testing-library/react"
2+
import { rest } from "msw"
3+
import React from "react"
4+
import { MockTemplate } from "../../testHelpers/entities"
5+
import { history, render } from "../../testHelpers/renderHelpers"
6+
import { server } from "../../testHelpers/server"
7+
import TemplatesPage from "./TemplatesPage"
8+
import { Language } from "./TemplatesPageView"
9+
10+
describe("TemplatesPage", () => {
11+
beforeEach(() => {
12+
history.replace("/workspaces")
13+
})
14+
15+
it("renders an empty templates page", async () => {
16+
// Given
17+
server.use(
18+
rest.get("/api/v2/organizations/:organizationId/templates", (req, res, ctx) => {
19+
return res(ctx.status(200), ctx.json([]))
20+
}),
21+
rest.post("/api/v2/users/:userId/authorization", async (req, res, ctx) => {
22+
return res(
23+
ctx.status(200),
24+
ctx.json({
25+
createTemplates: true,
26+
}),
27+
)
28+
}),
29+
)
30+
31+
// When
32+
render(<TemplatesPage />)
33+
34+
// Then
35+
await screen.findByText(Language.emptyViewCreate)
36+
})
37+
38+
it("renders a filled templates page", async () => {
39+
// When
40+
render(<TemplatesPage />)
41+
42+
// Then
43+
await screen.findByText(MockTemplate.name)
44+
})
45+
46+
it("shows empty view without permissions to create", async () => {
47+
server.use(
48+
rest.get("/api/v2/organizations/:organizationId/templates", (req, res, ctx) => {
49+
return res(ctx.status(200), ctx.json([]))
50+
}),
51+
rest.post("/api/v2/users/:userId/authorization", async (req, res, ctx) => {
52+
return res(
53+
ctx.status(200),
54+
ctx.json({
55+
createTemplates: false,
56+
}),
57+
)
58+
}),
59+
)
60+
61+
// When
62+
render(<TemplatesPage />)
63+
64+
// Then
65+
await screen.findByText(Language.emptyViewNoPerms)
66+
})
67+
})

site/src/pages/TemplatesPage/TemplatesPageView.stories.tsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ export const AllStates = Template.bind({})
1414
AllStates.args = {
1515
canCreateTemplate: true,
1616
templates: [
17-
MockTemplate,
18-
{
19-
...MockTemplate,
20-
description: "🚀 Some magical template that does some magical things!",
21-
},
22-
{
23-
...MockTemplate,
24-
workspace_owner_count: 150,
25-
description: "😮 Wow, this one has a bunch of usage!"
26-
}
17+
MockTemplate,
18+
{
19+
...MockTemplate,
20+
description: "🚀 Some magical template that does some magical things!",
21+
},
22+
{
23+
...MockTemplate,
24+
workspace_owner_count: 150,
25+
description: "😮 Wow, this one has a bunch of usage!",
26+
},
2727
],
2828
}
2929

site/src/pages/TemplatesPage/TemplatesPageView.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ dayjs.extend(relativeTime)
2121

2222
export const Language = {
2323
createButton: "Create Template",
24-
emptyView: "to standardize development workspaces for your team.",
24+
emptyViewCreate: "to standardize development workspaces for your team.",
25+
emptyViewNoPerms: "No templates have been created! Contact your Coder administrator.",
2526
}
2627

2728
export interface TemplatesPageViewProps {
@@ -57,10 +58,10 @@ export const TemplatesPageView: React.FC<TemplatesPageViewProps> = (props) => {
5758
<Link component={RouterLink} to="/templates/new">
5859
Create a template
5960
</Link>
60-
&nbsp;{Language.emptyView}
61+
&nbsp;{Language.emptyViewCreate}
6162
</span>
6263
) : (
63-
<span>No templates have been created! Contact your Coder administrator.</span>
64+
<span>{Language.emptyViewNoPerms}</span>
6465
)}
6566
</div>
6667
</TableCell>

site/src/testHelpers/handlers.ts

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export const handlers = [
1717
rest.get("/api/v2/organizations/:organizationId/templates/:templateId", async (req, res, ctx) => {
1818
return res(ctx.status(200), ctx.json(M.MockTemplate))
1919
}),
20+
rest.get("/api/v2/organizations/:organizationId/templates", async (req, res, ctx) => {
21+
return res(ctx.status(200), ctx.json([M.MockTemplate]))
22+
}),
2023

2124
// templates
2225
rest.get("/api/v2/templates/:templateId", async (req, res, ctx) => {

site/static/terraform-logo.svg

-1
This file was deleted.

site/webpack.dev.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const config: Configuration = {
6161
port: process.env.PORT || 8080,
6262
proxy: {
6363
"/api": {
64-
target: "https://dev.coder.com",
64+
target: "http://localhost:3000",
6565
ws: true,
6666
secure: false,
6767
},

0 commit comments

Comments
 (0)