Skip to content

Commit

Permalink
Un utilisateur peut créer une simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
NerOcrO committed May 7, 2024
1 parent 20ee7c6 commit 9bd3f4d
Show file tree
Hide file tree
Showing 12 changed files with 619 additions and 15 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/consistent-type-imports": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-member-accessibility": "off",
"@typescript-eslint/naming-convention": "off",
"@typescript-eslint/max-params": "off",
"@typescript-eslint/no-magic-numbers": "off",
Expand Down
40 changes: 40 additions & 0 deletions src/app/(connecte)/creer-une-simulation/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Metadata } from 'next'
import { notFound } from 'next/navigation'
import { ReactElement } from 'react'

import { getProfilAtih } from '../../../authentification'
import Breadcrumb from '../../../components/commun/Breadcrumb'
import CreerUneSimulation from '../../../components/CreerUneSimulation/CreerUneSimulation'

const title = 'Créer une simulation'
export const metadata: Metadata = {
title,
}

type PageProps = Readonly<{
searchParams?: Readonly<{
nomInventaire?: string
}>
}>

export default async function PageCreerUneSimulation({ searchParams }: PageProps): Promise<ReactElement> {
if (searchParams?.nomInventaire === undefined) {
notFound()
}

const profil = await getProfilAtih()

if (profil.isAdmin) {
notFound()
}

return (
<>
<Breadcrumb label={title} />
<CreerUneSimulation
ancienNomInventaire={searchParams.nomInventaire}
nomEtablissement={profil.nomEtablissement}
/>
</>
)
}
14 changes: 8 additions & 6 deletions src/app/(connecte)/inventaire/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ export const metadata: Metadata = {
title,
}

