Skip to content

Commit 741534d

Browse files
migrate to React-Router
1 parent 1fe7f40 commit 741534d

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

src/App/NewTodoInput/index.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { fireEvent } from '@testing-library/react'
22
import React from 'react'
33

4-
import { renderWithRecoilRoot } from '../../testUtil'
4+
import { TestRenderer } from '../../testUtil'
55

66
import NewTodoTextInput from './index'
77

88
test('should be render <TodoTextInput/>', () => {
9-
const screen = renderWithRecoilRoot(<NewTodoTextInput />)
9+
const screen = TestRenderer(<NewTodoTextInput />)
1010
const input = screen.getByTestId('new-todo-input-text') as HTMLInputElement
1111

1212
// Header big text

src/App/TodoList/Item/index.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useRecoilState } from 'recoil'
44

55
import type { AppState } from '../../../dataStructure'
66
import { recoilState } from '../../../dataStructure'
7-
import { renderWithRecoilRoot } from '../../../testUtil'
7+
import { TestRenderer } from '../../../testUtil'
88

99
import Item from './index'
1010

@@ -29,7 +29,7 @@ const App = () => {
2929
}
3030

3131
test('should each initialAppstate todo object value is set to Item element', () => {
32-
renderWithRecoilRoot(
32+
TestRenderer(
3333
<Item todo={initialRecoilState.todoList[0]} />,
3434
initialRecoilState
3535
)
@@ -46,15 +46,15 @@ test('should each initialAppstate todo object value is set to Item element', ()
4646
})
4747

4848
test('should set css classes correctly', () => {
49-
renderWithRecoilRoot(<App />, initialRecoilState)
49+
TestRenderer(<App />, initialRecoilState)
5050

5151
// when not.completed & not.onEdit, SwitchStyle doesn't show .completed .editting selectors
5252
expect(screen.getByTestId('todo-item')).not.toHaveClass('completed')
5353
expect(screen.getByTestId('todo-item')).not.toHaveClass('editing')
5454
})
5555

5656
test('should work todo completed checkbox', () => {
57-
renderWithRecoilRoot(<App />, initialRecoilState)
57+
TestRenderer(<App />, initialRecoilState)
5858

5959
// click complete checkbox then should appear completed class
6060
fireEvent.click(screen.getByTestId('todo-item-complete-check'))
@@ -72,7 +72,7 @@ test('should work todo completed checkbox', () => {
7272
})
7373

7474
test('should work edit mode and toggle show/hide', () => {
75-
renderWithRecoilRoot(<App />, initialRecoilState)
75+
TestRenderer(<App />, initialRecoilState)
7676

7777
// by default, edit input form is not visible
7878
expect(screen.getByTestId('todo-edit-input')).not.toBeVisible()
@@ -109,7 +109,7 @@ test('should work edit mode and toggle show/hide', () => {
109109
})
110110

111111
test('delete todo item', () => {
112-
renderWithRecoilRoot(<App />, initialRecoilState)
112+
TestRenderer(<App />, initialRecoilState)
113113

114114
// click delete button, then todo item is removed
115115
expect(screen.getByTestId('todo-item')).toBeInTheDocument()

src/App/TodoList/index.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fireEvent } from '@testing-library/react'
22
import React from 'react'
33

44
import type { AppState } from '../../dataStructure'
5-
import { renderWithRecoilRoot } from '../../testUtil'
5+
import { TestRenderer } from '../../testUtil'
66

77
import TodoList from './index'
88

@@ -27,7 +27,7 @@ const initialRecoilState: AppState = {
2727
}
2828

2929
test('should be render 3 todo items in initialAppState', () => {
30-
const screen = renderWithRecoilRoot(<TodoList path="/" />, initialRecoilState)
30+
const screen = TestRenderer(<TodoList />, initialRecoilState)
3131

3232
expect(screen.getByTestId('todo-list')).toBeInTheDocument()
3333
expect(screen.getByTestId('todo-list').children.length).toBe(3)
@@ -38,7 +38,7 @@ test('should be render 3 todo items in initialAppState', () => {
3838
})
3939

4040
test('should be work delete todo button', () => {
41-
const screen = renderWithRecoilRoot(<TodoList path="/" />, initialRecoilState)
41+
const screen = TestRenderer(<TodoList />, initialRecoilState)
4242

4343
// delete first item
4444
fireEvent.click(screen.getAllByTestId('delete-todo-btn')[0])
@@ -50,7 +50,7 @@ test('should be work delete todo button', () => {
5050
})
5151

5252
test('should be work correctly all completed:true|false checkbox toggle button', () => {
53-
const screen = renderWithRecoilRoot(<TodoList path="/" />, initialRecoilState)
53+
const screen = TestRenderer(<TodoList />, initialRecoilState)
5454

5555
// toggle on
5656
fireEvent.click(screen.getByTestId('toggle-all-btn'))

src/App/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const App: React.FC = () => (
1212
<RecoilRoot>
1313
<Routes>
1414
<Route path="/" element={<TodoMVC />} />
15+
<Route path="/active" element={<TodoMVC />} />
16+
<Route path="/completed" element={<TodoMVC />} />
1517
<Route path="*" element={<NotFound />} />
1618
</Routes>
1719
</RecoilRoot>

src/NotFound.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { RouteComponentProps } from '@reach/router'
21
import React from 'react'
32

43
const css: React.CSSProperties = {
@@ -9,7 +8,7 @@ const css: React.CSSProperties = {
98
width: '100%',
109
}
1110

12-
export const NotFound: React.FC<RouteComponentProps> = () => (
11+
export const NotFound: React.FC = () => (
1312
<div data-cy="not-found-page" style={css}>
1413
<h1>Page Not Found</h1>
1514
</div>

src/testUtil.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RenderResult } from '@testing-library/react'
22
import { render } from '@testing-library/react'
33
import React from 'react'
4+
import { BrowserRouter } from 'react-router-dom'
45
import type { MutableSnapshot } from 'recoil'
56
import { RecoilRoot } from 'recoil'
67

@@ -11,16 +12,18 @@ const defaultValue: AppState = {
1112
todoList: [],
1213
}
1314

14-
export const renderWithRecoilRoot = (
15+
export const TestRenderer = (
1516
ui: React.ReactElement,
1617
initialRecoilStateValue: AppState = defaultValue
1718
): RenderResult =>
1819
render(
19-
<RecoilRoot
20-
initializeState={({ set }: MutableSnapshot): void =>
21-
set(recoilState, initialRecoilStateValue)
22-
}
23-
>
24-
{ui}
25-
</RecoilRoot>
20+
<BrowserRouter>
21+
<RecoilRoot
22+
initializeState={({ set }: MutableSnapshot): void =>
23+
set(recoilState, initialRecoilStateValue)
24+
}
25+
>
26+
{ui}
27+
</RecoilRoot>
28+
</BrowserRouter>
2629
)

0 commit comments

Comments
 (0)