Skip to content

Commit

Permalink
test: update types
Browse files Browse the repository at this point in the history
  • Loading branch information
andipaetzold committed Aug 15, 2024
1 parent 1043439 commit f7c3e4c
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 108 deletions.
4 changes: 2 additions & 2 deletions src/__testfixtures__/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./newPromise";
export * from "./newSymbol";
export * from "./newPromise.js";
export * from "./newSymbol.js";
4 changes: 2 additions & 2 deletions src/auth/useAuthState.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { renderHook } from "@testing-library/react";
import { Auth, onAuthStateChanged, User } from "firebase/auth";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { newSymbol } from "../__testfixtures__";
import { useAuthState } from "./useAuthState";
import { newSymbol } from "../__testfixtures__/index.js";
import { useAuthState } from "./useAuthState.js";

vi.mock("firebase/auth", () => ({
onAuthStateChanged: vi.fn(),
Expand Down
4 changes: 2 additions & 2 deletions src/firestore/internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
Query,
} from "firebase/firestore";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { newSymbol } from "../__testfixtures__";
import { getDocFromSource, getDocsFromSource, isAggregateSpecEqual, isDocRefEqual, isQueryEqual } from "./internal";
import { newSymbol } from "../__testfixtures__/index.js";
import { getDocFromSource, getDocsFromSource, isAggregateSpecEqual, isDocRefEqual, isQueryEqual } from "./internal.js";

vi.mock("firebase/firestore", async () => {
const mod = await vi.importActual<typeof import("firebase/firestore")>("firebase/firestore");
Expand Down
4 changes: 2 additions & 2 deletions src/firestore/useDocument.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderHook } from "@testing-library/react";
import { expect, it, vi, describe, beforeEach } from "vitest";
import { useDocument } from "./useDocument";
import { newSymbol } from "../__testfixtures__";
import { useDocument } from "./useDocument.js";
import { newSymbol } from "../__testfixtures__/index.js";
import { DocumentReference, onSnapshot } from "firebase/firestore";

vi.mock("firebase/firestore", async () => ({
Expand Down
2 changes: 1 addition & 1 deletion src/firestore/useQueries.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Query, QuerySnapshot } from "firebase/firestore";
import { describe, expectTypeOf, it } from "vitest";
import { useQueries } from "./useQueries";
import { useQueries } from "./useQueries.js";

describe("useQueries", () => {
it("single query", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/firestore/useQueriesData.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Query } from "firebase/firestore";
import { describe, expectTypeOf, it } from "vitest";
import { useQueriesData } from "./useQueriesData";
import { useQueriesData } from "./useQueriesData.js";

describe("useQueriesData", () => {
it("single query", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/useGet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderHook, waitFor } from "@testing-library/react";
import { newPromise, newSymbol } from "../__testfixtures__";
import { useGet } from "./useGet";
import { newPromise, newSymbol } from "../__testfixtures__/index.js";
import { useGet } from "./useGet.js";
import { it, expect, beforeEach, describe, vi } from "vitest";

const result1 = newSymbol<string>("Result 1");
Expand Down
2 changes: 1 addition & 1 deletion src/internal/useIsMounted.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { renderHook } from "@testing-library/react";
import { expect, it } from "vitest";
import { useIsMounted } from "./useIsMounted";
import { useIsMounted } from "./useIsMounted.js";

it("should return `true` on first render", () => {
const { result } = renderHook(() => useIsMounted());
Expand Down
31 changes: 17 additions & 14 deletions src/internal/useListen.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { act, renderHook } from "@testing-library/react";
import { newSymbol } from "../__testfixtures__";
import { useListen } from "./useListen";
import { LoadingState } from "./useLoadingValue";
import { newSymbol } from "../__testfixtures__/index.js";
import { useListen } from "./useListen.js";
import { LoadingState } from "./useLoadingValue.js";
import { it, expect, beforeEach, describe, vi } from "vitest";

const result1 = newSymbol("Result 1");
Expand Down Expand Up @@ -50,7 +50,7 @@ describe("when changing ref", () => {
expect(onChange).toHaveBeenCalledTimes(1);

// emit value
act(() => onChange.mock.calls[0][1](result1));
act(() => onChange.mock.calls[0]![1](result1));
expect(result.current).toStrictEqual([result1, false, undefined]);

// change ref
Expand All @@ -69,7 +69,7 @@ describe("when changing ref", () => {
expect(onChange).toHaveBeenCalledTimes(1);

// emit value
act(() => onChange.mock.calls[0][1](result1));
act(() => onChange.mock.calls[0]![1](result1));
expect(result.current).toStrictEqual([result1, false, undefined]);

// change ref
Expand All @@ -79,14 +79,17 @@ describe("when changing ref", () => {
expect(onChangeUnsubscribe).toHaveBeenCalledTimes(1);

// emit value
act(() => onChange.mock.calls[1][1](result2));
act(() => onChange.mock.calls[1]![1](result2));
expect(result.current).toStrictEqual([result2, false, undefined]);
});

it("from undefined ref to defined", () => {
const { result, rerender } = renderHook(({ ref }) => useListen(ref, onChange, isEqual, LoadingState), {
initialProps: { ref: undefined },
});
const { result, rerender } = renderHook<unknown, { ref: unknown }>(
({ ref }) => useListen(ref, onChange, isEqual, LoadingState),
{
initialProps: { ref: undefined },
},
);

expect(onChangeUnsubscribe).toHaveBeenCalledTimes(0);
expect(onChange).toHaveBeenCalledTimes(0);
Expand Down Expand Up @@ -116,7 +119,7 @@ describe("when changing ref", () => {

it("should return emitted values", () => {
const { result } = renderHook(() => useListen(refA1, onChange, isEqual, LoadingState));
const setValue = onChange.mock.calls[0][1];
const setValue = onChange.mock.calls[0]![1];

expect(result.current).toStrictEqual([undefined, true, undefined]);

Expand All @@ -129,8 +132,8 @@ it("should return emitted values", () => {

it("should return emitted error", () => {
const { result } = renderHook(() => useListen(refA1, onChange, isEqual, LoadingState));
const setValue = onChange.mock.calls[0][1];
const setError = onChange.mock.calls[0][2];
const setValue = onChange.mock.calls[0]![1];
const setError = onChange.mock.calls[0]![2];

expect(result.current).toStrictEqual([undefined, true, undefined]);

Expand All @@ -147,13 +150,13 @@ it("resubscribes if `onChange` changes", async () => {
const { result, rerender } = renderHook(({ onChange }) => useListen(refA1, onChange, isEqual, LoadingState), {
initialProps: { onChange: onChange1 },
});
const setValue1 = onChange1.mock.calls[0][1];
const setValue1 = onChange1.mock.calls[0]![1];
act(() => setValue1(result1));
expect(result.current).toStrictEqual([result1, false, undefined]);

const onChange2 = vi.fn().mockReturnValue(onChangeUnsubscribe);
rerender({ onChange: onChange2 });
const setValue2 = onChange1.mock.calls[0][1];
const setValue2 = onChange1.mock.calls[0]![1];
act(() => setValue2(result2));
expect(result.current).toStrictEqual([result2, false, undefined]);
});
18 changes: 9 additions & 9 deletions src/internal/useLoadingValue.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, renderHook } from "@testing-library/react";
import { newSymbol } from "../__testfixtures__";
import { LoadingState, useLoadingValue } from "./useLoadingValue";
import { newSymbol } from "../__testfixtures__/index.js";
import { LoadingState, useLoadingValue } from "./useLoadingValue.js";
import { it, expect, describe } from "vitest";

const value = newSymbol("Value");
Expand Down Expand Up @@ -34,7 +34,7 @@ describe("initial state", () => {

describe("setValue", () => {
it("with undefined value", () => {
const { result } = renderHook(() => useLoadingValue<symbol>(value));
const { result } = renderHook(() => useLoadingValue(value));
act(() => result.current.setValue(undefined));

expect(result.current.value).toBeUndefined();
Expand All @@ -43,7 +43,7 @@ describe("setValue", () => {
});

it("with a value", () => {
const { result } = renderHook(() => useLoadingValue<symbol>(LoadingState));
const { result } = renderHook(() => useLoadingValue<typeof LoadingState | unknown>(LoadingState));
act(() => result.current.setValue(value));

expect(result.current.value).toBe(value);
Expand All @@ -52,7 +52,7 @@ describe("setValue", () => {
});

it("with error", () => {
const { result } = renderHook(() => useLoadingValue<symbol>(LoadingState));
const { result } = renderHook(() => useLoadingValue<typeof LoadingState | unknown>(LoadingState));
act(() => result.current.setError(error));

act(() => result.current.setValue(value));
Expand All @@ -65,7 +65,7 @@ describe("setValue", () => {

describe("setError", () => {
it("without value", () => {
const { result } = renderHook(() => useLoadingValue<symbol>(LoadingState));
const { result } = renderHook(() => useLoadingValue(LoadingState));
act(() => result.current.setError(error));

expect(result.current.value).toBeUndefined();
Expand All @@ -74,7 +74,7 @@ describe("setError", () => {
});

it("with value", () => {
const { result } = renderHook(() => useLoadingValue<symbol>(value));
const { result } = renderHook(() => useLoadingValue(value));
act(() => result.current.setError(error));

expect(result.current.value).toBeUndefined();
Expand All @@ -85,7 +85,7 @@ describe("setError", () => {

describe("setLoading", () => {
it("with value", () => {
const { result } = renderHook(() => useLoadingValue<symbol>(value));
const { result } = renderHook(() => useLoadingValue(value));
act(() => result.current.setLoading());

expect(result.current.value).toBeUndefined();
Expand All @@ -94,7 +94,7 @@ describe("setLoading", () => {
});

it("with error", () => {
const { result } = renderHook(() => useLoadingValue<symbol>(value));
const { result } = renderHook(() => useLoadingValue(value));
act(() => result.current.setError(error));

act(() => result.current.setLoading());
Expand Down
26 changes: 13 additions & 13 deletions src/internal/useMultiListen.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { act, renderHook } from "@testing-library/react";
import { newSymbol } from "../__testfixtures__";
import { useMultiListen } from "./useMultiListen";
import { newSymbol } from "../__testfixtures__/index.js";
import { useMultiListen } from "./useMultiListen.js";
import { it, expect, beforeEach, describe, vi } from "vitest";

const result1 = newSymbol("Result 1");
Expand Down Expand Up @@ -46,7 +46,7 @@ describe("when changing refs", () => {
expect(onChange).toHaveBeenCalledTimes(1);

// emit value
act(() => onChange.mock.calls[0][1](result1));
act(() => onChange.mock.calls[0]![1](result1));
expect(result.current).toStrictEqual([[result1, false, undefined]]);

// change ref
Expand All @@ -65,7 +65,7 @@ describe("when changing refs", () => {
expect(onChange).toHaveBeenCalledTimes(1);

// emit value
act(() => onChange.mock.calls[0][1](result1));
act(() => onChange.mock.calls[0]![1](result1));
expect(result.current).toStrictEqual([[result1, false, undefined]]);

// change ref
Expand All @@ -75,7 +75,7 @@ describe("when changing refs", () => {
expect(onChangeUnsubscribe).toHaveBeenCalledTimes(1);

// emit value
act(() => onChange.mock.calls[1][1](result2));
act(() => onChange.mock.calls[1]![1](result2));
expect(result.current).toStrictEqual([[result2, false, undefined]]);
});
});
Expand All @@ -86,7 +86,7 @@ describe("changing size", () => {
initialProps: { refs: [refA1] },
});

const setValue1 = onChange.mock.calls[0][1];
const setValue1 = onChange.mock.calls[0]![1];
act(() => setValue1(result1));
expect(result.current).toStrictEqual([[result1, false, undefined]]);

Expand All @@ -96,7 +96,7 @@ describe("changing size", () => {
[undefined, true, undefined],
]);

const setValue2 = onChange.mock.calls[1][1];
const setValue2 = onChange.mock.calls[1]![1];
act(() => setValue2(result2));
expect(result.current).toStrictEqual([
[result1, false, undefined],
Expand All @@ -109,8 +109,8 @@ describe("changing size", () => {
initialProps: { refs: [refA1, refB1] },
});

const setValue1 = onChange.mock.calls[0][1];
const setValue2 = onChange.mock.calls[1][1];
const setValue1 = onChange.mock.calls[0]![1];
const setValue2 = onChange.mock.calls[1]![1];
act(() => setValue1(result1));
act(() => setValue2(result2));
expect(result.current).toStrictEqual([
Expand All @@ -129,8 +129,8 @@ describe("changing size", () => {

it("should return emitted values", () => {
const { result } = renderHook(() => useMultiListen([refA1, refB1], onChange, isEqual));
const setValue1 = onChange.mock.calls[0][1];
const setValue2 = onChange.mock.calls[1][1];
const setValue1 = onChange.mock.calls[0]![1];
const setValue2 = onChange.mock.calls[1]![1];

expect(result.current).toStrictEqual([
[undefined, true, undefined],
Expand Down Expand Up @@ -164,8 +164,8 @@ it("should return emitted values", () => {

it("should return emitted error", () => {
const { result } = renderHook(() => useMultiListen([refA1, refB1], onChange, isEqual));
const setError1 = onChange.mock.calls[0][2];
const setError2 = onChange.mock.calls[1][2];
const setError1 = onChange.mock.calls[0]![2];
const setError2 = onChange.mock.calls[1]![2];

expect(result.current).toStrictEqual([
[undefined, true, undefined],
Expand Down
Loading

0 comments on commit f7c3e4c

Please sign in to comment.