Skip to content

Commit

Permalink
feat: rename useAllPrismicDocuments to useAllPrismicDocumentsDangerously
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed Nov 18, 2021
1 parent 1e2b3a9 commit 22d63fe
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 51 deletions.
9 changes: 6 additions & 3 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ export const useFirstPrismicDocument = <
* @returns The composable payload {@link ClientHookReturnType}
* @see Underlying `@prismicio/client` method {@link proto.getAll}
*/
export const useAllPrismicDocuments = <
export const useAllPrismicDocumentsDangerously = <
TDocument extends prismicT.PrismicDocument,
>(
...args: [params?: ClientMethodParameters<"getAll">[0] & HookOnlyParameters]
...args: [
params?: ClientMethodParameters<"dangerouslyGetAll">[0] &
HookOnlyParameters,
]
): ClientHookReturnType<TDocument[]> =>
useStatefulPrismicClientMethod(proto.getAll, args);
useStatefulPrismicClientMethod(proto.dangerouslyGetAll, args);

/**
* A hook that queries a document from the Prismic repository with a specific ID.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export { PrismicToolbar } from "./PrismicToolbar";
export type { PrismicToolbarProps } from "./PrismicToolbar";

export {
useAllPrismicDocuments,
useAllPrismicDocumentsDangerously,
useAllPrismicDocumentsByEveryTag,
useAllPrismicDocumentsByIDs,
useAllPrismicDocumentsBySomeTags,
Expand Down
5 changes: 3 additions & 2 deletions test/__testutils__/createMockQueryHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as ava from "ava";
import * as msw from "msw";
import * as prismic from "@prismicio/client";
import * as prismicT from "@prismicio/types";

import { createQueryResponse } from "./createQueryResponse";
Expand All @@ -12,7 +11,9 @@ export const createMockQueryHandler = <
TDocument extends prismicT.PrismicDocument = prismicT.PrismicDocument,
>(
t: ava.ExecutionContext,
pagedResponses: Partial<prismic.Query<TDocument>>[] = [createQueryResponse()],
pagedResponses: Partial<prismicT.Query<TDocument>>[] = [
createQueryResponse(),
],
requiredSearchParams?: Record<
string,
string | number | (string | number)[] | undefined
Expand Down
9 changes: 4 additions & 5 deletions test/__testutils__/createQueryResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as prismic from "@prismicio/client";
import * as prismicT from "@prismicio/types";

import { createDocument } from "./createDocument";
Expand All @@ -7,15 +6,15 @@ export const createQueryResponse = <
TDocument extends prismicT.PrismicDocument = prismicT.PrismicDocument,
>(
docs: TDocument[] = [createDocument(), createDocument()],
overrides?: Partial<prismic.Query<TDocument>>,
): prismic.Query<TDocument> => ({
overrides?: Partial<prismicT.Query<TDocument>>,
): prismicT.Query<TDocument> => ({
page: 1,
results_per_page: docs.length,
results_size: docs.length,
total_results_size: docs.length,
total_pages: 1,
next_page: "",
prev_page: "",
next_page: null,
prev_page: null,
results: docs,
...overrides,
});
1 change: 1 addition & 0 deletions test/__testutils__/createQueryResponsePages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const createQueryResponsePages = <
createQueryResponse(documents, {
page: i + 1,
total_pages: arr.length,
next_page: i < numPages - 1 ? "next_page" : null,
}),
);
};
26 changes: 21 additions & 5 deletions test/useAllPrismicDocumentsByEveryTag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from "react";
import * as msw from "msw";
import * as mswNode from "msw/node";
import * as prismic from "@prismicio/client";
import { renderHook, cleanup } from "@testing-library/react-hooks";
import { renderHook, cleanup, WaitOptions } from "@testing-library/react-hooks";

import { createClient } from "./__testutils__/createClient";
import { createMockQueryHandler } from "./__testutils__/createMockQueryHandler";
Expand All @@ -32,6 +32,10 @@ const createWrapper = (client: prismic.Client): React.ComponentType => {
return (props) => <PrismicProvider client={client} {...props} />;
};

const waitForValueToChangeOptions: WaitOptions = {
timeout: 2000,
};

test.serial("returns documents with matching IDs", async (t) => {
const client = createClient(t);
const wrapper = createWrapper(client);
Expand All @@ -55,7 +59,10 @@ test.serial("returns documents with matching IDs", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand Down Expand Up @@ -87,7 +94,10 @@ test.serial("supports params", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand All @@ -113,7 +123,10 @@ test.serial("supports explicit client", async (t) => {
useAllPrismicDocumentsByEveryTag(tags, { client }),
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand All @@ -138,7 +151,10 @@ test.serial("returns failed state on error", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "failed");
await waitForValueToChange(
() => result.current[1].state === "failed",
waitForValueToChangeOptions,
);

t.true(result.current[1].error instanceof prismic.ForbiddenError);
t.is(result.current[0], undefined);
Expand Down
26 changes: 21 additions & 5 deletions test/useAllPrismicDocumentsByIDs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from "react";
import * as msw from "msw";
import * as mswNode from "msw/node";
import * as prismic from "@prismicio/client";
import { renderHook, cleanup } from "@testing-library/react-hooks";
import { renderHook, cleanup, WaitOptions } from "@testing-library/react-hooks";

import { createClient } from "./__testutils__/createClient";
import { createMockQueryHandler } from "./__testutils__/createMockQueryHandler";
Expand All @@ -32,6 +32,10 @@ const createWrapper = (client: prismic.Client): React.ComponentType => {
return (props) => <PrismicProvider client={client} {...props} />;
};

const waitForValueToChangeOptions: WaitOptions = {
timeout: 2000,
};

test.serial("returns documents with matching IDs", async (t) => {
const client = createClient(t);
const wrapper = createWrapper(client);
Expand All @@ -55,7 +59,10 @@ test.serial("returns documents with matching IDs", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand Down Expand Up @@ -87,7 +94,10 @@ test.serial("supports params", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand All @@ -113,7 +123,10 @@ test.serial("supports explicit client", async (t) => {
useAllPrismicDocumentsByIDs(documentIds, { client }),
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand All @@ -138,7 +151,10 @@ test.serial("returns failed state on error", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "failed");
await waitForValueToChange(
() => result.current[1].state === "failed",
waitForValueToChangeOptions,
);

t.true(result.current[1].error instanceof prismic.ForbiddenError);
t.is(result.current[0], undefined);
Expand Down
26 changes: 21 additions & 5 deletions test/useAllPrismicDocumentsBySomeTags.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from "react";
import * as msw from "msw";
import * as mswNode from "msw/node";
import * as prismic from "@prismicio/client";
import { renderHook, cleanup } from "@testing-library/react-hooks";
import { renderHook, cleanup, WaitOptions } from "@testing-library/react-hooks";

import { createClient } from "./__testutils__/createClient";
import { createMockQueryHandler } from "./__testutils__/createMockQueryHandler";
Expand All @@ -32,6 +32,10 @@ const createWrapper = (client: prismic.Client): React.ComponentType => {
return (props) => <PrismicProvider client={client} {...props} />;
};

const waitForValueToChangeOptions: WaitOptions = {
timeout: 2000,
};

test.serial("returns documents with matching IDs", async (t) => {
const client = createClient(t);
const wrapper = createWrapper(client);
Expand All @@ -55,7 +59,10 @@ test.serial("returns documents with matching IDs", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand Down Expand Up @@ -87,7 +94,10 @@ test.serial("supports params", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand All @@ -113,7 +123,10 @@ test.serial("supports explicit client", async (t) => {
useAllPrismicDocumentsBySomeTags(tags, { client }),
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand All @@ -138,7 +151,10 @@ test.serial("returns failed state on error", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "failed");
await waitForValueToChange(
() => result.current[1].state === "failed",
waitForValueToChangeOptions,
);

t.true(result.current[1].error instanceof prismic.ForbiddenError);
t.is(result.current[0], undefined);
Expand Down
26 changes: 21 additions & 5 deletions test/useAllPrismicDocumentsByTag.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as React from "react";
import * as msw from "msw";
import * as mswNode from "msw/node";
import * as prismic from "@prismicio/client";
import { renderHook, cleanup } from "@testing-library/react-hooks";
import { renderHook, cleanup, WaitOptions } from "@testing-library/react-hooks";

import { createClient } from "./__testutils__/createClient";
import { createMockQueryHandler } from "./__testutils__/createMockQueryHandler";
Expand All @@ -32,6 +32,10 @@ const createWrapper = (client: prismic.Client): React.ComponentType => {
return (props) => <PrismicProvider client={client} {...props} />;
};

const waitForValueToChangeOptions: WaitOptions = {
timeout: 2000,
};

test.serial("returns documents with matching IDs", async (t) => {
const client = createClient(t);
const wrapper = createWrapper(client);
Expand All @@ -55,7 +59,10 @@ test.serial("returns documents with matching IDs", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand Down Expand Up @@ -87,7 +94,10 @@ test.serial("supports params", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand All @@ -113,7 +123,10 @@ test.serial("supports explicit client", async (t) => {
useAllPrismicDocumentsByTag(tag, { client }),
);

await waitForValueToChange(() => result.current[1].state === "loaded");
await waitForValueToChange(
() => result.current[1].state === "loaded",
waitForValueToChangeOptions,
);

t.deepEqual(result.current[0], documents);
});
Expand All @@ -138,7 +151,10 @@ test.serial("returns failed state on error", async (t) => {
{ wrapper },
);

await waitForValueToChange(() => result.current[1].state === "failed");
await waitForValueToChange(
() => result.current[1].state === "failed",
waitForValueToChangeOptions,
);

t.true(result.current[1].error instanceof prismic.ForbiddenError);
t.is(result.current[0], undefined);
Expand Down
Loading

0 comments on commit 22d63fe

Please sign in to comment.