Newer
Older
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 {
createMockEcolyoStore,
mockGlobalState,
mockProfileState,
} from 'tests/__mocks__/store'
import { mockUpToDateTerm } from 'tests/__mocks__/termsData.mock'
import TermsView from './TermsView'
const mockCreateTerm = jest.fn()
jest.mock('services/terms.service', () => {
return jest.fn(() => ({
createTerm: mockCreateTerm,
getTermsVersionType: jest.fn(),
const mockAppDispatch = jest.spyOn(storeHooks, 'useAppDispatch')
const mockUpdateProfile = jest.fn()
jest.mock('services/profile.service', () => {
return jest.fn(() => ({
updateProfile: mockUpdateProfile,
}))
beforeEach(() => {
jest.clearAllMocks()
})
it('should be rendered correctly', async () => {
<Provider store={store}>
<TermsView />
</Provider>
)
await waitFor(() => null, { container })
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()
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)
})
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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()
})