-
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.
- Loading branch information
Adrien Castagliola
committed
Feb 26, 2024
1 parent
a1342ad
commit acb5f8e
Showing
10 changed files
with
552 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import React from 'react'; | ||
import { TopAppBar } from '../../components/topAppBar/TopAppBar'; | ||
import { | ||
render, | ||
screen, | ||
setupFakeTimer, | ||
userEvent, | ||
waitForElementToBeRemoved, | ||
} from '../../shared/testUtils'; | ||
import { Text } from 'react-native'; | ||
import TopAppBarMenuItem from '../../components/topAppBar/TopAppBarMenuItem'; | ||
|
||
const topBarTitle = 'Menu'; | ||
|
||
describe('TopAppBar mounting with a simple title', () => { | ||
it('displays a title', () => { | ||
const title = { | ||
value: topBarTitle, | ||
}; | ||
|
||
render(<TopAppBar title={title} />); | ||
|
||
expect(screen.getByText(topBarTitle)).toBeOnTheScreen(); | ||
}); | ||
|
||
it.todo("testing menu title 'as string' on press"); | ||
}); | ||
|
||
describe('TopAppBar mounting with a title by passing a custom component', () => { | ||
it('displays a title', () => { | ||
const title = { | ||
value: <Text>{topBarTitle}</Text>, | ||
}; | ||
|
||
render(<TopAppBar title={title} />); | ||
|
||
expect(screen.getByText(topBarTitle)).toBeOnTheScreen(); | ||
}); | ||
|
||
it.todo("testing menu title 'as component' on press"); | ||
}); | ||
|
||
describe('TopAppBar mounting with a go back button', () => { | ||
it('triggers `goBack` event when user press the go back button', async () => { | ||
setupFakeTimer(); | ||
|
||
const user = userEvent.setup(); | ||
|
||
const mockOnGoBack = jest.fn(); | ||
|
||
const title = { | ||
value: topBarTitle, | ||
}; | ||
|
||
render(<TopAppBar title={title} onBack={mockOnGoBack} />); | ||
|
||
expect(mockOnGoBack).not.toHaveBeenCalled(); | ||
|
||
await user.press(screen.getByLabelText(/back/i)); | ||
|
||
expect(mockOnGoBack).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('TopAppBar mounting with a menu', () => { | ||
const noLongerMonitorMenuItem = { | ||
id: 'noLongerMonitor' as const, | ||
title: 'Ne plus surveiller', | ||
}; | ||
const title = { | ||
value: topBarTitle, | ||
}; | ||
let mockOnMenuItemPress: jest.Mock; | ||
|
||
beforeEach(() => { | ||
mockOnMenuItemPress = jest.fn(); | ||
|
||
const handlePress = | ||
(menuItem: typeof noLongerMonitorMenuItem, hideMenu: () => void) => | ||
() => { | ||
hideMenu(); | ||
mockOnMenuItemPress(menuItem); | ||
}; | ||
|
||
render( | ||
<TopAppBar | ||
title={title} | ||
menu={{ | ||
items: [noLongerMonitorMenuItem], | ||
renderItem: (menuItem, { hideMenu }) => { | ||
return ( | ||
<TopAppBarMenuItem | ||
id={menuItem.id} | ||
title={menuItem.title} | ||
onPress={handlePress(menuItem, hideMenu)} | ||
/> | ||
); | ||
}, | ||
}} | ||
/>, | ||
); | ||
}); | ||
|
||
it('displays action menu button', () => { | ||
expect(screen.getByLabelText(/menu/i)).toBeOnTheScreen(); | ||
}); | ||
|
||
it('displays menu items when user press the action menu button', async () => { | ||
setupFakeTimer(); | ||
|
||
const user = userEvent.setup(); | ||
|
||
expect( | ||
screen.queryByText(noLongerMonitorMenuItem.title), | ||
).not.toBeOnTheScreen(); | ||
|
||
await user.press(screen.getByLabelText(/menu/i)); | ||
|
||
expect( | ||
screen.getByText(noLongerMonitorMenuItem.title), | ||
).toBeOnTheScreen(); | ||
}); | ||
|
||
it('retreives menu item data when user press a menu item', async () => { | ||
setupFakeTimer(); | ||
|
||
const user = userEvent.setup(); | ||
|
||
await user.press(screen.getByLabelText(/menu/i)); | ||
|
||
expect(mockOnMenuItemPress).not.toHaveBeenCalled(); | ||
|
||
await user.press(screen.getByText(noLongerMonitorMenuItem.title)); | ||
|
||
expect(mockOnMenuItemPress).toHaveBeenCalledWith( | ||
noLongerMonitorMenuItem, | ||
); | ||
}); | ||
|
||
it('hides menu when user press a menu item', async () => { | ||
setupFakeTimer(); | ||
|
||
const user = userEvent.setup(); | ||
|
||
await user.press(screen.getByLabelText(/menu/i)); | ||
|
||
expect( | ||
screen.getByText(noLongerMonitorMenuItem.title), | ||
).toBeOnTheScreen(); | ||
|
||
await user.press(screen.getByText(noLongerMonitorMenuItem.title)); | ||
|
||
await waitForElementToBeRemoved(() => | ||
screen.queryByText(noLongerMonitorMenuItem.title), | ||
); | ||
}); | ||
}); |
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,53 @@ | ||
import React, { ComponentProps } from 'react'; | ||
import { | ||
setupFakeTimer, | ||
render, | ||
screen, | ||
userEvent, | ||
} from '../../shared/testUtils'; | ||
|
||
import TopAppBarMenuItem from '../../components/topAppBar/TopAppBarMenuItem'; | ||
|
||
describe('TopAppBarMenuItem mounting', () => { | ||
const title = 'Ne plus surveiller'; | ||
const id = 'noLongerMonitor'; | ||
const onPress = jest.fn(); | ||
|
||
it('displays a menu item', () => { | ||
render(<TopAppBarMenuItem id={id} onPress={onPress} title={title} />); | ||
|
||
expect(screen.getByText(title)).toBeOnTheScreen(); | ||
}); | ||
|
||
it('triggers onPress event when user press the menu item', async () => { | ||
setupFakeTimer(); | ||
|
||
const user = userEvent.setup(); | ||
|
||
render(<TopAppBarMenuItem id={id} onPress={onPress} title={title} />); | ||
|
||
await user.press(screen.getByText(title)); | ||
|
||
expect(onPress).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('TopAppBarMenuItem failing to mount', () => { | ||
const title = 'Ne plus surveiller'; | ||
const notExpectedId = '__not_expected_id__' as ComponentProps< | ||
typeof TopAppBarMenuItem | ||
>['id']; | ||
const onPress = jest.fn(); | ||
|
||
it('throw an error if menu item id is not handled', () => { | ||
expect(() => | ||
render( | ||
<TopAppBarMenuItem | ||
id={notExpectedId} | ||
onPress={onPress} | ||
title={title} | ||
/>, | ||
), | ||
).toThrowError(); | ||
}); | ||
}); |
Oops, something went wrong.