type PageProps = Readonly<{
export type PageProps = Readonly<{
searchParams: Readonly<{
dureeDeVie?: string
heureUtilisation?: string
nombreEquipement?: string
nomEtablissement?: string
nomInventaire?: string
statut?: string
nouveauNomInventaire?: string
statut?: StatutsInventaire
}>
}>

Expand All @@ -38,15 +42,13 @@ export default async function Page({ searchParams }: PageProps): Promise<ReactEl

const referentielsTypesEquipementsModel = await recupererLesReferentielsTypesEquipementsRepository()

const statut = searchParams.statut === undefined ? StatutsInventaire.EN_ATTENTE : searchParams.statut as StatutsInventaire

return (
<>
<Breadcrumb label={title} />
<Inventaire
nomEtablissement={searchParams.nomEtablissement}
nomInventaire={searchParams.nomInventaire}
presenter={inventairePresenter(referentielsTypesEquipementsModel, modelesModel, statut)}
nomInventaire={searchParams.nouveauNomInventaire ?? searchParams.nomInventaire}
presenter={inventairePresenter(referentielsTypesEquipementsModel, modelesModel, searchParams)}
/>
</>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.astuce {
background-color: #EFF4FD;
}
162 changes: 162 additions & 0 deletions src/components/CreerUneSimulation/CreerUneSimulation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { inventaireModel } from '@prisma/client'
import { fireEvent, screen } from '@testing-library/react'
import * as navigation from 'next/navigation'

import PageCreerUneSimulation from '../../app/(connecte)/creer-une-simulation/page'
import * as repository from '../../gateways/inventairesRepository'
import { jeSuisUnAdmin, jeSuisUnUtilisateur, renderComponent } from '../../testShared'

describe('page créer une simulation', () => {
describe('en tant qu’utilisateur', () => {
it('quand j’affiche la page alors j’ai le nom de l’inventaire prérempli avec comme suffixe le mot simulation et la date', async () => {
// GIVEN
jeSuisUnUtilisateur()

// WHEN
renderComponent(await PageCreerUneSimulation(queryParams()))

// THEN
const champNomInventaire = screen.getByLabelText('Nom de l’inventaire * (minimum 4 caractères)')
expect(champNomInventaire).toHaveValue('Centre hospitalier - simulation 17/12/1995 03:24:00')

const boutonContinuer = screen.getByRole('button', { name: 'Continuer' })
expect(boutonContinuer).toBeEnabled()
})

it('quand j’affiche la page alors je ne peux pas dépasser -100 et 100 dans le champ nombre d’équipement en %', async () => {
// GIVEN
jeSuisUnUtilisateur()

// WHEN
renderComponent(await PageCreerUneSimulation(queryParams()))

// THEN
const champNombreEquipement = screen.getByLabelText('Nombre d’équipements en %')
expect(champNombreEquipement).toHaveAttribute('max', '100')
expect(champNombreEquipement).toHaveAttribute('min', '-100')
expect(champNombreEquipement).toHaveAttribute('type', 'number')
expect(champNombreEquipement).toHaveValue(0)
})

it('quand j’affiche la page alors je ne peux pas dépasser -20 et 20 dans le champ Durée de vie en années', async () => {
// GIVEN
jeSuisUnUtilisateur()

// WHEN
renderComponent(await PageCreerUneSimulation(queryParams()))

// THEN
const champDureeDeVie = screen.getByLabelText('Durée de vie en années')
expect(champDureeDeVie).toHaveAttribute('max', '20')
expect(champDureeDeVie).toHaveAttribute('min', '-20')
expect(champDureeDeVie).toHaveAttribute('type', 'number')
expect(champDureeDeVie).toHaveValue(0)
})

it('quand j’affiche la page alors je ne peux pas dépasser -24 et 24 dans le champ Heures d’utilisation par jour', async () => {
// GIVEN
jeSuisUnUtilisateur()

// WHEN
renderComponent(await PageCreerUneSimulation(queryParams()))

// THEN
const champHeuresUtilisation = screen.getByLabelText('Heures d’utilisation par jour')
expect(champHeuresUtilisation).toHaveAttribute('max', '24')
expect(champHeuresUtilisation).toHaveAttribute('min', '-24')
expect(champHeuresUtilisation).toHaveAttribute('type', 'number')
expect(champHeuresUtilisation).toHaveValue(0)
})

it('quand j’écris un nom d’inventaire inférieur à 4 caractères alors je ne peux pas créer la simulation', async () => {
// GIVEN
jeSuisUnUtilisateur()
renderComponent(await PageCreerUneSimulation(queryParams()))
const champNomInventaire = screen.getByLabelText('Nom de l’inventaire * (minimum 4 caractères)')

// WHEN
fireEvent.change(champNomInventaire, { target: { value: '???' } })

// THEN
const boutonContinuer = screen.getByRole('button', { name: 'Continuer' })
expect(boutonContinuer).toBeDisabled()
})

it('quand je valide le formulaire avec un nom d’inventaire qui existe déjà alors j’ai un message d’erreur', async () => {
// GIVEN
vi.spyOn(repository, 'recupererUnInventaireRepository').mockResolvedValueOnce({} as inventaireModel)
jeSuisUnUtilisateur()
renderComponent(await PageCreerUneSimulation(queryParams()))
const champNomInventaire = screen.getByLabelText('Nom de l’inventaire * (minimum 4 caractères)')
fireEvent.change(champNomInventaire, { target: { value: 'nom inventaire dejà exitant' } })
const boutonContinuer = screen.getByRole('button', { name: 'Continuer' })

// WHEN
fireEvent.click(boutonContinuer)

// THEN
const champNomInventaireMaj = await screen.findByLabelText('Nom de l’inventaire * (minimum 4 caractères)')
expect(champNomInventaireMaj).toHaveAttribute('aria-describedby', 'formInputError-error')
expect(champNomInventaireMaj).toHaveAttribute('aria-invalid', 'true')

const textErreur = await screen.findByText('Cet inventaire existe déjà. Modifiez le nom de l’inventaire pour continuer.', { selector: 'p' })
expect(textErreur).toBeInTheDocument()
})

it('quand je valide le formulaire alors je vais à la suite', async () => {
// GIVEN
vi.spyOn(repository, 'recupererUnInventaireRepository').mockResolvedValue(null)
jeSuisUnUtilisateur()
renderComponent(await PageCreerUneSimulation(queryParams()))

const champNomInventaire = screen.getByLabelText('Nom de l’inventaire * (minimum 4 caractères)')
fireEvent.change(champNomInventaire, { target: { value: 'nom inventaire correct' } })
const boutonContinuer = screen.getByRole('button', { name: 'Continuer' })
const champNombreEquipement = screen.getByLabelText('Nombre d’équipements en %')
fireEvent.change(champNombreEquipement, { target: { value: 10 } })
const champDureeDeVie = screen.getByLabelText('Durée de vie en années')
fireEvent.change(champDureeDeVie, { target: { value: 10 } })
const champHeuresUtilisation = screen.getByLabelText('Heures d’utilisation par jour')
fireEvent.change(champHeuresUtilisation, { target: { value: 10 } })

// WHEN
fireEvent.click(boutonContinuer)

// THEN
expect(navigation.useRouter).toHaveBeenCalledWith()
// expect(navigation.useRouter.push).toHaveBeenCalledWith('')
})
})

describe('en tant qu’admin', () => {
it('quand j’affiche la page alors je n’y ai pas accès', async () => {
// GIVEN
jeSuisUnAdmin()

// WHEN
const page = async () => renderComponent(await PageCreerUneSimulation(queryParams()))

// THEN
await expect(page).rejects.toThrow('NEXT_NOT_FOUND')
})
})

it('quand il n’y a pas de nom d’inventaire dans l’url alors je n’y ai pas accès', async () => {
// GIVEN
const queryParams = {}

// WHEN
const page = async () => renderComponent(await PageCreerUneSimulation(queryParams))

// THEN
await expect(page).rejects.toThrow('NEXT_NOT_FOUND')
})
})

function queryParams() {
return {
searchParams: {
nomInventaire: 'Centre hospitalier',
},
}
}
Loading

0 comments on commit 9bd3f4d

Please sign in to comment.