Skip to content
Snippets Groups Projects
TermsView.spec.tsx 3.75 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { render, screen, waitFor } from '@testing-library/react'
    
    import { userEvent } from '@testing-library/user-event'
    
    import React from 'react'
    import { Provider } from 'react-redux'
    
    import * as storeHooks from 'store/hooks'
    
    import {
      createMockEcolyoStore,
      mockGlobalState,
      mockProfileState,
    } from 'tests/__mocks__/store'
    
    import { mockUpToDateTerm } from 'tests/__mocks__/termsData.mock'
    
    import TermsView from './TermsView'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    const mockCreateTerm = jest.fn()
    jest.mock('services/terms.service', () => {
    
      return jest.fn(() => ({
        createTerm: mockCreateTerm,
    
        getTermsVersionType: jest.fn(),
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    })
    
    
    const mockAppDispatch = jest.spyOn(storeHooks, 'useAppDispatch')
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    
    const mockUpdateProfile = jest.fn()
    jest.mock('services/profile.service', () => {
    
      return jest.fn(() => ({
        updateProfile: mockUpdateProfile,
      }))
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    })
    
    describe('TermsView component', () => {
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const store = createMockEcolyoStore()
    
    
      beforeEach(() => {
        jest.clearAllMocks()
      })
    
    
      it('should be rendered correctly', async () => {
    
        const { container } = render(
    
          <Provider store={store}>
            <TermsView />
          </Provider>
        )
    
        await waitFor(() => null, { container })
    
        expect(container).toMatchSnapshot()
    
    
      it('should open CGU modal', async () => {
        render(
          <Provider store={store}>
            <TermsView />
          </Provider>
        )
    
        await userEvent.click(
          screen.getByRole('button', {
            name: 'dataShare.validCGU_button',
          })
        )
    
        expect(screen.getByRole('dialog')).toBeInTheDocument()
      })
    
      it('should open legal notice modal', async () => {
        render(
          <Provider store={store}>
            <TermsView />
          </Provider>
        )
    
        await userEvent.click(
          screen.getByRole('button', { name: 'dataShare.validLegal_button' })
        )
    
        expect(screen.getByRole('dialog')).toBeInTheDocument()
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      })
    
      it('should be unable to click "validate" button first then enable checkboxes and valid consent', async () => {
        mockCreateTerm.mockResolvedValueOnce(mockUpToDateTerm)
        const { container } = render(
          <Provider store={store}>
            <TermsView />
          </Provider>
        )
        await waitFor(() => null, { container })
    
        const acceptButton = screen.getByLabelText(
          'dataShare.accessibility.button_accept'
        )
        expect(acceptButton).toBeDisabled()
        expect(mockUpdateProfile).toHaveBeenCalledTimes(0)
    
        const [boxGCU, boxLegal] = screen.getAllByRole('checkbox')
    
        await userEvent.click(boxGCU)
        await userEvent.click(boxLegal)
        await userEvent.click(acceptButton)
    
    
        expect(mockAppDispatch).toHaveBeenCalledTimes(4)
      })
    
    
      it('should not display the newsletter checkbox if already subscribed', async () => {
        const storeSubscribed = createMockEcolyoStore({
          global: {
            ...mockGlobalState,
            termsStatus: { accepted: false, versionType: 'init' },
          },
          profile: { ...mockProfileState, sendAnalysisNotification: true },
        })
    
        const { container } = render(
          <Provider store={storeSubscribed}>
            <TermsView />
          </Provider>
        )
        await waitFor(() => null, { container })
    
        const [, , boxNewsletter] = screen.getAllByRole('checkbox')
        expect(boxNewsletter).toBeUndefined()
      })
    
      it('should display the newsletter checkbox if not subscribed', async () => {
        const storeNotSubscribed = createMockEcolyoStore({
          global: {
            ...mockGlobalState,
            termsStatus: { accepted: false, versionType: 'init' },
          },
          profile: { ...mockProfileState, sendAnalysisNotification: false },
        })
        const { container } = render(
          <Provider store={storeNotSubscribed}>
            <TermsView />
          </Provider>
        )
        await waitFor(() => null, { container })
    
        const [, , boxNewsletter] = screen.getAllByRole('checkbox')
        expect(boxNewsletter).toBeInTheDocument()
      })
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    })