Skip to content

Commit 057a068

Browse files
authored
Merge pull request #30 from vanGalilea/chore/maintenance
Chore: maintenance and upgrading to v11
2 parents fdf7ff9 + e36413b commit 057a068

38 files changed

+3461
-3091
lines changed

.eslintrc.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,12 @@ module.exports = {
22
root: true,
33
extends: '@react-native-community',
44
parser: '@typescript-eslint/parser',
5-
plugins: ['@typescript-eslint'],
6-
};
5+
plugins: ['@typescript-eslint', 'jest'],
6+
rules: {
7+
semi: [2, 'never'],
8+
'react-native/no-inline-styles': 0,
9+
},
10+
env: {
11+
'jest/globals': true,
12+
},
13+
}

__tests__/Counter.test.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import 'react-native'
21
import React from 'react'
3-
import {fireEvent, render} from '@testing-library/react-native'
2+
import {cleanup, fireEvent, render, screen} from '@testing-library/react-native'
43
import Counter from '../src/components/Counter'
5-
import {expect, it} from '@jest/globals'
64

7-
it('renders correctly', () => {
8-
const {getByText} = render(<Counter />)
5+
afterEach(cleanup)
6+
7+
it('renders correctly after in/decrement action', () => {
8+
render(<Counter />)
9+
const {getByText} = screen
910

1011
const decrement = getByText(/decrement/i)
1112
const increment = getByText(/increment/i)

__tests__/CounterUsesCustomHook.test.tsx

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1-
import 'react-native'
21
import React from 'react'
3-
import {act, fireEvent, render} from '@testing-library/react-native'
2+
import {
3+
act,
4+
cleanup,
5+
fireEvent,
6+
render,
7+
screen,
8+
} from '@testing-library/react-native'
49
import CounterUsesCustomHook from '../src/components/CounterUsesCustomHook'
510
import useCounter from '../src/hooks/useCounter'
611
import {renderHook} from '@testing-library/react-hooks'
7-
import {expect, it, test} from '@jest/globals'
812

9-
//testing with the component
13+
afterEach(cleanup)
14+
1015
it('exposes the count and increment/decrement functions', () => {
11-
const {getByText} = render(<CounterUsesCustomHook />)
16+
render(<CounterUsesCustomHook />)
17+
const {getByText} = screen
1218

1319
const decrement = getByText(/decrement/i)
1420
const increment = getByText(/increment/i)
@@ -22,18 +28,17 @@ it('exposes the count and increment/decrement functions', () => {
2228
})
2329

2430
// @ts-ignore
25-
function setup({initialProps} = {}) {
31+
const setup = ({initialProps} = {}) => {
2632
const result: any = {current: null}
27-
function TestComponent(props: any) {
33+
const TestComponent = (props: any) => {
2834
result.current = useCounter(props)
2935
return null
3036
}
3137
render(<TestComponent {...initialProps} />)
3238
return result
3339
}
3440

35-
//testing without component
36-
test('exposes the count and increment/decrement functions', () => {
41+
it('exposes the count and increment/decrement functions- without component', () => {
3742
const result = setup()
3843
expect(result.current.count).toBe(0)
3944
act(() => result.current.increment())
@@ -42,12 +47,12 @@ test('exposes the count and increment/decrement functions', () => {
4247
expect(result.current.count).toBe(0)
4348
})
4449

45-
test('allows customization of the initial count', () => {
50+
it('allows customization of the initial count', () => {
4651
const result = setup({initialProps: {initialCount: 3}})
4752
expect(result.current.count).toBe(3)
4853
})
4954

50-
test('allows customization of the step', () => {
55+
it('allows customization of the step', () => {
5156
const result = setup({initialProps: {step: 2}})
5257
expect(result.current.count).toBe(0)
5358
act(() => result.current.increment())
@@ -56,7 +61,7 @@ test('allows customization of the step', () => {
5661
expect(result.current.count).toBe(0)
5762
})
5863

59-
test('exposes the count and increment/decrement functions', () => {
64+
it('exposes the count and increment/decrement functions- hook only', () => {
6065
const {result} = renderHook(useCounter)
6166
expect(result.current.count).toBe(0)
6267
act(() => result.current.increment())

__tests__/EasyButton.test.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import 'react-native'
22
import React from 'react'
33
import EasyButton from '../src/components/EasyButton'
44
import {render} from '../src/test/test-utils'
5-
import {expect, it} from '@jest/globals'
65
import {ReactTestInstance} from 'react-test-renderer'
6+
import {cleanup, screen} from '@testing-library/react-native'
7+
8+
afterEach(cleanup)
79

810
it('renders with the light styles for the light theme', () => {
9-
const {getByText} = render(<EasyButton>Click me!</EasyButton>)
11+
render(<EasyButton>Click me!</EasyButton>)
12+
const {getByText} = screen
13+
1014
const innerText = getByText(/click/i)
1115
const button = innerText.parent as ReactTestInstance
1216

@@ -15,9 +19,11 @@ it('renders with the light styles for the light theme', () => {
1519
})
1620

1721
it('renders with the dark styles for the dark theme', () => {
18-
const {getByText} = render(<EasyButton>Click me!</EasyButton>, {
22+
render(<EasyButton>Click me!</EasyButton>, {
1923
theme: 'dark',
2024
})
25+
const {getByText} = screen
26+
2127
const innerText = getByText(/click/i)
2228
const button = innerText.parent as ReactTestInstance
2329

__tests__/FlatList.test.tsx

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,48 @@
1-
import 'react-native'
2-
// @ts-ignore
31
import React from 'react'
42
import {
3+
cleanup,
54
fireEvent,
65
render,
6+
screen,
77
waitFor,
88
waitForElementToBeRemoved,
99
} from '@testing-library/react-native'
10-
import {expect, it} from '@jest/globals'
1110
import SectionList from '../src/components/FlatList'
1211

1312
const eventData = {
1413
nativeEvent: {
1514
contentOffset: {
16-
x: 0,
17-
y: 425,
15+
y: 500,
1816
},
1917
contentSize: {
2018
// Dimensions of the scrollable content
21-
height: 885,
22-
width: 328,
19+
height: 500,
20+
width: 100,
2321
},
2422
layoutMeasurement: {
2523
// Dimensions of the device
26-
height: 469,
27-
width: 328,
24+
height: 100,
25+
width: 100,
2826
},
2927
},
3028
}
3129

30+
afterEach(cleanup)
31+
3232
it('scrolls to top and refreshes all items', async () => {
33-
const {getByText, getByTestId} = render(<SectionList />)
33+
render(<SectionList />)
34+
const {getByText, getByTestId} = screen
3435

3536
getByText(/pizza/i)
3637
expect(() => getByText(/the impossible burger/i)).toThrow(
3738
'Unable to find an element with text: /the impossible burger/i',
3839
) //intially not shown
3940
fireEvent.scroll(getByTestId('flat-list'), eventData)
40-
await waitForElementToBeRemoved(() => getByText(/loading more dishes/i))
41-
await waitFor(() => expect(getByText(/the impossible burger/i)))
41+
await waitForElementToBeRemoved(() => getByText(/loading more dishes/i), {
42+
timeout: 1500,
43+
})
44+
45+
await waitFor(() => {
46+
expect(getByText(/the impossible burger/i)).toBeTruthy()
47+
})
4248
})

__tests__/Home.test.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import React from 'react'
2-
import {fireEvent, render, waitFor} from '@testing-library/react-native'
2+
import {
3+
cleanup,
4+
fireEvent,
5+
render,
6+
screen,
7+
waitFor,
8+
} from '@testing-library/react-native'
39
import App from '../src/components/App'
4-
import {expect, it, jest} from '@jest/globals'
10+
11+
afterEach(cleanup)
512

613
//mocking async storage module
714
const mockedSetItem = jest.fn()
@@ -10,7 +17,8 @@ jest.mock('@react-native-community/async-storage', () => ({
1017
}))
1118

1219
it('renders/navigates throughout app screens', async () => {
13-
const {getByText} = render(<App />)
20+
render(<App />)
21+
const {getByText} = screen
1422
const homeText = getByText(/home/i)
1523
expect(homeText).not.toBeNull()
1624
fireEvent.press(getByText(/counter/i))

__tests__/ListWithFetch.test.tsx

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import 'react-native'
21
import React from 'react'
3-
import {render} from '@testing-library/react-native'
2+
import {cleanup, render, screen} from '@testing-library/react-native'
43
import ListWithFetch from '../src/components/ListWithFetch'
54
import {server} from '../src/test/mocks/server'
65
import {rest} from 'msw'
76

7+
afterEach(cleanup)
8+
89
test('displays images from the server', async () => {
9-
const {getByLabelText, findAllByLabelText, queryByLabelText} = render(
10-
<ListWithFetch />,
11-
)
10+
render(<ListWithFetch />)
11+
const {getByLabelText, findAllByLabelText, queryByLabelText} = screen
1212

1313
// show loading spinner
1414
const loadingSpinner = getByLabelText(/loader/i)
1515
expect(loadingSpinner).not.toBeUndefined()
1616

1717
//load images from server
18-
const images = await findAllByLabelText(/flavor/i)
19-
expect(images).toHaveLength(2)
18+
const userContainers = await findAllByLabelText(/user-container/i)
19+
expect(userContainers).toHaveLength(10)
2020

2121
//loading spinner no longer shows
2222
expect(queryByLabelText(/loader/i)).toBeNull()
@@ -25,19 +25,15 @@ test('displays images from the server', async () => {
2525
expect(queryByLabelText(/alert/i)).toBeNull()
2626
})
2727

28-
test('displays error upon error esponse from server', async () => {
28+
test('displays error upon error response from server', async () => {
2929
server.resetHandlers(
30-
rest.get(
31-
'https://4ec38857-2800-4f07-838e-535a78cf7d51.mock.pstmn.io/flavors',
32-
(res, req, ctx) => {
33-
// @ts-ignore
34-
res(ctx.status(500))
35-
},
36-
),
37-
)
38-
const {getByLabelText, getByText, findByLabelText, queryByLabelText} = render(
39-
<ListWithFetch />,
30+
rest.get('https://random-data-api.com/api/v2/users', (res, req, ctx) => {
31+
// @ts-ignore
32+
res(ctx.status(500))
33+
}),
4034
)
35+
render(<ListWithFetch />)
36+
const {findByLabelText, getByLabelText, getByText, queryByLabelText} = screen
4137

4238
// show loading spinner
4339
const loadingSpinner = getByLabelText(/loader/i)

__tests__/Login.test.tsx

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import 'react-native'
21
import React from 'react'
3-
import {fireEvent, render} from '@testing-library/react-native'
2+
import {cleanup, fireEvent, render, screen} from '@testing-library/react-native'
43
import Login from '../src/components/Login'
5-
import {expect, it, jest} from '@jest/globals'
4+
5+
afterEach(cleanup)
66

77
it('renders correctly', async () => {
88
const username = 'hi'
99
const password = 'qwerty1234'
1010
let submittedData = {}
11-
// @ts-ignore
1211
const handleSubmit = jest.fn(data => (submittedData = data))
13-
const {getByText, getByPlaceholderText} = render(
14-
<Login onSubmit={handleSubmit} />,
15-
)
12+
render(<Login onSubmit={handleSubmit} />)
13+
const {getByText, getByPlaceholderText} = screen
1614
const button = getByText(/submit/i)
1715

1816
await fireEvent.changeText(getByPlaceholderText(/username/i), username)

__tests__/LoginSubmission.test.tsx

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import 'react-native'
22
import React from 'react'
3-
import {fireEvent, render, waitFor} from '@testing-library/react-native'
3+
import {
4+
cleanup,
5+
fireEvent,
6+
render,
7+
screen,
8+
waitFor,
9+
} from '@testing-library/react-native'
410
import LoginSubmission from '../src/components/LoginSubmission'
5-
import {useNavigation} from '@react-navigation/native'
611
import AsyncStorage from '@react-native-community/async-storage'
12+
import {useNavigationMock} from '../src/test/test-utils'
713

814
jest.mock('@react-native-community/async-storage', () => ({setItem: jest.fn()}))
9-
1015
jest.mock('@react-navigation/native', () => {
1116
return {
1217
createNavigatorFactory: jest.fn(),
@@ -18,48 +23,44 @@ jest.mock('@react-navigation/stack', () => ({
1823
}))
1924
jest.mock('@react-native-community/masked-view', () => ({}))
2025

26+
afterEach(cleanup)
2127
beforeEach(() => {
22-
// @ts-ignore
23-
useNavigation.mockReset()
28+
useNavigationMock.mockReset()
2429
})
25-
2630
it('renders correctly', async () => {
31+
const fetchMock = global.fetch as jest.MockedFunction<typeof global.fetch>
2732
const mockNavigate = jest.fn()
28-
// @ts-ignore
29-
useNavigation.mockImplementation(() => ({navigate: mockNavigate}))
30-
const fakeResponse = Promise.resolve({token: 'fake-token'})
31-
// @ts-ignore
32-
global.fetch.mockResolvedValueOnce({
33+
useNavigationMock.mockImplementation(() => ({navigate: mockNavigate}))
34+
fetchMock.mockResolvedValueOnce({
3335
json: () => Promise.resolve({token: 'fake-token'}),
34-
})
35-
36+
} as Response | Awaited<Response>)
3637
const username = 'chucknorris'
3738
const password = 'i need no password'
38-
const {getByText, getByPlaceholderText} = render(<LoginSubmission />)
39+
40+
render(<LoginSubmission />)
41+
const {getByText, getByPlaceholderText} = screen
3942
const button = getByText(/submit/i)
4043

4144
fireEvent.changeText(getByPlaceholderText(/username/i), username)
4245
fireEvent.changeText(getByPlaceholderText(/password/i), password)
4346
fireEvent.press(button)
4447

4548
getByText(/loading/i)
46-
// @ts-ignore
47-
expect(global.fetch).toHaveBeenCalledWith(
49+
expect(fetchMock).toHaveBeenCalledWith(
4850
'https://e2c168f9-97f3-42e1-8b31-57f4ab52a3bc.mock.pstmn.io/api/login',
4951
{
5052
method: 'POST',
5153
body: JSON.stringify({username, password}),
5254
headers: {'content-type': 'application/json'},
5355
},
5456
)
55-
// @ts-ignore
56-
expect(global.fetch.mock.calls).toMatchInlineSnapshot(`
57-
Array [
58-
Array [
57+
expect(fetchMock.mock.calls).toMatchInlineSnapshot(`
58+
[
59+
[
5960
"https://e2c168f9-97f3-42e1-8b31-57f4ab52a3bc.mock.pstmn.io/api/login",
60-
Object {
61-
"body": "{\\"username\\":\\"chucknorris\\",\\"password\\":\\"i need no password\\"}",
62-
"headers": Object {
61+
{
62+
"body": "{"username":"chucknorris","password":"i need no password"}",
63+
"headers": {
6364
"content-type": "application/json",
6465
},
6566
"method": "POST",

0 commit comments

Comments
 (0)