chore: add tests

This commit is contained in:
Julien Calixte
2026-06-06 22:13:14 +02:00
parent 8a8509a0f3
commit 1a2d8f4ebf
23 changed files with 1943 additions and 3 deletions

View File

@@ -0,0 +1,74 @@
import { mount } from "@vue/test-utils"
import { describe, expect, it } from "vitest"
import NoteFreshnessBadge from "./NoteFreshnessBadge.vue"
const factory = (props: { status: string; lastCheckedAt?: Date | null }) =>
mount(NoteFreshnessBadge, {
props: { lastCheckedAt: null, ...props } as never
})
describe("NoteFreshnessBadge", () => {
it("renders the verified state with the cloud-check icon", () => {
const wrapper = factory({ status: "verified" })
expect(wrapper.classes()).toContain("state-verified")
expect(wrapper.find(".icon-tabler-cloud-check").exists()).toBe(true)
})
it("renders the outdated state with the download icon", () => {
const wrapper = factory({ status: "outdated" })
expect(wrapper.classes()).toContain("state-outdated")
expect(wrapper.find(".icon-tabler-cloud-download").exists()).toBe(true)
})
it("renders the offline state with the cloud-off icon", () => {
const wrapper = factory({ status: "offline" })
expect(wrapper.classes()).toContain("state-offline")
expect(wrapper.find(".icon-tabler-cloud-off").exists()).toBe(true)
})
it("renders the unauthorized state with the lock icon", () => {
const wrapper = factory({ status: "unauthorized" })
expect(wrapper.classes()).toContain("state-unauthorized")
expect(wrapper.find(".icon-tabler-cloud-lock").exists()).toBe(true)
})
it("disables the button while checking", () => {
const wrapper = factory({ status: "checking" })
expect(wrapper.attributes("disabled")).toBeDefined()
expect(wrapper.find(".icon-tabler-loader-2").exists()).toBe(true)
})
it("emits click when not busy", async () => {
const wrapper = factory({ status: "verified" })
await wrapper.trigger("click")
expect(wrapper.emitted("click")).toHaveLength(1)
})
it("includes the last-checked time in the verified tooltip", () => {
const wrapper = factory({
status: "verified",
lastCheckedAt: new Date("2026-01-01T10:30:00")
})
const tooltip = wrapper.attributes("title") as string
expect(tooltip).toMatch(/Verified at/)
})
it("renders the unknown state as default", () => {
const wrapper = factory({ status: "unknown" })
expect(wrapper.classes()).toContain("state-unknown")
expect(wrapper.find(".icon-tabler-cloud-question").exists()).toBe(true)
})
it("offline tooltip prompts a retry", () => {
const wrapper = factory({ status: "offline" })
expect(wrapper.attributes("title")).toMatch(/retry/i)
})
it("unauthorized tooltip prompts a sign-in", () => {
const wrapper = factory({ status: "unauthorized" })
expect(wrapper.attributes("title")).toMatch(/[Ss]ign in/)
})
})

View File

@@ -0,0 +1,98 @@
import { mount } from "@vue/test-utils"
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
import { ref } from "vue"
const ghAccessToken = ref<string | null>(null)
const ghUsername = ref<string | null>(null)
const atIsLoggedIn = ref(false)
const atHandle = ref<string | null>(null)
const atAvatarUrl = ref<string | null>(null)
vi.mock("@/hooks/useGitHubLogin.hook", () => ({
useGitHubLogin: () => ({
username: ghUsername,
accessToken: ghAccessToken
})
}))
vi.mock("@/hooks/useATProtoLogin.hook", () => ({
useATProtoLogin: () => ({
isLoggedIn: atIsLoggedIn,
handle: atHandle,
avatarUrl: atAvatarUrl
})
}))
import UserPill from "./UserPill.vue"
describe("UserPill", () => {
beforeEach(() => {
ghAccessToken.value = null
ghUsername.value = null
atIsLoggedIn.value = false
atHandle.value = null
atAvatarUrl.value = null
})
afterEach(() => {
vi.clearAllMocks()
})
it("shows the ghost 'Sign in' pill when nobody is logged in", () => {
const wrapper = mount(UserPill)
expect(wrapper.text()).toContain("Sign in")
expect(wrapper.classes()).toContain("profile-chip--ghost")
})
it("shows the GitHub username and initial when logged in via GitHub only", () => {
ghAccessToken.value = "tok"
ghUsername.value = "alice"
const wrapper = mount(UserPill)
expect(wrapper.text()).toContain("alice")
expect(wrapper.find(".profile-avatar-initial").text()).toBe("A")
expect(wrapper.find("img").exists()).toBe(false)
expect(wrapper.classes()).not.toContain("profile-chip--ghost")
})
it("shows the ATProto handle and avatar when logged in via ATProto", () => {
atIsLoggedIn.value = true
atHandle.value = "alice.bsky.social"
atAvatarUrl.value = "https://cdn.example.com/avatar.jpg"
const wrapper = mount(UserPill)
expect(wrapper.text()).toContain("alice.bsky.social")
const img = wrapper.find("img")
expect(img.exists()).toBe(true)
expect(img.attributes("src")).toBe("https://cdn.example.com/avatar.jpg")
})
it("prefers the ATProto handle over the GitHub username when both are present", () => {
ghAccessToken.value = "tok"
ghUsername.value = "alice-gh"
atIsLoggedIn.value = true
atHandle.value = "alice.bsky.social"
const wrapper = mount(UserPill)
expect(wrapper.text()).toContain("alice.bsky.social")
expect(wrapper.text()).not.toContain("alice-gh")
})
it("falls back to '?' initial when no name is available but a user is logged in", () => {
atIsLoggedIn.value = true
atHandle.value = ""
const wrapper = mount(UserPill)
expect(wrapper.find(".profile-avatar-initial").text()).toBe("?")
})
it("emits click when clicked", async () => {
const wrapper = mount(UserPill)
await wrapper.trigger("click")
expect(wrapper.emitted("click")).toHaveLength(1)
})
})