Skip to content

Commit f5d6001

Browse files
committed
✅ more tests (100% test coverage)
1 parent c9ecd99 commit f5d6001

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

Diff for: test.ts

+93-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import "fake-indexeddb/auto";
22

3-
import { describe, expect, test } from "vitest";
3+
import { describe, expect, test, vitest } from "vitest";
44
import { act, renderHook } from "@testing-library/react";
55
import useDb, { type UseDbOptions } from "./index.js";
6+
import { DbStorage } from "local-db-storage";
67

78
describe("use-db", () => {
89
describe("optimistic", () => {
@@ -65,7 +66,7 @@ describe("use-db", () => {
6566
expect(todos).toStrictEqual(["first", "second", "third", "forth"]);
6667
});
6768

68-
test("removes item from state", () => {
69+
test("removes item from state", async () => {
6970
const key = crypto.randomUUID();
7071
const { result } = renderHook(() =>
7172
useDb(key, {
@@ -74,9 +75,9 @@ describe("use-db", () => {
7475
);
7576

7677
{
77-
act(() => {
78+
await act(() => {
7879
const setTodos = result.current[1];
79-
setTodos(["third", "forth"]);
80+
return setTodos(["third", "forth"]);
8081
});
8182
const [todos] = result.current;
8283
expect(todos).toStrictEqual(["third", "forth"]);
@@ -166,6 +167,66 @@ describe("use-db", () => {
166167
const { unmount } = renderHook(() => useDb(key));
167168
unmount();
168169
});
170+
171+
test("set state throws an error", () => {
172+
const key = crypto.randomUUID();
173+
const { result } = renderHook(() => useDb(key));
174+
175+
vitest
176+
.spyOn(DbStorage.prototype, "setItem")
177+
.mockReturnValue(Promise.reject("QuotaExceededError"));
178+
179+
act(() => {
180+
const setState = result.current[1];
181+
setState("defined");
182+
});
183+
});
184+
185+
test("set state throws an error and reverts to previous state", async () => {
186+
const key = crypto.randomUUID();
187+
const { result } = renderHook(() => useDb(key));
188+
189+
await act(() => {
190+
const setState = result.current[1];
191+
return setState(1);
192+
});
193+
194+
vitest
195+
.spyOn(DbStorage.prototype, "setItem")
196+
.mockReturnValue(Promise.reject("QuotaExceededError"));
197+
198+
await act(() => {
199+
const setState = result.current[1];
200+
return setState(2);
201+
});
202+
203+
const [number] = result.current;
204+
expect(number).toBe(1);
205+
});
206+
207+
test("remove item throws an error and reverts to previous state", async () => {
208+
const key = crypto.randomUUID();
209+
const { result } = renderHook(() =>
210+
useDb(key, { defaultValue: 1 }),
211+
);
212+
213+
await act(() => {
214+
const [, setNumber] = result.current;
215+
return setNumber(2);
216+
});
217+
218+
vitest
219+
.spyOn(DbStorage.prototype, "removeItem")
220+
.mockReturnValue(Promise.reject("QuotaExceededError"));
221+
222+
await act(() => {
223+
const removeItem = result.current[2];
224+
return removeItem();
225+
});
226+
227+
const [number] = result.current;
228+
expect(number).toBe(2);
229+
});
169230
});
170231

171232
describe("non-optimistic", () => {
@@ -199,5 +260,33 @@ describe("use-db", () => {
199260
const [todos] = result.current;
200261
expect(todos).toStrictEqual(["third", "forth"]);
201262
});
263+
264+
test("removes item from state", async () => {
265+
const key = crypto.randomUUID();
266+
const { result } = renderHook(() =>
267+
useDb(key, {
268+
optimistic: false,
269+
defaultValue: ["first", "second"],
270+
}),
271+
);
272+
273+
{
274+
await act(() => {
275+
const setTodos = result.current[1];
276+
return setTodos(["third", "forth"]);
277+
});
278+
const [todos] = result.current;
279+
expect(todos).toStrictEqual(["third", "forth"]);
280+
}
281+
282+
{
283+
await act(() => {
284+
const removeItem = result.current[2];
285+
return removeItem();
286+
});
287+
const [todos] = result.current;
288+
expect(todos).toStrictEqual(["first", "second"]);
289+
}
290+
});
202291
});
203292
});

0 commit comments

Comments
 (0)