Skip to content
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

Прикрой меня тестом #16

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/react": "18.2.25",
"@types/react-dom": "18.2.11",
"@types/react-redux": "7.1.27",
"@types/redux-mock-store": "^1.5.0",
"@types/testing-library__jest-dom": "5.14.9",
"@typescript-eslint/eslint-plugin": "6.7.4",
"@typescript-eslint/parser": "6.7.4",
Expand All @@ -44,6 +45,7 @@
"eslint-plugin-react-refresh": "0.4.3",
"faker": "5.5.3",
"jsdom": "22.1.0",
"redux-mock-store": "^1.5.5",
"typescript": "5.2.2",
"vite": "^4.5.5",
"vitest": "0.34.6"
Expand Down
133 changes: 133 additions & 0 deletions src/components/app/app.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { render, screen } from '@testing-library/react';
import { App } from './app';
import { AppRoute, AuthorizationStatus } from '../../const';
import { createMemoryHistory } from 'history';
import {withHistory, withStore} from '../../utils/mock-component.tsx';
import {createRandomDetailedOffer} from '../../utils/create-random-detailed-offer.ts';

const makeFakeStore = () => ({
CITY: {
activeCity: 'Paris',
},
COMMENTS: {
comments: [],
},
OFFER: {
offer: createRandomDetailedOffer(),
nearbyOffers: [],
},
OFFERS: {
offers: [],
isDataLoading: false,
},
USER: {
authorizationStatus: AuthorizationStatus.NoAuth,
userInfo: null,
favoriteOffers: [],
},
});

describe('Application Routing', () => {
let mockHistory = createMemoryHistory();

beforeEach(() => {
mockHistory = createMemoryHistory();
});

it('should render "MainPage" when user navigates to "/"', () => {
const withHistoryComponent = withHistory(<App />, mockHistory);
const { withStoreComponent } = withStore(withHistoryComponent, makeFakeStore());
mockHistory.push(AppRoute.Main);

render(withStoreComponent);

expect(screen.getByText(/No places to stay available/i)).toBeInTheDocument();
});

it('should render "LoginPage" when user navigates to "/login"', () => {
const withHistoryComponent = withHistory(<App />, mockHistory);
const { withStoreComponent } = withStore(withHistoryComponent, makeFakeStore());
mockHistory.push(AppRoute.Login);

render(withStoreComponent);

expect(screen.getByRole('button', { name: /Sign in/i })).toBeInTheDocument();
});

it('should render "FavoritesPage" when user navigates to "/favorites" and is authorized', () => {
const withHistoryComponent = withHistory(<App />, mockHistory);
const { withStoreComponent } = withStore(withHistoryComponent, {
...makeFakeStore(),
USER: {
...makeFakeStore().USER,
authorizationStatus: AuthorizationStatus.Auth,
},
});
mockHistory.push(AppRoute.Favorites);

render(withStoreComponent);

expect(screen.getByText(/Nothing yet saved./i)).toBeInTheDocument();
});

it('should render "OfferPage" when user navigates to "/offer/:id"', () => {
const withHistoryComponent = withHistory(<App />, mockHistory);
const { withStoreComponent } = withStore(withHistoryComponent, makeFakeStore());
mockHistory.push(`${AppRoute.Offer}/1`);

render(withStoreComponent);

expect(screen.getByText(/Other places in the neighbourhood/i)).toBeInTheDocument();
});

it('should render "NotFoundPage" when user navigates to non-existent route', () => {
const withHistoryComponent = withHistory(<App />, mockHistory);
const { withStoreComponent } = withStore(withHistoryComponent, makeFakeStore());
mockHistory.push('/non-existent-route');

render(withStoreComponent);

expect(screen.getByText(/404 Page Not Found/i)).toBeInTheDocument();
});

it('should render "Spinner" when offers data is loading', () => {
const withHistoryComponent = withHistory(<App />, mockHistory);
const { withStoreComponent } = withStore(withHistoryComponent, {
...makeFakeStore(),
OFFERS: {
...makeFakeStore().OFFERS,
isDataLoading: true,
},
});

render(withStoreComponent);

expect(screen.getByTestId('spinner-container')).toBeInTheDocument();
});

it('should redirect to "/login" when unauthorized user navigates to "/favorites"', () => {
const withHistoryComponent = withHistory(<App />, mockHistory);
const { withStoreComponent } = withStore(withHistoryComponent, makeFakeStore());
mockHistory.push(AppRoute.Favorites);

render(withStoreComponent);

expect(screen.getByRole('button', { name: /Sign in/i })).toBeInTheDocument();
});

it('should redirect to "/" when authorized user navigates to "/login"', () => {
const withHistoryComponent = withHistory(<App />, mockHistory);
const { withStoreComponent } = withStore(withHistoryComponent, {
...makeFakeStore(),
USER: {
...makeFakeStore().USER,
authorizationStatus: AuthorizationStatus.Auth,
},
});
mockHistory.push(AppRoute.Login);

render(withStoreComponent);

expect(screen.getByText(/No places to stay available/i)).toBeInTheDocument();
});
});
64 changes: 30 additions & 34 deletions src/components/app/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {NotFoundPage} from '../../pages/not-found-page/not-found-page.tsx';
import {FavoritesPage} from '../../pages/favorites-page/favorites-page.tsx';
import {useAppSelector} from '../../hooks';
import {Spinner} from '../spinner/spinner.tsx';
import {HistoryRouter} from '../history-route/history-route.tsx';
import {browserHistory} from '../../browser-history.ts';
import {PrivateLoginRoute} from '../private-login-route/private-login-route.tsx';
import {getDataLoadingStatus} from '../../store/offers-data/selectors.ts';

Expand All @@ -25,38 +23,36 @@ export function App() {

return (
<HelmetProvider>
<HistoryRouter history={browserHistory}>
<Routes>
<Route
path={AppRoute.Main}
element={<MainPage/>}
/>
<Route
path={AppRoute.Login}
element={
<PrivateLoginRoute>
<LoginPage/>
</PrivateLoginRoute>
}
/>
<Route
path={AppRoute.Favorites}
element={
<PrivateRoute>
<FavoritesPage/>
</PrivateRoute>
}
/>
<Route
path={AppRoute.OfferWithId}
element={<OfferPage/>}
/>
<Route
path="*"
element={<NotFoundPage/>}
/>
</Routes>
</HistoryRouter>
<Routes>
<Route
path={AppRoute.Main}
element={<MainPage/>}
/>
<Route
path={AppRoute.Login}
element={
<PrivateLoginRoute>
<LoginPage/>
</PrivateLoginRoute>
}
/>
<Route
path={AppRoute.Favorites}
element={
<PrivateRoute>
<FavoritesPage/>
</PrivateRoute>
}
/>
<Route
path={AppRoute.OfferWithId}
element={<OfferPage/>}
/>
<Route
path="*"
element={<NotFoundPage/>}
/>
</Routes>
</HelmetProvider>
);
}
Loading
Loading