-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from jbrunton/test-header
test: header
- Loading branch information
Showing
9 changed files
with
142 additions
and
30 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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"user": { | ||
"id": "user:google-oauth2|123", | ||
"name": "Test User", | ||
"picture": "https://ui-avatars.com/api/?name=Joe+Bloggs", | ||
"email": "[email protected]" | ||
}, | ||
"rooms": [ | ||
{ | ||
"id": "room:100", | ||
"name": "Test Room", | ||
"ownerId": "user:google-oauth2|123", | ||
"contentPolicy": "private", | ||
"joinPolicy": "invite" | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "id": "system", "name": "System", "email": "[email protected]" } |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React from 'react' | ||
import { render } from '../../../test/fixtures' | ||
import { waitFor, screen } from '@testing-library/react' | ||
import { getAuthState } from '../../../features/auth/context' | ||
import { Header } from './header' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
describe('Header', () => { | ||
const signIn = vitest.fn() | ||
const signOut = vitest.fn() | ||
|
||
const getMenuButton = () => screen.getByRole('button', { name: 'Open Menu' }) | ||
const getMenu = () => screen.queryByRole('dialog') | ||
const getSignInButton = () => screen.getByRole('button', { name: 'Sign In' }) | ||
const getSignOutButton = () => screen.getByRole('button', { name: 'Sign Out Test User' }) | ||
|
||
const signedInState = () => | ||
getAuthState({ token: 'a1b2c3', isLoading: false, userName: 'Test User', signOut, signIn }) | ||
const signedOutState = () => getAuthState({ isLoading: false, signOut, signIn }) | ||
|
||
it('hides the navigation menu by default', () => { | ||
render(<Header />, { auth: signedOutState() }) | ||
expect(getMenuButton()).toBeVisible() | ||
expect(getMenu()).not.toBeInTheDocument() | ||
}) | ||
|
||
it('shows the navigation menu when the menu button is clicked', async () => { | ||
render(<Header />, { auth: signedOutState() }) | ||
await userEvent.click(getMenuButton()) | ||
expect(getMenu()).toBeVisible() | ||
}) | ||
|
||
describe('when signed out', () => { | ||
it('has a sign in button', async () => { | ||
// arrange | ||
render(<Header />, { | ||
auth: signedOutState(), | ||
}) | ||
|
||
// act | ||
await userEvent.click(getMenuButton()) | ||
|
||
// assert | ||
await waitFor(() => { | ||
expect(getSignInButton()).toBeVisible() | ||
}) | ||
|
||
await userEvent.click(getSignInButton()) | ||
|
||
expect(signIn).toHaveBeenCalledOnce() | ||
}) | ||
}) | ||
|
||
describe('when signed in', () => { | ||
it('has a sign out button', async () => { | ||
// arrange | ||
render(<Header />, { | ||
auth: signedInState(), | ||
}) | ||
|
||
// act | ||
await waitFor(() => { | ||
expect(getMenuButton()).toBeVisible() | ||
}) | ||
|
||
// assert | ||
userEvent.click(getMenuButton()) | ||
|
||
await waitFor(() => { | ||
expect(getSignOutButton()).toBeVisible() | ||
}) | ||
|
||
await userEvent.click(getSignOutButton()) | ||
|
||
expect(signOut).toHaveBeenCalledOnce() | ||
}) | ||
}) | ||
}) |
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