Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

56 close button fix tests #57

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions e2e/team.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('shows the team in English', async ({ page }) => {
await expect(annLink).toHaveAttribute('target', '_blank')
})

test('shows the team in Japanese', async ({ page }) => {
test('shows the team in Japanese', async ({ page, viewport }) => {
await page.goto('/#/team')

// switch locale to Japanese
Expand All @@ -56,8 +56,13 @@ test('shows the team in Japanese', async ({ page }) => {

const teamContainer = page.getByLabel('team-container')

// click off to close sidebar
await teamContainer.click({ force: true })
// close the sidebar
if (viewport && viewport.width < 600) {
const closeButton = page.getByLabel('close-button')
await closeButton.click()
} else {
await teamContainer.click({ force: true })
}

const heading = teamContainer.getByText('✨ リーダーシップ・チーム ✨')
await expect(heading).toBeVisible()
Expand Down
4 changes: 3 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default defineConfig({
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
retries: process.env.CI ? 2 : 1,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be useful to retry once locally, also we will get saved traces

/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
Expand All @@ -29,6 +29,8 @@ export default defineConfig({
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
outputDir: './playwright-report',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will ensure our test results are saved when running in GitHub Actions



/* Configure projects for major browsers */
projects: [
Expand Down
20 changes: 17 additions & 3 deletions src/components/SideDrawer/DrawerContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@ import Box from '@mui/material/Box'
import Divider from '@mui/material/Divider'
import List from '@mui/material/List'
import ListItem from '@mui/material/ListItem'
import IconButton from '@mui/material/IconButton'
import CloseIcon from '@mui/icons-material/Close'
import Stack from '@mui/material/Stack'
import { useTheme } from '@mui/material/styles'
import useMediaQuery from '@mui/material/useMediaQuery'
import StyledNavLink from '../StyledNavLink/StyledNavLink'
import LocaleToggle from '../LocaleToggle/LocaleToggle'
import { useTranslation } from 'react-i18next'

const DrawerContents: FC = () => {
interface DrawerContentsProps {
closeDrawer: () => void
}

const DrawerContents: FC<DrawerContentsProps> = ({ closeDrawer }) => {
const theme = useTheme()
const { t } = useTranslation()

let navList = <></>
if (useMediaQuery(theme.breakpoints.down('sm'))) {
navList = (<>
<ListItem>
<StyledNavLink to='/'>Home</StyledNavLink>
<Stack direction='row' sx={{ width: '100%', m: 0, p: 0 }}>
<StyledNavLink to='/'>Home</StyledNavLink>
<Box sx={{ display: 'flex-grow', width: '100%' }} />
<IconButton onClick={closeDrawer} aria-label='close-button'>
<CloseIcon />
</IconButton>
</Stack>
</ListItem>
<ListItem>
<StyledNavLink to='/team'>{t('sidebar.team')}</StyledNavLink>
Expand All @@ -28,7 +42,7 @@ const DrawerContents: FC = () => {
</>)
}

return <Box sx={{ width: 300 }}>
return <Box sx={{ width: 300 }} aria-label='drawer-contents'>
<List>
{navList}
<ListItem>
Expand Down
2 changes: 1 addition & 1 deletion src/components/SideDrawer/SideDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const SideDrawer: FC = () => {
onClose={toggleDrawer(false)}
aria-label="drawer"
>
<DrawerContents />
<DrawerContents closeDrawer={() => { setOpen(false) }} />
</SwipeableDrawer>
</>
}
Expand Down
24 changes: 22 additions & 2 deletions src/components/SideDrawer/__test__/TestDrawerContents.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { render } from '@/tests/customRender'
import { screen } from '@testing-library/react'
import DrawerContents from '../DrawerContents'
import userEvent from '@testing-library/user-event'
import useMediaQuery from '@mui/material/useMediaQuery'

vi.mock('@mui/material/useMediaQuery', () => {
return {
default: vi.fn()
}
})

describe('DrawerContents', () => {
it('should display the DrawerContents', async () => {
render(<DrawerContents />)
render(<DrawerContents closeDrawer={() => { }} />)
const japanese = await screen.findByText('日本語')
expect(japanese).toBeVisible()
})

it('should call closeDrawer when clicking x on mobile viewport', async () => {
const mock = vi.fn(() => { })
const user = userEvent.setup()
vi.mocked(useMediaQuery).mockReturnValue(true)

render(<DrawerContents closeDrawer={mock} />)
const closeButton = await screen.findByLabelText('close-button')
await user.click(closeButton)

expect(mock).toHaveBeenCalled()
})

it.todo('should show the NavLinks on mobile screens')
})
33 changes: 32 additions & 1 deletion src/components/SideDrawer/__test__/TestSideDrawer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { describe, expect, it } from 'vitest'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { render } from '@/tests/customRender'
import { screen, waitFor } from '@testing-library/react'
import Box from '@mui/material/Box'
import Stack from '@mui/material/Stack'
import SideDrawer from '../SideDrawer'
import userEvent from '@testing-library/user-event'
import useMediaQuery from '@mui/material/useMediaQuery'

vi.mock('@mui/material/useMediaQuery', () => {
return {
default: vi.fn()
}
})

beforeEach(() => {
vi.resetAllMocks()
})

describe('SideDrawer', () => {
it('should initially render the Drawer closed', async () => {
Expand Down Expand Up @@ -61,4 +72,24 @@ describe('SideDrawer', () => {
expect(drawer).not.toBeVisible()
})
})

it('should close the Drawer when clicking the close icon on mobile view', async () => {
const user = userEvent.setup()
vi.mocked(useMediaQuery).mockReturnValue(true)

render(<SideDrawer />)

const button = await screen.findByLabelText('drawer-toggle-button')
await user.click(button)

const drawerContents = await screen.findByLabelText('drawer-contents')
expect(drawerContents).toBeVisible()

const closeButton = await screen.findByLabelText('close-button')
await user.click(closeButton)

await waitFor(() => {
expect(drawerContents).not.toBeVisible()
})
})
})
Loading