-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
64 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,136 +7,89 @@ interface ContextWrapperProps { | |
} | ||
|
||
vi.mock('react-oidc-context', () => ({ | ||
useAuth: vi.fn().mockImplementation(() => ({ | ||
signinRedirect: vi.fn().mockResolvedValue(true), | ||
signoutRedirect: vi.fn().mockResolvedValue(true), | ||
useAuth: () => ({ | ||
isAuthenticated: true, | ||
isLoading: false, | ||
user: { | ||
profile: undefined, | ||
}, | ||
})), | ||
})); | ||
|
||
vi.mock('@src/utils/auth', () => ({ | ||
getSignInRedirectUrl: vi.fn(() => 'mocked-redirect-url'), // Replace with the expected URL | ||
signinRedirect: vi.fn(), | ||
signoutRedirect: vi.fn(), | ||
}), | ||
})); | ||
|
||
describe('useAuth', () => { | ||
const OLD_ENV = process.env; | ||
beforeEach(() => { | ||
process.env = { ...OLD_ENV }; | ||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
const contextWrapper = ({ children }: ContextWrapperProps) => ( | ||
<RecoilRoot>{children}</RecoilRoot> | ||
); | ||
|
||
test('should call signIn with SSO and no configs', async () => { | ||
it('should set isSignedIn to true when authenticated with sso', async () => { | ||
const { result } = renderHook(() => useAuth(), { | ||
wrapper: contextWrapper, | ||
}); | ||
|
||
await act(async () => { | ||
result.current.signIn(true); | ||
}); | ||
expect(result.current.signIn).toBeTruthy(); | ||
}); | ||
|
||
test('should call signIn with SSO and available configs', async () => { | ||
process.env.SSO_AUTHORITY = 'http://localhost'; | ||
process.env.SSO_CLIENT_ID = 'dev-client'; | ||
expect(result.current.isSignedIn).toBe(true); | ||
}); | ||
|
||
it('should set isSignedIn to true when authenticated without sso', () => { | ||
const { result } = renderHook(() => useAuth(), { | ||
wrapper: contextWrapper, | ||
}); | ||
|
||
await act(async () => { | ||
result.current.signIn(true); | ||
act(() => { | ||
result.current.signIn(false); | ||
}); | ||
expect(result.current.signIn).toBeTruthy(); | ||
|
||
expect(result.current.isSignedIn).toBe(true); | ||
}); | ||
|
||
it('should set isSignedIn to true when authenticated with sso', () => { | ||
it('should sign out and set isSignedIn to false when authenticated', () => { | ||
const { result } = renderHook(() => useAuth(), { | ||
wrapper: contextWrapper, | ||
}); | ||
|
||
act(() => { | ||
result.current.signOut(); | ||
}); | ||
|
||
expect(result.current.isSignedIn).toBe(false); | ||
}); | ||
|
||
it('should set isSignedIn to true when authenticated and with profile', async () => { | ||
vi.mock('react-oidc-context', () => ({ | ||
useAuth: () => ({ | ||
isAuthenticated: true, | ||
isLoading: false, | ||
user: { | ||
profile: { | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
displayName: 'John Doe', | ||
emailAddress: '[email protected]', | ||
phoneNumber: '1234567890', | ||
}, | ||
}, | ||
signinRedirect: vi.fn(), | ||
signoutRedirect: vi.fn(), | ||
}), | ||
})); | ||
|
||
const { result } = renderHook(() => useAuth(), { | ||
wrapper: contextWrapper, | ||
}); | ||
|
||
await act(async () => { | ||
result.current.signIn(true); | ||
}); | ||
|
||
expect(result.current.isSignedIn).toBe(true); | ||
}); | ||
|
||
// it('should not authenticated with sso and error', () => { | ||
// vi.mock('react-oidc-context', () => ({ | ||
// useAuth: vi.fn().mockImplementation(() => ({ | ||
// signinRedirect: vi.fn().mockRejectedValue(true), | ||
// signoutRedirect: vi.fn(), | ||
// isAuthenticated: false, | ||
// })), | ||
// })); | ||
// const { result } = renderHook(() => useAuth(), { | ||
// wrapper: contextWrapper, | ||
// }); | ||
|
||
// act(() => { | ||
// result.current.signIn(true); | ||
// }); | ||
|
||
// expect(result.current.isSignedIn).toBe(true); | ||
// }); | ||
|
||
// it('should set isSignedIn to true when authenticated without sso', () => { | ||
// const { result } = renderHook(() => useAuth(), { | ||
// wrapper: contextWrapper, | ||
// }); | ||
|
||
// act(() => { | ||
// result.current.signIn(false); | ||
// }); | ||
|
||
// expect(result.current.isSignedIn).toBe(true); | ||
// }); | ||
|
||
// it('should sign out and set isSignedIn to false when authenticated', () => { | ||
// const { result } = renderHook(() => useAuth(), { | ||
// wrapper: contextWrapper, | ||
// }); | ||
|
||
// act(() => { | ||
// result.current.signOut(); | ||
// }); | ||
|
||
// expect(result.current.isSignedIn).toBe(false); | ||
// }); | ||
|
||
// it('should set isSignedIn to true when authenticated and with profile', () => { | ||
// vi.mock('react-oidc-context', () => ({ | ||
// useAuth: vi.fn().mockImplementation(() => ({ | ||
// signinRedirect: vi.fn().mockResolvedValue(true), | ||
// signoutRedirect: vi.fn().mockResolvedValue(true), | ||
// isAuthenticated: true, | ||
// user: { | ||
// profile: { | ||
// firstName: 'John', | ||
// lastName: 'Doe', | ||
// displayName: 'John Doe', | ||
// emailAddress: '[email protected]', | ||
// phoneNumber: '1234567890', | ||
// }, | ||
// }, | ||
// })), | ||
// })); | ||
// const { result } = renderHook(() => useAuth(), { | ||
// wrapper: contextWrapper, | ||
// }); | ||
|
||
// act(() => { | ||
// result.current.signIn(false); | ||
// }); | ||
|
||
// expect(result.current.isSignedIn).toBe(true); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters