Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/ls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe("ls", () => {
})

afterEach(() => {
vi.restoreAllMocks()
localStorage.clear()
storage = null
})
Expand Down
22 changes: 12 additions & 10 deletions src/once.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ describe("once", () => {
mockQuery = vi.fn()
})

afterEach(() => {
vi.restoreAllMocks()
})

it("should call the query function once", async () => {
once("123", mockQuery)
await delay()
expect(mockQuery).toHaveBeenCalledTimes(1)
})

it("should only call the query function once with the same ID", async () => {
Promise.all([
once("123", mockQuery),
Expand All @@ -41,4 +31,16 @@ describe("once", () => {
await delay()
expect(mockQuery).toHaveBeenCalledTimes(3)
})

it("should call the query function again if the key changes and then changes again", async () => {
once("123", mockQuery)
await delay()
expect(mockQuery).toHaveBeenCalledTimes(1)
once("234", mockQuery)
await delay()
expect(mockQuery).toHaveBeenCalledTimes(2)
once("123", mockQuery)
await delay()
expect(mockQuery).toHaveBeenCalledTimes(3)
})
})
8 changes: 4 additions & 4 deletions src/once.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const makeOnce = () => {
const calls = new Set()
return (id: string | number, query: () => void | Promise<void>) => {
if (!calls.has(id)) {
calls.add(id)
let latestCalledKey: string | number | undefined
return (key: string | number, query: () => void | Promise<void>) => {
if (latestCalledKey !== key) {
latestCalledKey = key
setTimeout(query)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/purity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe("purity", () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
app = undefined
vi.restoreAllMocks()
})

describe("counter app", () => {
Expand Down
5 changes: 5 additions & 0 deletions src/purity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export const init = <State extends Record<string, unknown>>(
* Forces html re-rendering with the current state
*/
function rerender() {
if (!rootComponent) {
throw new Error(
"Cannot rerender: app has not been mounted. Call mount() first."
)
}
const newNodesMap = rootComponent()
console.warn("🌀")
for (const [id, domNode] of domNodesMap) {
Expand Down
1 change: 0 additions & 1 deletion src/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe("router", () => {
})
afterEach(() => {
document.body.innerHTML = ""
vi.restoreAllMocks()
})
it("switches between different views depending on the url state", async () => {
const App = (): string => render`
Expand Down
3 changes: 0 additions & 3 deletions test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ export const setBeforeAndAfter = ({
beforeEach(() => {
console.warn = vi.fn()
})
afterEach(() => {
vi.restoreAllMocks()
})
afterAll(() => {
document.body.innerHTML = ""
})
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {defineConfig} from "vitest/config"
export default defineConfig({
test: {
environment: "jsdom",
restoreMocks: true,
coverage: {
reporter: ["text", "json-summary", "json"],
extension: [".ts"],
Expand Down
Loading