Skip to content

Commit 1b0ed59

Browse files
committed
Add tests to check if the audit logs are showing
1 parent 294dceb commit 1b0ed59

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

site/src/api/api.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import axios, { AxiosRequestHeaders } from "axios"
22
import dayjs from "dayjs"
3-
import { MockAuditLog } from "testHelpers/entities"
43
import * as Types from "./types"
54
import { WorkspaceBuildTransition } from "./types"
65
import * as TypesGen from "./typesGenerated"
@@ -386,8 +385,6 @@ export const getEntitlements = async (): Promise<TypesGen.Entitlements> => {
386385
}
387386

388387
export const getAuditLogs = async (): Promise<TypesGen.AuditLog[]> => {
389-
return [MockAuditLog]
390-
// TODO: Uncomment this to get the data from the API instead of mock
391-
// const response = await axios.get("/api/v2/audit")
392-
// return response.data
388+
const response = await axios.get("/api/v2/audit")
389+
return response.data
393390
}

site/src/components/AuditLogRow/AuditLogRow.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ export const AuditLogRow: React.FC<AuditLogRowProps> = ({
109109
}
110110

111111
return (
112-
<TableRow key={auditLog.id} hover={shouldDisplayDiff}>
112+
<TableRow
113+
key={auditLog.id}
114+
hover={shouldDisplayDiff}
115+
data-testid={`audit-log-row-${auditLog.id}`}
116+
>
113117
<TableCell className={styles.auditLogCell}>
114118
<Stack
115119
style={{ cursor: shouldDisplayDiff ? "pointer" : undefined }}

site/src/pages/AuditPage/AuditPage.test.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ import { fireEvent, screen } from "@testing-library/react"
22
import { Language as CopyButtonLanguage } from "components/CopyButton/CopyButton"
33
import { Language as AuditTooltipLanguage } from "components/Tooltips/AuditHelpTooltip"
44
import { Language as TooltipLanguage } from "components/Tooltips/HelpTooltip/HelpTooltip"
5-
import { render } from "testHelpers/renderHelpers"
5+
import { MockAuditLog, MockAuditLog2, render } from "testHelpers/renderHelpers"
6+
import * as CreateDayString from "util/createDayString"
67
import AuditPage from "./AuditPage"
78
import { Language as AuditViewLanguage } from "./AuditPageView"
89

910
describe("AuditPage", () => {
11+
beforeEach(() => {
12+
// Mocking the dayjs module within the createDayString file
13+
const mock = jest.spyOn(CreateDayString, "createDayString")
14+
mock.mockImplementation(() => "a minute ago")
15+
})
16+
1017
it("renders a page with a title and subtitle", async () => {
1118
// When
1219
render(<AuditPage />)
@@ -29,4 +36,13 @@ describe("AuditPage", () => {
2936
fireEvent.mouseOver(copyIcon)
3037
expect(await screen.findByText(AuditViewLanguage.tooltipTitle)).toBeInTheDocument()
3138
})
39+
40+
it("shows the audit logs", async () => {
41+
// When
42+
render(<AuditPage />)
43+
44+
// Then
45+
await screen.findByTestId(`audit-log-row-${MockAuditLog.id}`)
46+
screen.getByTestId(`audit-log-row-${MockAuditLog2.id}`)
47+
})
3248
})

site/src/testHelpers/entities.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ export const MockAuditLog: TypesGen.AuditLog = {
706706

707707
export const MockAuditLog2: TypesGen.AuditLog = {
708708
...MockAuditLog,
709+
id: "53bded77-7b9d-4e82-8771-991a34d759f9",
709710
action: "write",
710711
diff: {
711712
workspace_name: {

site/src/testHelpers/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,6 @@ export const handlers = [
150150

151151
// Audit
152152
rest.get("/api/v2/audit", (req, res, ctx) => {
153-
return res(ctx.status(200), ctx.json(M.MockAuditLog))
153+
return res(ctx.status(200), ctx.json([M.MockAuditLog, M.MockAuditLog2]))
154154
}),
155155
]

site/src/xServices/audit/auditXService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const auditMachine = createMachine(
1515
},
1616
},
1717
tsTypes: {} as import("./auditXService.typegen").Typegen0,
18+
initial: "loadingLogs",
1819
states: {
1920
loadingLogs: {
2021
invoke: {

0 commit comments

Comments
 (0)