chore: add tests
This commit is contained in:
76
src/hooks/useEditionMode.spec.ts
Normal file
76
src/hooks/useEditionMode.spec.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { mount } from "@vue/test-utils"
|
||||
import { afterEach, describe, expect, it, vi } from "vitest"
|
||||
import { defineComponent, ref } from "vue"
|
||||
|
||||
const escape = ref(false)
|
||||
vi.mock("@vueuse/core", () => ({
|
||||
useMagicKeys: () => ({ escape })
|
||||
}))
|
||||
|
||||
import { useEditionMode } from "./useEditionMode"
|
||||
|
||||
const host = (slot: (api: ReturnType<typeof useEditionMode>) => void) =>
|
||||
mount(
|
||||
defineComponent({
|
||||
template: "<div/>",
|
||||
setup() {
|
||||
const api = useEditionMode()
|
||||
slot(api)
|
||||
return api
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
describe("useEditionMode", () => {
|
||||
afterEach(() => {
|
||||
escape.value = false
|
||||
})
|
||||
|
||||
it("starts in read mode", () => {
|
||||
let mode: unknown
|
||||
host((api) => {
|
||||
mode = api.mode.value
|
||||
})
|
||||
expect(mode).toBe("read")
|
||||
})
|
||||
|
||||
it("toggleMode flips read ↔ edit", () => {
|
||||
let api: ReturnType<typeof useEditionMode> | undefined
|
||||
host((a) => {
|
||||
api = a
|
||||
})
|
||||
|
||||
api!.toggleMode()
|
||||
expect(api!.mode.value).toBe("edit")
|
||||
|
||||
api!.toggleMode()
|
||||
expect(api!.mode.value).toBe("read")
|
||||
})
|
||||
|
||||
it("escape key exits edit mode", async () => {
|
||||
let api: ReturnType<typeof useEditionMode> | undefined
|
||||
host((a) => {
|
||||
a.toggleMode()
|
||||
api = a
|
||||
})
|
||||
|
||||
expect(api!.mode.value).toBe("edit")
|
||||
|
||||
escape.value = true
|
||||
await new Promise((r) => setTimeout(r, 0))
|
||||
|
||||
expect(api!.mode.value).toBe("read")
|
||||
})
|
||||
|
||||
it("escape key is a no-op when already in read mode", async () => {
|
||||
let api: ReturnType<typeof useEditionMode> | undefined
|
||||
host((a) => {
|
||||
api = a
|
||||
})
|
||||
|
||||
escape.value = true
|
||||
await new Promise((r) => setTimeout(r, 0))
|
||||
|
||||
expect(api!.mode.value).toBe("read")
|
||||
})
|
||||
})
|
||||
67
src/hooks/useForm.hook.spec.ts
Normal file
67
src/hooks/useForm.hook.spec.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { mount } from "@vue/test-utils"
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest"
|
||||
import { defineComponent } from "vue"
|
||||
|
||||
const push = vi.fn()
|
||||
vi.mock("vue-router", () => ({
|
||||
useRouter: () => ({ push })
|
||||
}))
|
||||
|
||||
import { useForm } from "./useForm.hook"
|
||||
|
||||
const host = () => {
|
||||
let api!: ReturnType<typeof useForm>
|
||||
mount(
|
||||
defineComponent({
|
||||
template: "<div/>",
|
||||
setup() {
|
||||
api = useForm()
|
||||
return api
|
||||
}
|
||||
})
|
||||
)
|
||||
return api
|
||||
}
|
||||
|
||||
describe("useForm", () => {
|
||||
beforeEach(() => {
|
||||
push.mockReset()
|
||||
})
|
||||
|
||||
it("starts with empty user and repo inputs", () => {
|
||||
const api = host()
|
||||
expect(api.userInput.value).toBe("")
|
||||
expect(api.repoInput.value).toBe("")
|
||||
})
|
||||
|
||||
it("submit is a no-op when userInput is empty", () => {
|
||||
const api = host()
|
||||
api.repoInput.value = "notes"
|
||||
|
||||
api.submit()
|
||||
|
||||
expect(push).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("submit is a no-op when repoInput is empty", () => {
|
||||
const api = host()
|
||||
api.userInput.value = "alice"
|
||||
|
||||
api.submit()
|
||||
|
||||
expect(push).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("submit pushes the FluxNoteView route with user/repo params", () => {
|
||||
const api = host()
|
||||
api.userInput.value = "alice"
|
||||
api.repoInput.value = "notes"
|
||||
|
||||
api.submit()
|
||||
|
||||
expect(push).toHaveBeenCalledWith({
|
||||
name: "FluxNoteView",
|
||||
params: { user: "alice", repo: "notes" }
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user