Skip to content

Commit 13905d1

Browse files
committed
fix: test page without async
1 parent 153c3af commit 13905d1

File tree

3 files changed

+73
-66
lines changed

3 files changed

+73
-66
lines changed

src/__tests__/pages/HomePage.test.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
// !STARTERCONF You should delete this page
2-
31
import { render, screen } from '@testing-library/react';
42

5-
import HomePage from '@/app/page';
3+
import Homepage from '@/component/Homepage';
4+
5+
export function mockFetch(data: unknown) {
6+
return jest.fn().mockImplementation(() =>
7+
Promise.resolve({
8+
ok: true,
9+
json: () => data,
10+
})
11+
);
12+
}
613

714
describe('Homepage', () => {
815
it('renders the Components', () => {
9-
render(<HomePage />);
16+
window.fetch = mockFetch({});
17+
render(<Homepage />);
1018

11-
const heading = screen.getByText(/A starter for Next.js/i);
19+
const heading = screen.getByText(/HiHB/i);
1220

1321
expect(heading).toBeInTheDocument();
1422
});

src/app/page.tsx

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
import PinDropIcon from '@mui/icons-material/PinDrop';
2-
import { Box, Typography } from '@mui/material';
3-
import Link from 'next/link';
4-
import * as React from 'react';
5-
6-
import PageFooter from '@/component/PageFooter';
7-
import { SITE_CONFIG } from '@/constant';
1+
import Homepage from '@/component/Homepage';
82
import { getApiResponse } from '@/util/shared/get-api-response';
93

10-
// !STARTERCONF -> Select !STARTERCONF and CMD + SHIFT + F
11-
// Before you begin editing, follow all comments with `STARTERCONF`,
12-
// to customize the default configuration.
13-
144
const loadDataFromApi = async (slug?: string) => {
155
if (slug === 'testError500') {
166
throw new Error('This is a ssr 500 test error');
@@ -21,59 +11,12 @@ const loadDataFromApi = async (slug?: string) => {
2111
});
2212
};
2313

24-
interface HomePageProps {
14+
interface AppHomeProps {
2515
searchParams: { [key: string]: string | undefined };
2616
}
2717

28-
export default async function HomePage({ searchParams }: HomePageProps) {
18+
export default async function AppHome({ searchParams }: AppHomeProps) {
2919
const apiResult = await loadDataFromApi(searchParams['slug']);
3020

31-
return (
32-
<main>
33-
<section>
34-
<Box sx={{ textAlign: 'center' }}>
35-
<PinDropIcon />
36-
<Typography variant='h5' component='h1' gutterBottom>
37-
{SITE_CONFIG.title}
38-
</Typography>
39-
<Typography variant='subtitle2' gutterBottom>
40-
{SITE_CONFIG.description}
41-
</Typography>
42-
43-
<Typography
44-
variant='subtitle1'
45-
gutterBottom
46-
sx={{ color: 'green', mt: 3 }}
47-
>
48-
Get data from api test: The latest React version is{' '}
49-
{apiResult?.version}
50-
</Typography>
51-
52-
<Box sx={{ m: 5 }}>
53-
<Link
54-
href='https://github.com/AlexStack/nextjs-materia-mui-typescript-hook-form-scaffold-boilerplate-starter'
55-
target='_blank'
56-
>
57-
See the Github repository page
58-
</Link>
59-
</Box>
60-
<Box sx={{ m: 5 }}>
61-
<Link
62-
href='https://vercel.com/new/clone?s=https%3A%2F%2Fgithub.com%2FAlexStack%2Fnextjs-materia-mui-typescript-hook-form-scaffold-boilerplate-starter&showOptionalTeamCreation=false'
63-
target='_blank'
64-
>
65-
Click here to deploy a demo site to your Vercel in 1 minute
66-
</Link>
67-
</Box>
68-
<Box sx={{ m: 5 }}>
69-
<Link href='/test-page-not-exists'>Test 404 page not found</Link>
70-
</Box>
71-
<Box sx={{ m: 5 }}>
72-
<Link href='/?slug=testError500'>Test 500 error page</Link>
73-
</Box>
74-
</Box>
75-
</section>
76-
<PageFooter />
77-
</main>
78-
);
21+
return <Homepage reactVersion={apiResult?.version} />;
7922
}

src/component/Homepage.tsx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import PinDropIcon from '@mui/icons-material/PinDrop';
2+
import { Box, Typography } from '@mui/material';
3+
import Link from 'next/link';
4+
5+
import PageFooter from '@/component/PageFooter';
6+
import { SITE_CONFIG } from '@/constant';
7+
8+
export default function Homepage({ reactVersion = 'unknown' }) {
9+
return (
10+
<main>
11+
<section>
12+
<Box sx={{ textAlign: 'center' }}>
13+
<PinDropIcon />
14+
<Typography variant='h5' component='h1' gutterBottom>
15+
{SITE_CONFIG.title}
16+
</Typography>
17+
<Typography variant='subtitle2' gutterBottom>
18+
{SITE_CONFIG.description}
19+
</Typography>
20+
21+
<Typography
22+
variant='subtitle1'
23+
gutterBottom
24+
sx={{ color: 'green', mt: 3 }}
25+
>
26+
Get data from api test: The latest React version is {reactVersion}
27+
</Typography>
28+
29+
<Box sx={{ m: 5 }}>
30+
<Link
31+
href='https://github.com/AlexStack/nextjs-materia-mui-typescript-hook-form-scaffold-boilerplate-starter'
32+
target='_blank'
33+
>
34+
See the Github repository page
35+
</Link>
36+
</Box>
37+
<Box sx={{ m: 5 }}>
38+
<Link
39+
href='https://vercel.com/new/clone?s=https%3A%2F%2Fgithub.com%2FAlexStack%2Fnextjs-materia-mui-typescript-hook-form-scaffold-boilerplate-starter&showOptionalTeamCreation=false'
40+
target='_blank'
41+
>
42+
Click here to deploy a demo site to your Vercel in 1 minute
43+
</Link>
44+
</Box>
45+
<Box sx={{ m: 5 }}>
46+
<Link href='/test-page-not-exists'>Test 404 page not found</Link>
47+
</Box>
48+
<Box sx={{ m: 5 }}>
49+
<Link href='/?slug=testError500'>Test 500 error page</Link>
50+
</Box>
51+
</Box>
52+
</section>
53+
<PageFooter />
54+
</main>
55+
);
56+
}

0 commit comments

Comments
 (0)