Skip to content

Commit aa07bd5

Browse files
authored
test: add tests for the "delete" hook (#322)
1 parent 8736d90 commit aa07bd5

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

tests/hooks/delete.test.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { Collection } from '#/src/collection.js'
2+
import type { HookEventListener } from '#/src/hooks.js'
3+
import z from 'zod'
4+
5+
it('invokes the delete hook when a record is deleted', async () => {
6+
const users = new Collection({
7+
schema: z.object({ id: z.number() }),
8+
})
9+
10+
const hook = vi.fn<HookEventListener<typeof users, 'delete'>>()
11+
users.hooks.on('delete', hook)
12+
13+
await users.createMany(5, (index) => ({ id: index + 1 }))
14+
15+
users.delete((q) => q.where({ id: 2 }))
16+
17+
expect(hook).toHaveBeenCalledOnce()
18+
expect(hook).toHaveBeenCalledWith(
19+
expect.objectContaining({
20+
data: { deletedRecord: { id: 2 } },
21+
}),
22+
)
23+
})
24+
25+
it('invokes the delete hook in the opposite order for every deleted record', async () => {
26+
const users = new Collection({
27+
schema: z.object({ id: z.number() }),
28+
})
29+
30+
const hook = vi.fn<HookEventListener<typeof users, 'delete'>>()
31+
users.hooks.on('delete', hook)
32+
33+
await users.createMany(5, (index) => ({ id: index + 1 }))
34+
35+
users.deleteMany((q) => q.where({ id: (id) => id >= 2 && id <= 4 }))
36+
37+
expect(hook).toHaveBeenCalledTimes(3)
38+
expect(hook).toHaveBeenNthCalledWith(
39+
1,
40+
expect.objectContaining({
41+
data: { deletedRecord: { id: 4 } },
42+
}),
43+
)
44+
expect(hook).toHaveBeenNthCalledWith(
45+
2,
46+
expect.objectContaining({
47+
data: { deletedRecord: { id: 3 } },
48+
}),
49+
)
50+
expect(hook).toHaveBeenNthCalledWith(
51+
3,
52+
expect.objectContaining({
53+
data: { deletedRecord: { id: 2 } },
54+
}),
55+
)
56+
})
57+
58+
it('does not delete the record if the delete ecent is prevented', async () => {
59+
const users = new Collection({
60+
schema: z.object({ id: z.number() }),
61+
})
62+
63+
const hook = vi.fn<HookEventListener<typeof users, 'delete'>>((event) => {
64+
event.preventDefault()
65+
})
66+
users.hooks.on('delete', hook)
67+
68+
await users.createMany(3, (index) => ({ id: index + 1 }))
69+
users.delete((q) => q.where({ id: 2 }))
70+
expect(hook).toHaveBeenCalledOnce()
71+
expect(users.all()).toEqual([{ id: 1 }, { id: 2 }, { id: 3 }])
72+
})
73+
74+
it('allows preventing the default for specific records', async () => {
75+
const users = new Collection({
76+
schema: z.object({ id: z.number() }),
77+
})
78+
79+
const hook = vi.fn<HookEventListener<typeof users, 'delete'>>((event) => {
80+
if (event.data.deletedRecord.id === 3) {
81+
event.preventDefault()
82+
}
83+
})
84+
users.hooks.on('delete', hook)
85+
86+
await users.createMany(5, (index) => ({ id: index + 1 }))
87+
88+
users.deleteMany((q) => q.where({ id: (id) => id >= 2 && id <= 4 }))
89+
90+
expect(hook).toHaveBeenCalledTimes(3)
91+
expect(hook).toHaveBeenNthCalledWith(
92+
1,
93+
expect.objectContaining({
94+
data: { deletedRecord: { id: 4 } },
95+
}),
96+
)
97+
expect(hook).toHaveBeenNthCalledWith(
98+
2,
99+
expect.objectContaining({
100+
data: { deletedRecord: { id: 3 } },
101+
}),
102+
)
103+
expect(hook).toHaveBeenNthCalledWith(
104+
3,
105+
expect.objectContaining({
106+
data: { deletedRecord: { id: 2 } },
107+
}),
108+
)
109+
110+
expect(users.all()).toEqual([{ id: 1 }, { id: 3 }, { id: 5 }])
111+
})

0 commit comments

Comments
 (0)