Skip to content

Commit

Permalink
Add and improve unit tests (#22)
Browse files Browse the repository at this point in the history
* Add and improve unit tests

* Fix failing tests
  • Loading branch information
razinj authored Apr 28, 2023
1 parent abe35b1 commit 64c885a
Show file tree
Hide file tree
Showing 26 changed files with 1,532 additions and 226 deletions.
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

0 comments on commit 64c885a

Please sign in to comment.