Skip to content

Commit

Permalink
Fix some more types
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianpd committed Aug 16, 2023
1 parent 4a4e75b commit e616bdf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
25 changes: 9 additions & 16 deletions packages/react/spec/auth/useSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { renderHook } from "@testing-library/react";
import { noUserModelApi, superAuthApi } from "../../spec/apis.js";
import { expectMockSignedInUser, expectMockSignedOutUser, mockInternalServerError, mockNetworkError } from "../../spec/utils.js";
import { useSession } from "../../src/auth/useSession.js";
import { MockClientWrapper, mockUrqlClient } from "../testWrappers.js";
import { MockClientWrapper } from "../testWrappers.js";

describe("useSession", () => {
test("it returns the current session when the user is logged in and no options are passed", async () => {
Expand Down Expand Up @@ -30,26 +30,19 @@ describe("useSession", () => {
});

test("it returns the current session when the user is logged in and api client with options is passed", async () => {
const { result:userResult, rerender } = renderHook(() => useSession(superAuthApi, {select: {user: { firstName: true}}}), { wrapper: MockClientWrapper(superAuthApi) });

// {
// id: '123',
// userId: '321',
// user: { id: '321', firstName: 'Jane', lastName: 'Doe' }
// } but we shouldnt be getting back lastname and id from user
const { result, rerender } = renderHook(() => useSession(superAuthApi, {select: {id: true, userId: true, user: { id: true, firstName: true}}}), { wrapper: MockClientWrapper(superAuthApi) });

expect(userResult.current.id).toEqual("123");
expect(userResult.current.userId).toEqual("321");

expect(userResult.current.user?.id).toEqual("321");
expect(userResult.current.user?.firstName).toEqual("Jane");
expect(userResult.current.user?.lastName).toEqual("Doe");
expectMockSignedInUser();
rerender();

expect(result.current.id).toEqual("123");
expect(result.current.userId).toEqual("321");
expect(result.current.user?.id).toEqual("321");
expect(result.current.user?.firstName).toEqual("Jane");

const { result: noUserResult, rerender: _noUserRerender } = renderHook(() => useSession(superAuthApi, {filter: {user: {firstName: {equals: "Bob"}}}}), { wrapper: MockClientWrapper(superAuthApi) });

expect(noUserResult.current).toBeNull();

});

test("it returns the current session when the user is logged out", async () => {
Expand Down
17 changes: 9 additions & 8 deletions packages/react/src/auth/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
LimitToKnownKeys,
Select,
} from "@gadgetinc/api-client-core";
import { useGet } from "../../src/useGet";
import { useGet } from "../useGet";
import type { OptionsType, ReadOperationOptions } from "../../src/utils";
import { useApi } from "../../src/GadgetProvider";

Expand Down Expand Up @@ -52,11 +52,10 @@ export function useSession<
>
>
> {
const fallbackApi = useApi() as any;
const api = client ?? fallbackApi;
const fallbackApi = useApi();
const api = client ?? (fallbackApi as ClientType);

if("currentSession" in api && "session" in api){
console.log('[jenny] here')
if(api && "currentSession" in api && "session" in api){
const opts:any = {
suspense: true,
select: {
Expand All @@ -65,13 +64,15 @@ export function useSession<
},
...(options ?? {}),
};

console.log('[jenny] the opts', opts)

const [{ data: session, error }] = useGet(api.currentSession, opts);

if (error) throw error;
if (!session) throw new Error("currentSession not found but should be present");
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return typeof client == "undefined" ? session : session as any;

// return typeof client == "undefined" ? session : session;
}else{
throw new Error("api client does not have a Session model");
}
Expand Down

0 comments on commit e616bdf

Please sign in to comment.