-
Notifications
You must be signed in to change notification settings - Fork 0
React: Routing and Hooks #3
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
base: unit-testing
Are you sure you want to change the base?
Changes from all commits
66a25a5
d69996b
653d5e1
e034448
a8f1ca7
2e7edde
326ace8
2216928
3e35c98
2311b71
fbb58f0
ccbca58
3e5dc94
cfbad37
e603859
2b46ca4
4f61b94
a4d74f8
999acb9
b3e20f9
9e59271
a36826d
06843d0
48ddf6b
ba412fe
bf1e2bc
9f0d3d0
9da071e
d8dc56d
97b62c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,21 @@ | ||
| import { render, screen, fireEvent } from '@testing-library/react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { RouterProvider, createMemoryRouter } from 'react-router'; | ||
| import { describe, it, expect } from 'vitest'; | ||
| import App from './App'; | ||
| import { ErrorBoundary } from '@/components/ErrorBoundary'; | ||
| import { routes } from '@/app/routes'; | ||
| import { UI_STRINGS } from '@/shared/constants/ui-strings'; | ||
|
|
||
| describe('App component', () => { | ||
| it('renders components', () => { | ||
| render(<App />); | ||
| expect(screen.getByText(UI_STRINGS.title)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('throw the error with the button', () => { | ||
| render( | ||
| <ErrorBoundary fallback={<div>App down</div>}> | ||
| <App /> | ||
| </ErrorBoundary>, | ||
| ); | ||
| it('renders ErrorLayout with Header and NotFoundPage', async () => { | ||
| const testRouter = createMemoryRouter(routes, { | ||
| initialEntries: ['/error'], | ||
| }); | ||
|
|
||
| const errorButton = screen.getByText(UI_STRINGS.errorButton); | ||
| render(<RouterProvider router={testRouter} />); | ||
|
|
||
| fireEvent.click(errorButton); | ||
| expect(await screen.findByAltText(UI_STRINGS.altLogo)).toBeInTheDocument(); | ||
|
|
||
| expect(screen.getByText(/App down/i)).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByRole('heading', { name: UI_STRINGS.title }), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,23 @@ | ||
| import { Component, type ReactNode } from 'react'; | ||
| import Footer from '@/components/Footer'; | ||
| import Header from '@/components/Header'; | ||
| import { HomePage } from '@/pages/HomePage'; | ||
| import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { createBrowserRouter, RouterProvider } from 'react-router'; | ||
| import { routes } from '@/app/routes'; | ||
|
|
||
| type AppState = { | ||
| wouldThrow: boolean; | ||
| }; | ||
| const router = createBrowserRouter(routes); | ||
|
|
||
| class App extends Component { | ||
| state: AppState = { | ||
| wouldThrow: false, | ||
| }; | ||
| const queryClient = new QueryClient({ | ||
| defaultOptions: { | ||
| queries: { | ||
| staleTime: 2 * 60 * 1000, | ||
| refetchOnWindowFocus: false, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| render(): ReactNode { | ||
| const { wouldThrow } = this.state; | ||
|
|
||
| if (wouldThrow) { | ||
| throw new Error('test error from button'); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="flex min-h-screen bg-white"> | ||
| <div className="w-full max-w-7xl flex flex-col bg-gray-100 mx-auto"> | ||
| <Header /> | ||
| <HomePage /> | ||
| <Footer onThrowError={() => this.setState({ wouldThrow: true })} /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
| export default function App() { | ||
| return ( | ||
| <QueryClientProvider client={queryClient}> | ||
| <RouterProvider router={router} /> | ||
| ); | ||
| </QueryClientProvider> | ||
| ); | ||
| } | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export const PATHS = { | ||
| HOME: '/', | ||
| ABOUT: '/about', | ||
| CHARACTER: '/character', | ||
| NOT_FOUND: '*', | ||
| } as const; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { Navigate } from 'react-router'; | ||
| import { PATHS } from '@/app/paths'; | ||
| import { FallBack } from '@/components/FallBack'; | ||
| import { ErrorLayout } from '@/layouts/ErrorLayout'; | ||
| import { MainLayout } from '@/layouts/MainLayout'; | ||
| import { AboutPage } from '@/pages/AboutPage'; | ||
| import { HomePage } from '@/pages/HomePage'; | ||
| import { NotFoundPage } from '@/pages/NotFoundPage'; | ||
|
|
||
| export const routes = [ | ||
| { | ||
| path: '/', | ||
| element: <Navigate to={PATHS.CHARACTER} replace />, | ||
| }, | ||
| { | ||
| path: '/character', | ||
| element: <MainLayout />, | ||
| errorElement: <FallBack />, | ||
| children: [ | ||
| { | ||
| index: true, | ||
| element: <HomePage />, | ||
| }, | ||
| { | ||
| path: ':characterId', | ||
| element: <HomePage />, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| path: PATHS.ABOUT, | ||
| element: <MainLayout />, | ||
| errorElement: <FallBack />, | ||
| children: [{ index: true, element: <AboutPage /> }], | ||
| }, | ||
| { | ||
| path: PATHS.NOT_FOUND, | ||
| element: <ErrorLayout />, | ||
| errorElement: <FallBack />, | ||
| children: [{ path: PATHS.NOT_FOUND, element: <NotFoundPage /> }], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. зачем здесь дублирование пути? |
||
| }, | ||
| ]; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,35 @@ | ||
| import { Component, type ButtonHTMLAttributes } from 'react'; | ||
| import { type ButtonHTMLAttributes, type ReactNode } from 'react'; | ||
|
|
||
| interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> { | ||
| type ButtonProps = { | ||
| variant?: 'default' | 'secondary'; | ||
| size?: 'sm' | 'default'; | ||
| className?: string; | ||
| } | ||
| children?: ReactNode; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут children обязателен |
||
| } & ButtonHTMLAttributes<HTMLButtonElement>; | ||
|
|
||
| export class Button extends Component<ButtonProps> { | ||
| render() { | ||
| const { | ||
| variant = 'default', | ||
| size = 'default', | ||
| className = '', | ||
| ...rest | ||
| } = this.props; | ||
| export const Button = ({ | ||
| variant = 'default', | ||
| size = 'default', | ||
| className = '', | ||
| children, | ||
| ...rest | ||
| }: ButtonProps) => { | ||
| let baseClasses = | ||
| 'inline-flex items-center justify-center text-sm transition-colors focus:outline-none focus:ring focus:ring-gray-300 disabled:opacity-50 disabled:pointer-events-none'; | ||
|
|
||
| let baseClasses = | ||
| 'inline-flex items-center justify-center text-sm transition-colors focus:outline-none focus:ring focus:ring-gray-300 disabled:opacity-50 disabled:pointer-events-none'; | ||
| let variantClasses = | ||
| variant === 'secondary' | ||
| ? 'bg-gray-300 text-gray-800 hover:bg-gray-400' | ||
| : 'bg-gray-200 hover:bg-gray-400'; | ||
|
|
||
| let variantClasses = | ||
| variant === 'secondary' | ||
| ? 'bg-gray-300 text-gray-800 hover:bg-gray-400' | ||
| : 'bg-gray-200 hover:bg-gray-400'; | ||
| let sizeClasses = size === 'sm' ? 'h-8 w-8 px-4' : 'h-9 px-4 min-sm:px-6'; | ||
|
Comment on lines
+17
to
+25
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. надо использовать classnames или clsx |
||
|
|
||
| let sizeClasses = size === 'sm' ? 'h-8 w-8 px-4' : 'h-9 px-4 min-sm:px-6'; | ||
|
|
||
| return ( | ||
| <button | ||
| className={`${baseClasses} ${variantClasses} ${sizeClasses} ${className}`} | ||
| {...rest} | ||
| > | ||
| {this.props.children} | ||
| </button> | ||
| ); | ||
| } | ||
| } | ||
| return ( | ||
| <button | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. надо указывать type кнопки или прокидывать через пропы. по умолчанию type="submit", может привести к неожиданному поведению |
||
| className={`${baseClasses} ${variantClasses} ${sizeClasses} ${className}`} | ||
| {...rest} | ||
| > | ||
| {children} | ||
| </button> | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
почему не используются пути из paths.ts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
пропустил, когда рефакторил