-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6c56d0
commit 13c1477
Showing
9 changed files
with
239 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { createMemoryHistory, MemoryHistory } from 'history'; | ||
import { Provider } from 'react-redux'; | ||
|
||
import { cities } from '@/entities/city'; | ||
import { makeOfferMaximum, makeOffers } from '@/entities/offer-card/lib/mocks'; | ||
import { makeReviews } from '@/entities/review/lib/mocks'; | ||
import { makeUser } from '@/entities/user/lib/mocks'; | ||
import { AppRoutes } from '@/shared/model/app-routes'; | ||
import { | ||
AuthorizationStatus, | ||
FetchStatus, | ||
NameSpace, | ||
} from '@/shared/model/enums'; | ||
|
||
import { App } from './app'; | ||
import { HistoryRouter } from './history-router'; | ||
import { initAsyncActionsStore } from './lib/mocks'; | ||
|
||
describe('<App />', () => { | ||
const { mockStoreCreator } = initAsyncActionsStore(); | ||
let store: ReturnType<typeof mockStoreCreator>; | ||
|
||
let mockHistory: MemoryHistory; | ||
|
||
beforeEach(() => { | ||
store = mockStoreCreator({ | ||
[NameSpace.USER]: { | ||
authorizationStatus: AuthorizationStatus.AUTH, | ||
user: makeUser(), | ||
}, | ||
[NameSpace.CITY]: { | ||
city: cities.Paris, | ||
}, | ||
[NameSpace.OFFER]: { | ||
offers: makeOffers(), | ||
offersFetchStatus: FetchStatus.SUCCESS, | ||
offer: makeOfferMaximum(), | ||
offerFetchStatus: FetchStatus.SUCCESS, | ||
}, | ||
[NameSpace.REVIEW]: { | ||
reviews: makeReviews(), | ||
reviewsFetchStatus: FetchStatus.SUCCESS, | ||
}, | ||
}); | ||
|
||
mockHistory = createMemoryHistory(); | ||
}); | ||
|
||
it('should render MainPage correctly', () => { | ||
const component = ( | ||
<Provider store={store}> | ||
<HistoryRouter history={mockHistory}> | ||
<App /> | ||
</HistoryRouter> | ||
</Provider> | ||
); | ||
|
||
mockHistory.push(AppRoutes.HOME); | ||
|
||
const { container } = render(component); | ||
|
||
expect(container.querySelector('.header')).toBeInTheDocument(); | ||
expect(container.querySelector('.locations__list')).toBeInTheDocument(); | ||
expect(container.querySelector('.cities')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render LoginPage correctly', () => { | ||
store = mockStoreCreator({ | ||
[NameSpace.USER]: { | ||
authorizationStatus: AuthorizationStatus.NO_AUTH, | ||
}, | ||
}); | ||
|
||
const component = ( | ||
<Provider store={store}> | ||
<HistoryRouter history={mockHistory}> | ||
<App /> | ||
</HistoryRouter> | ||
</Provider> | ||
); | ||
|
||
mockHistory.push(AppRoutes.LOGIN); | ||
|
||
const { container } = render(component); | ||
|
||
expect(container.querySelector('form.login__form')).toBeInTheDocument(); | ||
expect(container.querySelectorAll('input').length).toBe(2); | ||
}); | ||
|
||
it('should render LoginPage instead of FavoritesPage due to user is not authenticated', () => { | ||
store = mockStoreCreator({ | ||
[NameSpace.USER]: { | ||
authorizationStatus: AuthorizationStatus.NO_AUTH, | ||
}, | ||
}); | ||
|
||
const component = ( | ||
<Provider store={store}> | ||
<HistoryRouter history={mockHistory}> | ||
<App /> | ||
</HistoryRouter> | ||
</Provider> | ||
); | ||
|
||
mockHistory.push(AppRoutes.FAVORITES); | ||
|
||
const { container } = render(component); | ||
|
||
expect(container.querySelector('form.login__form')).toBeInTheDocument(); | ||
expect(container.querySelectorAll('input').length).toBe(2); | ||
}); | ||
|
||
it('should render FavoritesPage correctly', () => { | ||
const component = ( | ||
<Provider store={store}> | ||
<HistoryRouter history={mockHistory}> | ||
<App /> | ||
</HistoryRouter> | ||
</Provider> | ||
); | ||
|
||
mockHistory.push(AppRoutes.FAVORITES); | ||
|
||
const { container } = render(component); | ||
|
||
expect(screen.getByText('Saved listing')).toBeInTheDocument(); | ||
expect(container.querySelector('.favorites__list')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render OfferPage correctly', () => { | ||
const component = ( | ||
<Provider store={store}> | ||
<HistoryRouter history={mockHistory}> | ||
<App /> | ||
</HistoryRouter> | ||
</Provider> | ||
); | ||
|
||
mockHistory.push(`${AppRoutes.OFFER}/1`); | ||
|
||
const { container } = render(component); | ||
|
||
expect(container.querySelector('.page__main--offer')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render Error404Page if offer was not provided', () => { | ||
const component = ( | ||
<Provider store={store}> | ||
<HistoryRouter history={mockHistory}> | ||
<App /> | ||
</HistoryRouter> | ||
</Provider> | ||
); | ||
|
||
mockHistory.push(AppRoutes.OFFER); | ||
|
||
render(component); | ||
|
||
expect(screen.getByText('404 — Page not found')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render Error404Page route does not exist', () => { | ||
const component = ( | ||
<Provider store={store}> | ||
<HistoryRouter history={mockHistory}> | ||
<App /> | ||
</HistoryRouter> | ||
</Provider> | ||
); | ||
|
||
mockHistory.push('/123'); | ||
|
||
render(component); | ||
|
||
expect(screen.getByText('404 — Page not found')).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { BrowserHistory } from 'history'; | ||
import { useLayoutEffect, useState } from 'react'; | ||
import { Router } from 'react-router-dom'; | ||
|
||
type HistoryRouterProps = { | ||
history: BrowserHistory; | ||
basename?: string; | ||
children?: React.ReactNode; | ||
}; | ||
|
||
export const HistoryRouter = (props: HistoryRouterProps) => { | ||
const { basename, children, history } = props; | ||
|
||
const [state, setState] = useState({ | ||
action: history.action, | ||
location: history.location, | ||
}); | ||
|
||
useLayoutEffect(() => history.listen(setState), [history]); | ||
|
||
return ( | ||
<Router | ||
basename={basename} | ||
location={state.location} | ||
navigationType={state.action} | ||
navigator={history} | ||
> | ||
{children} | ||
</Router> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { App } from './app'; |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import { Provider } from 'react-redux'; | ||
import { BrowserRouter } from 'react-router-dom'; | ||
|
||
import { App } from './app'; | ||
import { App } from './app/app'; | ||
import { store } from './app/store'; | ||
|
||
const root = ReactDOM.createRoot( | ||
document.getElementById('root') as HTMLElement, | ||
); | ||
|
||
root.render( | ||
<React.StrictMode> | ||
<App /> | ||
<Provider store={store}> | ||
<BrowserRouter> | ||
<App /> | ||
</BrowserRouter> | ||
</Provider> | ||
</React.StrictMode>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters