619 Create test utils for react testing library

This commit is contained in:
Lisa
2022-10-19 13:17:19 +00:00
committed by Sofiya Tepikin
parent 9dfe8b4b05
commit 1b5ffaf5d6
9 changed files with 468 additions and 427 deletions
-1
View File
@@ -17,7 +17,6 @@ i18n
compatibilityJSON: 'v3', // TODO: migrate json to v4 and afterwards remove it compatibilityJSON: 'v3', // TODO: migrate json to v4 and afterwards remove it
resources, resources,
fallbackLng: 'en', fallbackLng: 'en',
debug: true,
interpolation: { interpolation: {
escapeValue: false, // not needed for react as it escapes by default escapeValue: false, // not needed for react as it escapes by default
+4
View File
@@ -5,4 +5,8 @@ module.exports = {
transformIgnorePatterns: [ transformIgnorePatterns: [
'node_modules/(?!((jest-)?react-native(-.*)?|@react-native(-community)?)/)', 'node_modules/(?!((jest-)?react-native(-.*)?|@react-native(-community)?)/)',
], ],
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname',
],
} }
+2 -1
View File
@@ -68,7 +68,8 @@
"eslint": "7.14.0", "eslint": "7.14.0",
"eslint-plugin-react": "^7.8.2", "eslint-plugin-react": "^7.8.2",
"husky": "^8.0.0", "husky": "^8.0.0",
"jest": "^28.1.3", "jest": "^29.1.2",
"jest-watch-typeahead": "^2.2.0",
"jetifier": "^1.6.6", "jetifier": "^1.6.6",
"metro-react-native-babel-preset": "^0.66.2", "metro-react-native-babel-preset": "^0.66.2",
"prettier": "2.4.0", "prettier": "2.4.0",
+5 -13
View File
@@ -1,27 +1,19 @@
import React from 'react' import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react-native'
import AcceptLicense from '../components/AcceptLicense' import AcceptLicense from '../components/AcceptLicense'
import { saveLicenseFlag } from '../local-storage' import { saveLicenseFlag } from '../local-storage'
import { render, screen, fireEvent } from './test-utils'
jest.mock('../local-storage', () => ({ jest.mock('../local-storage', () => ({
saveLicenseFlag: jest.fn(() => Promise.resolve()), saveLicenseFlag: jest.fn(() => Promise.resolve()),
})) }))
jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (str, options) => {
return str + (options ? JSON.stringify(options) : '')
},
}),
}))
describe('AcceptLicense', () => { describe('AcceptLicense', () => {
test('On clicking OK button, the license is accepted', async () => { test('should accept license when clicking ok button', async () => {
const mockedSetLicense = jest.fn() const mockedSetLicense = jest.fn()
render(<AcceptLicense setLicense={mockedSetLicense} />) render(<AcceptLicense setLicense={mockedSetLicense} />)
const okButton = screen.getByText('ok', { exact: false }) const okButton = screen.getByText('OK')
fireEvent(okButton, 'click') fireEvent(okButton, 'click')
@@ -29,9 +21,9 @@ describe('AcceptLicense', () => {
expect(mockedSetLicense).toHaveBeenCalled() expect(mockedSetLicense).toHaveBeenCalled()
}) })
test('There is a Cancel button', async () => { test('should render cancel button', async () => {
render(<AcceptLicense setLicense={jest.fn()} />) render(<AcceptLicense setLicense={jest.fn()} />)
screen.getByText('cancel', { exact: false }) screen.getByText('Cancel')
}) })
}) })
+3 -11
View File
@@ -1,24 +1,16 @@
import React from 'react' import React from 'react'
import { render, screen } from '@testing-library/react-native'
import License from '../components/settings/License' import License from '../components/settings/License'
import { render, screen } from './test-utils'
jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (str, options) => {
return str + (options ? JSON.stringify(options) : '')
},
}),
}))
describe('License screen', () => { describe('License screen', () => {
test('It should have a correct year', async () => { test('should display license text with correct year', async () => {
render(<License />) render(<License />)
const year = new Date().getFullYear().toString() const year = new Date().getFullYear().toString()
screen.getByText(year, { exact: false }) screen.getByText(year, { exact: false })
}) })
test('It should match the snapshot', async () => { test('should match the snapshot', async () => {
const licenseScreen = render(<License />) const licenseScreen = render(<License />)
expect(licenseScreen).toMatchSnapshot() expect(licenseScreen).toMatchSnapshot()
+15 -13
View File
@@ -1,9 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`License screen It should match the snapshot 1`] = ` exports[`License screen should match the snapshot 1`] = `
<View <View
style={ style={
Object { {
"backgroundColor": "#E9F2ED", "backgroundColor": "#E9F2ED",
"flex": 1, "flex": 1,
} }
@@ -11,8 +11,8 @@ exports[`License screen It should match the snapshot 1`] = `
> >
<RCTScrollView <RCTScrollView
contentContainerStyle={ contentContainerStyle={
Array [ [
Object { {
"backgroundColor": "#E9F2ED", "backgroundColor": "#E9F2ED",
"flexGrow": 1, "flexGrow": 1,
}, },
@@ -23,13 +23,13 @@ exports[`License screen It should match the snapshot 1`] = `
<View> <View>
<Text <Text
style={ style={
Array [ [
Object { {
"color": "#555", "color": "#555",
"fontFamily": "Jost-Book", "fontFamily": "Jost-Book",
"fontSize": 34.285714285714285, "fontSize": 34.285714285714285,
}, },
Object { {
"alignSelf": "center", "alignSelf": "center",
"color": "#3A2671", "color": "#3A2671",
"fontFamily": "Jost-Bold", "fontFamily": "Jost-Bold",
@@ -41,11 +41,11 @@ exports[`License screen It should match the snapshot 1`] = `
] ]
} }
> >
title drip. an open-source cycle tracking app
</Text> </Text>
<View <View
style={ style={
Object { {
"marginBottom": 34.285714285714285, "marginBottom": 34.285714285714285,
"marginHorizontal": 34.285714285714285, "marginHorizontal": 34.285714285714285,
} }
@@ -53,8 +53,8 @@ exports[`License screen It should match the snapshot 1`] = `
> >
<Text <Text
style={ style={
Array [ [
Object { {
"color": "#555", "color": "#555",
"fontFamily": "Jost-Book", "fontFamily": "Jost-Book",
"fontSize": 34.285714285714285, "fontSize": 34.285714285714285,
@@ -63,12 +63,14 @@ exports[`License screen It should match the snapshot 1`] = `
] ]
} }
> >
text{"currentYear":2022} Copyright (C) 2022 Heart of Code e.V.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details:
</Text> </Text>
<Text <Text
onPress={[Function]} onPress={[Function]}
style={ style={
Object { {
"color": "#3A2671", "color": "#3A2671",
"fontFamily": "Jost-Book", "fontFamily": "Jost-Book",
"fontSize": 34.285714285714285, "fontSize": 34.285714285714285,
@@ -3,7 +3,7 @@
exports[`Footnote component when children are present, renders them 1`] = ` exports[`Footnote component when children are present, renders them 1`] = `
<View <View
style={ style={
Object { {
"alignContent": "flex-start", "alignContent": "flex-start",
"flexDirection": "row", "flexDirection": "row",
"marginBottom": 8.571428571428571, "marginBottom": 8.571428571428571,
@@ -13,13 +13,13 @@ exports[`Footnote component when children are present, renders them 1`] = `
> >
<Text <Text
style={ style={
Array [ [
Object { {
"color": "#555", "color": "#555",
"fontFamily": "Jost-Book", "fontFamily": "Jost-Book",
"fontSize": 34.285714285714285, "fontSize": 34.285714285714285,
}, },
Object { {
"color": "#F38337", "color": "#F38337",
}, },
] ]
@@ -29,18 +29,18 @@ exports[`Footnote component when children are present, renders them 1`] = `
</Text> </Text>
<Text <Text
linkStyle={ linkStyle={
Object { {
"color": "white", "color": "white",
} }
} }
style={ style={
Array [ [
Object { {
"color": "#555", "color": "#555",
"fontFamily": "Jost-Book", "fontFamily": "Jost-Book",
"fontSize": 34.285714285714285, "fontSize": 34.285714285714285,
}, },
Object { {
"color": "#555", "color": "#555",
"paddingLeft": 21.428571428571427, "paddingLeft": 21.428571428571427,
}, },
+10
View File
@@ -0,0 +1,10 @@
import { render } from '@testing-library/react-native'
import '../i18n/i18n'
const customRender = (ui, options) => render(ui, { ...options })
// re-export everything
export * from '@testing-library/react-native'
// override render method
export { customRender as render }
+421 -380
View File
File diff suppressed because it is too large Load Diff