Skip to content

Commit

Permalink
test(frontend): fix failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Falinor committed Nov 26, 2024
1 parent af30c01 commit 784e1a6
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function AccountCampaignIntentCreationView() {
};

if (!location.state || !prospect || !password) {
return <Navigate to="email" />;
return <Navigate to="/inscription/email" />;
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ function AccountPasswordCreationView() {

if (prospect) {
if (prospect.hasAccount && !prospect.hasCommitment) {
return <Navigate to="en-attente" />;
return <Navigate to="/inscription/en-attente" />;
}
if (
!prospect.establishment ||
(!prospect.hasAccount && !prospect.hasCommitment)
) {
return <Navigate to="impossible" />;
return <Navigate to="/inscription/impossible" />;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import userEvent from '@testing-library/user-event';
import { genProspect, genSiren } from '../../../../../test/fixtures.test';
import { render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { MemoryRouter as Router, Route } from 'react-router-dom';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import * as randomstring from 'randomstring';
import AccountCampaignIntentCreationView from '../AccountCampaignIntentCreationView';
import { Prospect } from '../../../../models/Prospect';
Expand All @@ -14,27 +14,32 @@ describe('AccountCampaignIntentCreationView', () => {
const password = randomstring.generate();

function renderComponent(prospect: Prospect = genProspect()) {
const router = createMemoryRouter(
[
{
path: '/inscription/mot-de-passe',
element: 'Créer votre mot de passe'
},
{
path: '/inscription/campagne',
element: <AccountCampaignIntentCreationView />
}
],
{
initialEntries: [
{
pathname: '/inscription/campagne',
state: {
prospect,
password
}
}
]
}
);
render(
<Provider store={store}>
<Router
initialEntries={[
{
pathname: '/inscription/campagne',
state: {
prospect,
password
}
}
]}
>
<Route path="/inscription/mot-de-passe">
Créer votre mot de passe
</Route>
<Route
path="/inscription/campagne"
component={AccountCampaignIntentCreationView}
/>
</Router>
<RouterProvider router={router} />
</Provider>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { MemoryRouter as Router, Route } from 'react-router-dom';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import AccountEmailActivationView from '../AccountEmailActivationView';
import { Provider } from 'react-redux';
import { store } from '../../../../store/store';
Expand All @@ -11,16 +11,22 @@ describe('AccountEmailActivationView', () => {

describe('Without passing an email in route state', () => {
function setup() {
const router = createMemoryRouter(
[
{ path: '/inscription/email', element: 'Créer votre compte' },
{
path: '/inscription/activation',
element: <AccountEmailActivationView />
}
],
{
initialEntries: ['/inscription/activation']
}
);
render(
<Provider store={store}>
<Router initialEntries={['/inscription/activation']}>
<Route path="/inscription/email">Créer votre compte</Route>
<Route
path="/inscription/activation"
component={AccountEmailActivationView}
/>
</Router>
</Provider>,
<RouterProvider router={router} />
</Provider>
);
}

Expand All @@ -34,32 +40,34 @@ describe('AccountEmailActivationView', () => {

describe('With an email', () => {
function setup() {
const router = createMemoryRouter(
[
{
path: '/inscription/activation',
element: <AccountEmailActivationView />
}
],
{
initialEntries: [
{
pathname: '/inscription/activation',
state: { email: '[email protected]' }
}
]
}
);
render(
<Provider store={store}>
<Router
initialEntries={[
{
pathname: '/inscription/activation',
state: {
email: '[email protected]',
},
},
]}
>
<Route
path="/inscription/activation"
component={AccountEmailActivationView}
/>
</Router>
</Provider>,
<RouterProvider router={router} />
</Provider>
);
}

it('should render', async () => {
setup();

const title = screen.getByText(
'Vous devez confirmer votre adresse mail.',
'Vous devez confirmer votre adresse mail.'
);

expect(title).toBeVisible();
Expand All @@ -68,15 +76,15 @@ describe('AccountEmailActivationView', () => {
it('should send an email again', async () => {
const sendActivationEmail = jest.spyOn(
signupLinkApi.endpoints.sendActivationEmail,
'initiate',
'initiate'
);
setup();

const sendAgain = screen.getByText(/renvoyer le mail/i);
await user.click(sendAgain);

expect(sendActivationEmail).toHaveBeenCalledWith('[email protected]', {
fixedCacheKey: undefined,
fixedCacheKey: undefined
});
const sent = screen.getByText('Email envoyé.');
expect(sent).toBeVisible();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,51 @@
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import { MemoryRouter as Router, Route } from 'react-router-dom';
import {
genAuthUser,
genProspect,
genSignupLink,
} from '../../../../../test/fixtures.test';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import { genProspect, genSignupLink } from '../../../../../test/fixtures.test';
import AccountPasswordCreationView from '../AccountPasswordCreationView';
import { Provider } from 'react-redux';
import prospectService from '../../../../services/prospect.service';
import { Prospect } from '../../../../models/Prospect';
import { configureStore } from '@reduxjs/toolkit';
import { applicationReducer } from '../../../../store/store';
import AccountCampaignIntentCreationView from '../AccountCampaignIntentCreationView';
import configureTestStore from '../../../../utils/test/storeUtils';

describe('AccountPasswordCreationView', () => {
const user = userEvent.setup();
const store = configureStore({
reducer: applicationReducer,
preloadedState: { authentication: { authUser: genAuthUser() } },
});
const email = '[email protected]';
const link = genSignupLink(email);
const prospect: Prospect = {
...genProspect(),
email,
hasAccount: true,
hasCommitment: true,
hasCommitment: true
};

function setup() {
const router = createMemoryRouter(
[
{ path: '/inscription/en-attente', element: 'En attente' },
{ path: '/inscription/impossible', element: 'Impossible' },
{ path: '/inscription/email', element: 'Email' },
{
path: '/inscription/mot-de-passe',
element: <AccountPasswordCreationView />
},
{
path: '/inscription/campagne',
element: <AccountCampaignIntentCreationView />
}
],
{
initialEntries: [
{ pathname: '/inscription/mot-de-passe', hash: `#${link.id}` }
]
}
);
const store = configureTestStore();
render(
<Provider store={store}>
<Router
initialEntries={[
{
pathname: '/inscription/mot-de-passe',
hash: `#${link.id}`,
},
]}
>
<Route path="/inscription/en-attente">En attente</Route>
<Route path="/inscription/impossible">Impossible</Route>
<Route path="/inscription/email">Email</Route>
<Route
path="/inscription/mot-de-passe"
component={AccountPasswordCreationView}
/>
<Route
path="/inscription/campagne"
component={AccountCampaignIntentCreationView}
/>
</Router>
</Provider>,
<RouterProvider router={router} />
</Provider>
);
}

Expand Down Expand Up @@ -81,7 +74,7 @@ describe('AccountPasswordCreationView', () => {
it("should be forbidden if one's establishment does not exist in ZLV", async () => {
mockProspectServicePass({
...prospect,
establishment: null,
establishment: null
});
setup();

Expand All @@ -93,7 +86,7 @@ describe('AccountPasswordCreationView', () => {
mockProspectServicePass({
...prospect,
hasAccount: true,
hasCommitment: false,
hasCommitment: false
});
setup();

Expand All @@ -105,7 +98,7 @@ describe('AccountPasswordCreationView', () => {
mockProspectServicePass({
...prospect,
hasAccount: false,
hasCommitment: false,
hasCommitment: false
});
setup();

Expand All @@ -130,11 +123,11 @@ describe('AccountPasswordCreationView', () => {
const password = '123QWEasd';

const passwordInput = await screen.findByLabelText(
/Créer votre mot de passe/,
/Créer votre mot de passe/
);
await user.type(passwordInput, password);
const confirmationInput = await screen.findByLabelText(
/Confirmer votre mot de passe/,
/Confirmer votre mot de passe/
);
await user.type(confirmationInput, password);
await user.keyboard('{Enter}');
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/views/Housing/test/HousingView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker';
import { render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Provider } from 'react-redux';
import { MemoryRouter as Router, Route } from 'react-router-dom';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';

import {
HousingDTO,
Expand Down Expand Up @@ -40,11 +40,15 @@ describe('Housing view', () => {

function renderView(housing: HousingDTO) {
const store = configureTestStore();
const router = createMemoryRouter(
[{ path: '/housing/:housingId', element: <HousingView /> }],
{
initialEntries: [`/housing/${housing.id}`]
}
);
render(
<Provider store={store}>
<Router initialEntries={[`/housing/${housing.id}`]}>
<Route path="/housing/:housingId" component={HousingView} />
</Router>
<RouterProvider router={router} />
</Provider>
);
}
Expand Down
Loading

0 comments on commit 784e1a6

Please sign in to comment.