This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes usage of waitFor to render component tests
- Loading branch information
Showing
5 changed files
with
37 additions
and
80 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 |
---|---|---|
@@ -1,38 +1,23 @@ | ||
import React from 'react'; | ||
import { screen, render, fireEvent, waitFor } from '@testing-library/react'; | ||
import { screen, render, fireEvent } from '@testing-library/react'; | ||
import { FusionAuthLoginButton } from '../components/FusionAuthLoginButton'; | ||
import { FusionAuthProvider } from '../providers/FusionAuthProvider'; | ||
import { mockUseFusionAuth } from './mocks/mockUseFusionAuth'; | ||
import { TEST_CONFIG } from './mocks/testConfig'; | ||
|
||
describe('FusionAuthLoginButton', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('Login buttons renders the correct text', async () => { | ||
await renderProvider(); | ||
expect(await screen.findByText('Login')).toBeInTheDocument(); | ||
}); | ||
|
||
test('Login button will call the useFusionAuth hook', async () => { | ||
test('Login button will call the useFusionAuth hook', () => { | ||
const login = jest.fn(); | ||
mockUseFusionAuth({ login }); | ||
|
||
await renderProvider(); | ||
const stateValue = 'state-value-for-login'; | ||
render( | ||
<FusionAuthProvider {...TEST_CONFIG}> | ||
<FusionAuthLoginButton state={stateValue} /> | ||
</FusionAuthProvider>, | ||
); | ||
|
||
fireEvent.click(screen.getByText('Login')); | ||
|
||
expect(login).toBeCalledWith('state'); | ||
expect(login).toHaveBeenCalledWith(stateValue); | ||
}); | ||
}); | ||
|
||
const renderProvider = () => { | ||
return waitFor(() => | ||
render( | ||
<FusionAuthProvider {...TEST_CONFIG}> | ||
<FusionAuthLoginButton state="state" /> | ||
</FusionAuthProvider>, | ||
), | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,38 +1,23 @@ | ||
import React from 'react'; | ||
import { screen, render, fireEvent, waitFor } from '@testing-library/react'; | ||
import { screen, render, fireEvent } from '@testing-library/react'; | ||
import { FusionAuthLogoutButton } from '../components/FusionAuthLogoutButton'; | ||
import { FusionAuthProvider } from '../providers/FusionAuthProvider'; | ||
import { mockUseFusionAuth } from './mocks/mockUseFusionAuth'; | ||
import { TEST_CONFIG } from './mocks/testConfig'; | ||
|
||
describe('FusionAuthLogoutButton', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('Logout buttons renders the correct text', async () => { | ||
await renderProvider(); | ||
expect(await screen.findByText('Logout')).toBeInTheDocument(); | ||
}); | ||
|
||
test('Logout button will call the useFusionAuth hook', async () => { | ||
test('Logout button will call the useFusionAuth hook', () => { | ||
const logout = jest.fn(); | ||
mockUseFusionAuth({ logout }); | ||
|
||
await renderProvider(); | ||
render( | ||
<FusionAuthProvider {...TEST_CONFIG}> | ||
<FusionAuthLogoutButton /> | ||
</FusionAuthProvider>, | ||
); | ||
|
||
fireEvent.click(screen.getByText('Logout')); | ||
|
||
expect(logout).toBeCalledWith(); | ||
expect(logout).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
const renderProvider = () => { | ||
return waitFor(() => | ||
render( | ||
<FusionAuthProvider {...TEST_CONFIG}> | ||
<FusionAuthLogoutButton /> | ||
</FusionAuthProvider>, | ||
), | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,38 +1,24 @@ | ||
import React from 'react'; | ||
import { screen, render, fireEvent, waitFor } from '@testing-library/react'; | ||
import { screen, render, fireEvent } from '@testing-library/react'; | ||
import { FusionAuthRegisterButton } from '../components/FusionAuthRegisterButton'; | ||
import { FusionAuthProvider } from '../providers/FusionAuthProvider'; | ||
import { mockUseFusionAuth } from './mocks/mockUseFusionAuth'; | ||
import { TEST_CONFIG } from './mocks/testConfig'; | ||
|
||
describe('FusionAuthRegisterButton', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('Register buttons renders the correct text', async () => { | ||
await renderProvider(); | ||
expect(await screen.findByText('Register Now')).toBeInTheDocument(); | ||
}); | ||
|
||
test('Register button will call the useFusionAuth hook', async () => { | ||
const register = jest.fn(); | ||
mockUseFusionAuth({ register }); | ||
|
||
await renderProvider(); | ||
const stateValue = 'state-value-for-register'; | ||
render( | ||
<FusionAuthProvider {...TEST_CONFIG}> | ||
<FusionAuthRegisterButton state={stateValue} /> | ||
</FusionAuthProvider>, | ||
); | ||
|
||
fireEvent.click(screen.getByText('Register Now')); | ||
|
||
expect(register).toBeCalledWith('state'); | ||
expect(register).toHaveBeenCalledWith(stateValue); | ||
}); | ||
}); | ||
|
||
const renderProvider = () => { | ||
return waitFor(() => | ||
render( | ||
<FusionAuthProvider {...TEST_CONFIG}> | ||
<FusionAuthRegisterButton state="state" /> | ||
</FusionAuthProvider>, | ||
), | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import { IFusionAuthContext } from '../../providers/FusionAuthProvider'; | ||
|
||
export const createContextMock = (context: Partial<IFusionAuthContext>) => ({ | ||
export const createContextMock = ( | ||
context: Partial<IFusionAuthContext>, | ||
): IFusionAuthContext => ({ | ||
login: context.login ?? jest.fn(), | ||
logout: context.logout ?? jest.fn(), | ||
register: context.register ?? jest.fn(), | ||
isAuthenticated: context.isAuthenticated ?? false, | ||
user: context.user ?? {}, | ||
refreshToken: context.refreshToken ?? jest.fn(), | ||
isLoading: context.isLoading ?? false, | ||
}); |
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