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

update jest config #38

Merged
merged 1 commit into from
Jun 9, 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
8 changes: 4 additions & 4 deletions config/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default (): JestConfigWithTsJest => ({
}]
},
resolver: 'ts-jest-resolver',
testRegex: 'test/.*\\.(test|spec)?\\.(ts)$',
testRegex: 'test/.*\\.(test|spec)?\\.(ts|tsx)$',
// moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
preset: 'jest-expo',
transformIgnorePatterns: [
Expand All @@ -36,7 +36,7 @@ export default (): JestConfigWithTsJest => ({
},
collectCoverage: false,
collectCoverageFrom: [
'**/src/**/*.ts',
'**/src/**/*.{ts,tsx}',
'!**/coverage/**',
'!**/report/**',
'!**/assets/**',
Expand All @@ -49,9 +49,9 @@ export default (): JestConfigWithTsJest => ({
'!app.config.js',
'!metro.config.js',
'!react-native.config.js',
'!**/src/styles/**',
// '!**/src/styles/**',
'!**/src/consts/**',
'!**/src/components/**',
// '!**/src/components/**',
'!**/src/AppEntry.ts',
'!**/src/shim.ts',
// '!**/src/storage/store/AsyncStore.ts',
Expand Down
35 changes: 9 additions & 26 deletions test/components/Balance.test.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,22 @@
import Balance from '@comps/Balance'
import { render, fireEvent } from '@testing-library/react-native'
import { Locale } from 'expo-localization'
import { initDb } from '@src/storage/db'
import { fireEvent,render,screen } from '@testing-library/react-native'


// Mock the expo-sqlite openDatabase method
jest.mock('expo-sqlite', () => ({
openDatabase: () => ({
transaction: (callback: (tx: any) => void) => {
const mockExecuteSql = jest.fn()
const mockTx = { executeSql: mockExecuteSql }
callback(mockTx)
},
}),
}))
// Mock the expo-localization
jest.mock('expo-localization', () => {
return {
locale: 'de',
getLgetLocales: () => {
const arr: Locale[] = []
return arr
}
}
})
describe('Basic test of the Txt.tsx component', () => {
// eslint-disable-next-line no-return-await
beforeAll(async () => await initDb())
// Clear all mock function calls before each test
beforeEach(() => jest.clearAllMocks())
// Start tests
it('renders the expected string', () => {
const { getByText } = render(<Balance balance={69} />)
const textElement = getByText('69')
render(<Balance balance={69} />)
const textElement = screen.getByText('69')
expect(textElement).toBeDefined()
})
it('updates the balance format state on press', () => {
const { getByText } = render(<Balance balance={69} />)
const touchableElement = getByText('69')
render(<Balance balance={69} />)
const touchableElement = screen.getByText('69')
// Simulate press event
fireEvent.press(touchableElement)
expect(touchableElement.props.children).toBe('0.00000069')
Expand Down
14 changes: 7 additions & 7 deletions test/components/Txt.test.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import Txt from '@comps/Txt'
import { render } from '@testing-library/react-native'
import { render, screen } from '@testing-library/react-native'

describe('Basic test of the Txt.tsx component', () => {
it('renders the expected string', () => {
const { getByText } = render(<Txt txt='Hello World!' />)
const textElement = getByText('Hello World!')
render(<Txt txt='Hello World!' />)
const textElement = screen.getByText('Hello World!')
expect(textElement).toBeDefined()
})
it('has the global styles of a text component', () => {
const { getByText } = render(<Txt txt='Hello World!' />)
const textElement = getByText('Hello World!')
render(<Txt txt='Hello World!' />)
const textElement = screen.getByText('Hello World!')
expect(textElement.props.style[0]).toStrictEqual({
fontSize: 16,
color: '#656565'
})
})
it('has an additional specific style applied', () => {
const { getByText } = render(
render(
<Txt txt='Hello World!' styles={[{ marginTop: 10 }]} />
)
const textElement = getByText('Hello World!')
const textElement = screen.getByText('Hello World!')
expect(textElement.props.style[1].marginTop).toBe(10)
})
})
18 changes: 14 additions & 4 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// we always make sure 'react-native' gets included first
// import * as ReactNative from "react-native"
// eslint-disable-next-line simple-import-sort/imports
import * as ReactNative from 'react-native'

import { Locale } from 'expo-localization'

import { getDatabase } from './wrapper/getTestDb'

// libraries to mock
// jest.doMock("react-native", () => { return ReactNative })
jest.doMock('react-native', () => ReactNative)
jest.mock('expo-sqlite', () => ({
get openDatabase() {
return (_: string) => getDatabase(':memory:')
Expand All @@ -13,9 +17,15 @@ jest.mock('expo-sqlite', () => ({
jest.mock('expo-constants', () => ({}))
jest.mock('expo-secure-store', () => ({}))
jest.mock('@bugsnag/expo', () => ({}))
jest.mock('expo-localization', () => ({}))
jest.mock('expo-localization', () => ({
locale: 'de',
getLgetLocales: () => {
const arr: Locale[] = []
return arr
}
}))
jest.mock('reactotron-react-native', () => ({}))
jest.mock('react-native', () => ({}))
// jest.mock('react-native', () => ({}))
// jest.mock('@consts', () => ({}))
jest.mock('expo/config', () => ({}))

Expand Down