Skip to content

feat: Add support for update checks and notifications #4810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Dec 1, 2022
Merged
Prev Previous commit
fix(site): Add jest tests for UpdateCheckBanner
  • Loading branch information
mafredri committed Dec 1, 2022
commit 44f6475db8f6c2f378f06915ad05ec00cab76fa9
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ UpdateAvailable.args = {
},
}

export const NoUpdateAvailable = Template.bind({})
NoUpdateAvailable.args = {
updateCheck: {
current: true,
version: "v0.12.9",
url: "https://github.com/coder/coder/releases/tag/v0.12.9",
},
}

export const UpdateCheckError = Template.bind({})
UpdateCheckError.args = {
error: new Error("Something went wrong."),
Expand Down
51 changes: 51 additions & 0 deletions site/src/components/UpdateCheckBanner/UpdateCheckBanner.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { fireEvent, screen, waitFor } from "@testing-library/react"
import i18next from "i18next"
import { MockUpdateCheck, render } from "testHelpers/renderHelpers"
import { UpdateCheckBanner } from "./UpdateCheckBanner"

describe("UpdateCheckBanner", () => {
it("shows an update notification when one is available", () => {
const { t } = i18next
render(
<UpdateCheckBanner
updateCheck={{ ...MockUpdateCheck, current: false }}
/>,
)

const updateText = t("updateCheck.message", {
ns: "common",
version: MockUpdateCheck.version,
})
// Message contatins HTML elements so we check it in parts.
for (const text of updateText.split(/<\/?[0-9]+>/)) {
expect(screen.getByText(text, { exact: false })).toBeInTheDocument()
}

expect(screen.getAllByRole("link")[0]).toHaveAttribute(
"href",
MockUpdateCheck.url,
)
})

it("is hidden when dismissed", async () => {
const dismiss = jest.fn()
const { container } = render(
<UpdateCheckBanner
onDismiss={dismiss}
updateCheck={{ ...MockUpdateCheck, current: false }}
/>,
)

fireEvent.click(screen.getByRole("button"))
await waitFor(() => expect(dismiss).toBeCalledTimes(1), { timeout: 2000 })

expect(container.firstChild).toBeNull()
})

it("does not show when up-to-date", async () => {
const { container } = render(
<UpdateCheckBanner updateCheck={{ ...MockUpdateCheck, current: true }} />,
)
expect(container.firstChild).toBeNull()
})
})