Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
chadbrokaw committed Jan 24, 2025
1 parent a228ea6 commit 3d52243
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/javascript/__tests__/api/apiService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,20 @@ describe("apiService", () => {
expect(deleteSpy).toHaveBeenCalled()
})
})
it("fails if the token is present but invalid", async () => {
jest.spyOn(console, "error").mockImplementation(() => {})
Storage.prototype.getItem = (_key: string) => {
return JSON.stringify({ "access-token": "test-token", expiry: "0" })
}
try {
await authenticatedPost(url, data, config)
console.error("This console error should not be called")
} catch (error) {
// eslint-disable-next-line jest/no-conditional-expect, jest/no-try-expect
expect(error).toBeInstanceOf(Error)
// eslint-disable-next-line jest/no-conditional-expect, jest/no-try-expect
expect(error.message).toBe("Token expired")
}
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe("UserProvider", () => {
await waitFor(() => expect(screen.getByText("Sign In")).not.toBeNull())
})

it("should handle token invalidation on initial load", async () => {
it("should handle token invalidation", async () => {
;(isTokenValid as jest.Mock).mockReturnValue(false)
;(getProfile as jest.Mock).mockRejectedValueOnce(new Error("Token expired"))

Expand Down
11 changes: 11 additions & 0 deletions app/javascript/__tests__/hooks/useGTMDataLayer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,15 @@ describe("useGTMDataLayer", () => {

expect(consoleSpy).toHaveBeenCalled()
})

it("errors out when no event is provided in the data object", () => {
const event = "testEvent"
const data = { test: "data", event: "testEvent" }

const { result } = renderHook(() => useGTMDataLayer())

result.current.pushToDataLayer(event, data)

expect(consoleSpy).toHaveBeenCalled()
})
})

0 comments on commit 3d52243

Please sign in to comment.