Skip to content

Commit

Permalink
Fixes from PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Markionium committed Mar 20, 2024
1 parent 24fa558 commit ab276b2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
81 changes: 61 additions & 20 deletions packages/nova-react/src/graphql/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ describe(useFragment, () => {
expect(result.current).toBeUndefined();
});

it("return type can be `null`, `undefined` or data", () => {
it("returns a value that can be `null`, `undefined` or data", () => {
type SomeFragment$data = { someKey: string };
type SomeFragment$key = {
readonly " $data"?: SomeFragment$data;
Expand All @@ -189,21 +189,26 @@ describe(useFragment, () => {
const fragment = {} as unknown as GraphQLTaggedNode;
const opaqueFragmentRef = {} as unknown as SomeFragment$key | null;

() => {
const data = useFragment(fragment, opaqueFragmentRef);
const { result } = renderHook(
() => useFragment(fragment, opaqueFragmentRef),
{
wrapper: ({ children }) => (
<NovaGraphQLProvider graphql={{}}>{children}</NovaGraphQLProvider>
),
},
);

type ExpectedReturnType = typeof data;
type ExpectedReturnType = typeof result.current;

const _: ExpectedReturnType = null;
const __: ExpectedReturnType = undefined;
const ___: ExpectedReturnType = { someKey: "some-data" };
const _: ExpectedReturnType = null;
const __: ExpectedReturnType = undefined;
const ___: ExpectedReturnType = { someKey: "some-data" };

// Workaround for TS complaining about unused variables
void _, __, ___;
};
// Workaround for TS complaining about unused variables
void _, __, ___;
});

it("return type does not include `null` or `undefined`", () => {
it("returns a value that can not be `null` or `undefined` when the fragment ref is not `null` or `undefined`", () => {
type SomeFragment$data = { someKey: string };
type SomeFragment$key = {
readonly " $data"?: SomeFragment$data;
Expand All @@ -213,18 +218,26 @@ describe(useFragment, () => {
const fragment = {} as unknown as GraphQLTaggedNode;
const opaqueFragmentRef = {} as unknown as SomeFragment$key;

() => {
const data = useFragment(fragment, opaqueFragmentRef);
const { result } = renderHook(
() => useFragment(fragment, opaqueFragmentRef),
{
wrapper: ({ children }) => (
<NovaGraphQLProvider graphql={{}}>{children}</NovaGraphQLProvider>
),
initialProps: {
fragmentRef: opaqueFragmentRef,
},
},
);

type ExpectedReturnType = typeof data;
type ExpectedReturnType = typeof result.current;

const _: IsNotNull<ExpectedReturnType> = true;
const __: IsNotUndefined<ExpectedReturnType> = true;
const ___: ExpectedReturnType = { someKey: "some-data" };
const _: IsNotNull<ExpectedReturnType> = true;
const __: IsNotUndefined<ExpectedReturnType> = true;
const ___: ExpectedReturnType = { someKey: "some-data" };

// Workaround for TS complaining about unused variables
void _, __, ___;
};
// Workaround for TS complaining about unused variables
void _, __, ___;
});
});

Expand Down Expand Up @@ -290,6 +303,34 @@ describe(useRefetchableFragment, () => {
);
});

it("supports passing null as reference to the fragment", () => {
const { result } = renderHook(
() => {
const fragment = {} as unknown as GraphQLTaggedNode;

return useRefetchableFragment(fragment, null);
},
{
wrapper: ({ children }) => (
<NovaGraphQLProvider
graphql={{
useRefetchableFragment: (_, ref) => [ref, jest.fn()],
}}
>
{children}
</NovaGraphQLProvider>
),
},
);

const [data] = result.current;

const _: null | undefined = data;
void _;

expect(result.current[0]).toBeNull();
});

it("supports passing null as reference to the fragment", () => {
const { result } = renderHook(
() => {
Expand Down
3 changes: 2 additions & 1 deletion packages/nova-react/src/graphql/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function useLazyLoadQuery<TQuery extends OperationType>(
*/
export function useFragment(
fragmentInput: GraphQLTaggedNode,
fragmentRef: null | undefined, // This overload makes sure we do not get `unknown` as the return type.
fragmentRef: null | undefined,
): null | undefined;
export function useFragment<TKey extends KeyType>(
fragmentInput: GraphQLTaggedNode,
Expand Down Expand Up @@ -223,6 +223,7 @@ export function useRefetchableFragment<

export function usePaginationFragment<
TQuery extends OperationType,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TKey extends null | undefined,
>(
fragmentInput: GraphQLTaggedNode,
Expand Down

0 comments on commit ab276b2

Please sign in to comment.