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

Add and improve unit tests #22

Merged
merged 2 commits into from
Apr 28, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"start": "react-native start",
"info": "react-native info",
"upgrade": "react-native upgrade",
"test": "jest",
"test": "jest --verbose",
"test:update-snapshots": "jest -u",
"test:ci": "jest --ci --no-cache --silent",
"test:coverage": "jest --collect-coverage",
Expand Down
23 changes: 3 additions & 20 deletions src/components/AllAppsLetterIndex.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fireEvent, screen } from '@testing-library/react-native'
import React from 'react'
import { initialStoreState } from '../../utils/test/data'
import { getAppForTestsByName, initialStoreState } from '../../utils/test/data'
import { renderWithProvider } from '../../utils/test/utils'
import { RootState } from '../store'
import AllAppsLetterIndex from './AllAppsLetterIndex'
Expand All @@ -20,13 +20,7 @@ describe('<AllAppsLetterIndex /> Tests', () => {
const customInitialState: RootState = {
...initialStoreState,
appsList: {
list: [
{
packageName: 'com.google.chrome',
name: 'Chrome',
icon: 'ICON',
},
],
list: [getAppForTestsByName('Chrome')!],
},
}

Expand All @@ -49,18 +43,7 @@ describe('<AllAppsLetterIndex /> Tests', () => {
const customInitialState: RootState = {
...initialStoreState,
appsList: {
list: [
{
packageName: 'com.google.chrome',
name: 'Chrome',
icon: 'ICON',
},
{
packageName: 'com.google.maps',
name: 'Maps',
icon: 'ICON',
},
],
list: [getAppForTestsByName('Chrome')!, getAppForTestsByName('Maps')!],
},
}

Expand Down
32 changes: 5 additions & 27 deletions src/components/Search.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fireEvent, screen } from '@testing-library/react-native'
import React from 'react'
import { initialStoreState } from '../../utils/test/data'
import { getAppForTestsByName, initialStoreState } from '../../utils/test/data'
import { renderWithProvider, renderWithProviderAndContexts } from '../../utils/test/utils'
import { RootState } from '../store'
import Search from './Search'
Expand Down Expand Up @@ -65,21 +65,11 @@ describe('<Search /> Tests', () => {
})

it('should set correct result list when using correct query and reset search values when clear button is pressed', () => {
const chromeAppDetails = getAppForTestsByName('Chrome')!
const customInitialState: RootState = {
...initialStoreState,
appsList: {
list: [
{
packageName: 'com.google.chrome',
name: 'Chrome',
icon: 'ICON',
},
{
packageName: 'com.google.maps',
name: 'Maps',
icon: 'ICON',
},
],
list: [chromeAppDetails, getAppForTestsByName('Maps')!],
},
appState: {
...initialStoreState.appState,
Expand All @@ -101,13 +91,7 @@ describe('<Search /> Tests', () => {
let currentState = store.getState()

expect(currentState.appState.search.query).toBe('chr')
expect(currentState.appState.search.result).toEqual([
{
packageName: 'com.google.chrome',
name: 'Chrome',
icon: 'ICON',
},
])
expect(currentState.appState.search.result).toEqual([chromeAppDetails])

const searchInputClearButton = screen.getByTestId('search-input-clear-button')

Expand All @@ -125,13 +109,7 @@ describe('<Search /> Tests', () => {
const customInitialState: RootState = {
...initialStoreState,
appsList: {
list: [
{
packageName: 'com.google.chrome',
name: 'Chrome',
icon: 'ICON',
},
],
list: [getAppForTestsByName('Chrome')!],
},
appState: {
...initialStoreState.appState,
Expand Down
41 changes: 41 additions & 0 deletions src/components/Settings/SettingsHeader.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { fireEvent, screen } from '@testing-library/react-native'
import React from 'react'
import { renderWithProvider } from '../../../utils/test/utils'
import { APP_ID } from '../../constants'
import { setDisplaySettings } from '../../slices/appState'
import * as AppsModule from '../../utils/apps-module'
import SettingsHeader from './SettingsHeader'

const useDispatchMock = jest.fn()

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useDispatch: () => useDispatchMock,
}))

describe('<SettingsHeader /> Tests', () => {
beforeAll(() => {
jest.spyOn(AppsModule, 'showAppDetails')
})

it('should render correctly and match snapshot', () => {
renderWithProvider(<SettingsHeader />)

expect(screen.toJSON()).toMatchSnapshot()
expect(screen.getByTestId('wrapper')).toBeOnTheScreen()
expect(screen.getByTestId('app-info-button')).toBeOnTheScreen()
})

it('should call function to display settings bottom sheet when pressed', () => {
renderWithProvider(<SettingsHeader />)

const appInfoButton = screen.getByTestId('app-info-button')

expect(appInfoButton).toBeOnTheScreen()

fireEvent.press(appInfoButton)

expect(useDispatchMock).toBeCalledWith(setDisplaySettings(false))
expect(AppsModule.showAppDetails).toBeCalledWith(APP_ID)
})
})
4 changes: 2 additions & 2 deletions src/components/Settings/SettingsHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const SettingsHeader = () => {
}

return (
<View style={styles.wrapper} testID='settings-header-wrapper'>
<View style={styles.wrapper} testID='wrapper'>
<Text style={styles.title}>
Context Settings
<Text style={styles.appInfoText}>
Expand All @@ -33,7 +33,7 @@ const SettingsHeader = () => {
onPress={onAppInfoClick}
android_disableSound={true}
android_ripple={appInfoIconRippleConfig}
testID='settings-header-app-info-button'>
testID='app-info-button'>
<CustomIcon name='information-outline' size={34} color='#808080' />
</Pressable>
</View>
Expand Down
20 changes: 14 additions & 6 deletions src/components/Settings/__snapshots__/Settings.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
"paddingBottom": 20,
}
}
testID="settings-header-wrapper"
testID="wrapper"
>
<Text
style={
Expand Down Expand Up @@ -223,7 +223,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
testID="settings-header-app-info-button"
testID="app-info-button"
>
<Text
allowFontScaling={false}
Expand Down Expand Up @@ -292,7 +292,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
"justifyContent": "space-between",
}
}
testID="settings-bottom-toggle-advanced-settings-button"
testID="toggle-settings-button"
>
<View
style={
Expand All @@ -303,6 +303,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
undefined,
]
}
testID="wrapper"
>
<Text
style={
Expand All @@ -313,6 +314,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
[],
]
}
testID="title"
>
Recent Apps
</Text>
Expand Down Expand Up @@ -384,7 +386,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
"justifyContent": "space-between",
}
}
testID="settings-bottom-toggle-advanced-settings-button"
testID="toggle-settings-button"
>
<View
style={
Expand All @@ -395,6 +397,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
undefined,
]
}
testID="wrapper"
>
<Text
style={
Expand All @@ -405,6 +408,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
[],
]
}
testID="title"
>
Pinned Apps
</Text>
Expand Down Expand Up @@ -476,7 +480,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
"justifyContent": "space-between",
}
}
testID="settings-bottom-toggle-advanced-settings-button"
testID="toggle-settings-button"
>
<View
style={
Expand All @@ -487,6 +491,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
undefined,
]
}
testID="wrapper"
>
<Text
style={
Expand All @@ -497,6 +502,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
[],
]
}
testID="title"
>
Favorite Apps
</Text>
Expand Down Expand Up @@ -568,7 +574,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
"justifyContent": "space-between",
}
}
testID="settings-bottom-toggle-advanced-settings-button"
testID="toggle-settings-button"
>
<View
style={
Expand All @@ -579,6 +585,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
undefined,
]
}
testID="wrapper"
>
<Text
style={
Expand All @@ -589,6 +596,7 @@ exports[`<Settings /> Tests should render modal correctly and match snapshot whe
[],
]
}
testID="title"
>
Advanced Settings
</Text>
Expand Down
Loading