Skip to content

Commit 35bc056

Browse files
authored
feat: 🎸 add route change example (#11)
* docs: ✏️ add new generator info readme.md * feat: 🎸 add route change example * test: 💍 test for redirect button * docs: ✏️ remove whatwg @types
1 parent d69c0dd commit 35bc056

File tree

8 files changed

+76
-87
lines changed

8 files changed

+76
-87
lines changed

package-lock.json

Lines changed: 0 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"@types/node": "20.4.5",
3939
"@types/react": "18.2.18",
4040
"@types/react-dom": "18.2.7",
41-
"@types/whatwg-fetch": "^0.0.33",
4241
"@typescript-eslint/eslint-plugin": "^6.2.1",
4342
"@typescript-eslint/parser": "^6.2.1",
4443
"babel-loader": "^9.1.3",

src/app/(redux)/redux-example/page.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import userEvent from '@testing-library/user-event'
22

33
import Page from '@/app/(redux)/redux-example/page'
4+
import useRouterMock, { pushMock } from '@/mocks/useRouter.mock'
45
import { resetStore } from '@/redux/store/store'
56
import { render, screen, waitFor } from '@/utils/test-utils'
67

8+
jest.mock('next/navigation', () => ({
9+
useRouter() {
10+
return useRouterMock
11+
},
12+
}))
13+
714
describe('@/app/(redux)/redux-example/page', () => {
815
beforeEach(() => {
916
resetStore()
@@ -55,4 +62,16 @@ describe('@/app/(redux)/redux-example/page', () => {
5562
expect(screen.getByText('0')).toBeInTheDocument()
5663
})
5764
})
65+
66+
it('should redirect when clicked', async () => {
67+
render(<Page />)
68+
expect(screen.getByText(0)).toBeInTheDocument()
69+
70+
const decrement = screen.getByText('redirect')
71+
await userEvent.click(decrement)
72+
73+
await waitFor(() => {
74+
expect(pushMock).toHaveBeenCalledWith('/redux-persist-example')
75+
})
76+
})
5877
})

src/app/(redux)/redux-example/page.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22

33
import { ReactElement } from 'react'
4+
import { useRouter } from 'next/navigation'
45

56
import Lazy from '@/components/Lazy/Lazy.lazy'
67
import {
@@ -14,16 +15,27 @@ import useAppSelector from '@/redux/selectors/useAppSelector/useAppSelector'
1415
export default function Page(): ReactElement {
1516
const count = useAppSelector((state) => state.counterReducer.value)
1617
const dispatch = useAppDispatch()
18+
const { push } = useRouter()
1719

1820
return (
1921
<main style={{ maxWidth: 1200, marginInline: 'auto', padding: 20 }}>
2022
<div style={{ marginBottom: '4rem', textAlign: 'center' }}>
23+
<h1>STATE REDUX</h1>
2124
<h4 style={{ marginBottom: 16 }}>{count}</h4>
2225
<button onClick={() => dispatch(increment())}>increment</button>
2326
<button onClick={() => dispatch(decrement())} style={{ marginInline: 16 }}>
2427
decrement
2528
</button>
26-
<button onClick={() => dispatch(reset())}>reset</button>
29+
<button onClick={() => dispatch(reset())} style={{ marginInline: 16 }}>
30+
reset
31+
</button>
32+
<button
33+
onClick={() => {
34+
push('/redux-persist-example')
35+
}}
36+
>
37+
redirect
38+
</button>
2739
{parseInt(count) === 1 && <Lazy />}
2840
</div>
2941
</main>

src/app/(redux)/redux-persist-example/page.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import userEvent from '@testing-library/user-event'
22

33
import Page from '@/app/(redux)/redux-persist-example/page'
4+
import useRouterMock, { pushMock } from '@/mocks/useRouter.mock'
45
import { resetStore } from '@/redux/store/store'
56
import { render, screen, waitFor } from '@/utils/test-utils'
67

8+
jest.mock('next/navigation', () => ({
9+
useRouter() {
10+
return useRouterMock
11+
},
12+
}))
13+
714
describe('@/app/(redux)/redux-persist-example/page', () => {
815
beforeEach(() => {
916
resetStore()
@@ -54,4 +61,16 @@ describe('@/app/(redux)/redux-persist-example/page', () => {
5461
expect(screen.getByText('0')).toBeInTheDocument()
5562
})
5663
})
64+
65+
it('should redirect when clicked', async () => {
66+
render(<Page />)
67+
expect(screen.getByText(0)).toBeInTheDocument()
68+
69+
const decrement = screen.getByText('redirect')
70+
await userEvent.click(decrement)
71+
72+
await waitFor(() => {
73+
expect(pushMock).toHaveBeenCalledWith('/redux-example')
74+
})
75+
})
5776
})

src/app/(redux)/redux-persist-example/page.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client'
22

33
import { ReactElement } from 'react'
4+
import { useRouter } from 'next/navigation'
45

56
import {
67
decrement,
@@ -13,6 +14,7 @@ import useAppSelector from '@/redux/selectors/useAppSelector/useAppSelector'
1314
export default function Home(): ReactElement {
1415
const count = useAppSelector((state) => state.persistSaveReducerPersisted.value)
1516
const dispatch = useAppDispatch()
17+
const { push } = useRouter()
1618

1719
return (
1820
<main style={{ maxWidth: 1200, marginInline: 'auto', padding: 20 }}>
@@ -23,7 +25,16 @@ export default function Home(): ReactElement {
2325
<button onClick={() => dispatch(decrement())} style={{ marginInline: 16 }}>
2426
decrement
2527
</button>
26-
<button onClick={() => dispatch(reset())}>reset</button>
28+
<button onClick={() => dispatch(reset())} style={{ marginInline: 16 }}>
29+
reset
30+
</button>
31+
<button
32+
onClick={() => {
33+
push('/redux-example')
34+
}}
35+
>
36+
redirect
37+
</button>
2738
</div>
2839
</main>
2940
)

src/app/layout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function RootLayout({
2222
}): ReactElement {
2323
return (
2424
<html lang="en">
25+
<head></head>
2526
<body className={inter.className}>
2627
<ReduxProvider>
2728
<ThemeProvider>

src/mocks/useRouter.mock.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const pushMock = jest.fn()
2+
3+
const useRouterMock = {
4+
route: '/',
5+
pathname: '',
6+
query: '',
7+
asPath: '',
8+
push: pushMock,
9+
}
10+
11+
12+
export default useRouterMock

0 commit comments

Comments
 (0)