2022-05-16 04:03:42 -04:00
|
|
|
import { expect } from 'chai'
|
2022-05-24 07:12:21 -04:00
|
|
|
import sinon from 'sinon'
|
2022-05-16 04:03:42 -04:00
|
|
|
import { fireEvent, screen, render } from '@testing-library/react'
|
2022-05-24 03:47:08 -04:00
|
|
|
import { UserEmailsProvider } from '../../../../../frontend/js/features/settings/context/user-email-context'
|
2022-05-16 04:03:42 -04:00
|
|
|
import { LeaversSurveyAlert } from '../../../../../frontend/js/features/settings/components/leavers-survey-alert'
|
2022-05-24 07:12:21 -04:00
|
|
|
import * as eventTracking from '../../../../../frontend/js/infrastructure/event-tracking'
|
2022-05-18 09:45:31 -04:00
|
|
|
import localStorage from '../../../../../frontend/js/infrastructure/local-storage'
|
2023-03-16 08:32:48 -04:00
|
|
|
import fetchMock from 'fetch-mock'
|
2022-05-16 04:03:42 -04:00
|
|
|
|
2022-05-24 03:47:08 -04:00
|
|
|
function renderWithProvider() {
|
|
|
|
render(<LeaversSurveyAlert />, {
|
|
|
|
wrapper: ({ children }) => (
|
|
|
|
<UserEmailsProvider>{children}</UserEmailsProvider>
|
|
|
|
),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-16 04:03:42 -04:00
|
|
|
describe('<LeaversSurveyAlert/>', function () {
|
2023-03-16 08:32:48 -04:00
|
|
|
beforeEach(function () {
|
|
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [])
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
2022-05-16 04:03:42 -04:00
|
|
|
it('should render before the expiration date', function () {
|
|
|
|
const tomorrow = Date.now() + 1000 * 60 * 60 * 24
|
2022-05-18 09:45:31 -04:00
|
|
|
localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
|
|
|
|
localStorage.setItem('hideInstitutionalLeaversSurvey', false)
|
2022-05-24 03:47:08 -04:00
|
|
|
renderWithProvider()
|
2022-05-16 04:03:42 -04:00
|
|
|
screen.getByRole('alert')
|
|
|
|
screen.getByText(/Provide some quick feedback/)
|
|
|
|
screen.getByRole('link', { name: 'Take a short survey' })
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not render after the expiration date', function () {
|
|
|
|
const yesterday = Date.now() - 1000 * 60 * 60 * 24
|
2022-05-18 09:45:31 -04:00
|
|
|
localStorage.setItem('showInstitutionalLeaversSurveyUntil', yesterday)
|
|
|
|
localStorage.setItem('hideInstitutionalLeaversSurvey', false)
|
2022-05-24 03:47:08 -04:00
|
|
|
renderWithProvider()
|
2022-05-18 09:45:31 -04:00
|
|
|
expect(screen.queryByRole('alert')).to.be.null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not render if it has been hidden', function () {
|
|
|
|
const tomorrow = Date.now() + 1000 * 60 * 60 * 24
|
|
|
|
localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
|
|
|
|
localStorage.setItem('hideInstitutionalLeaversSurvey', true)
|
2022-05-24 03:47:08 -04:00
|
|
|
renderWithProvider()
|
2022-05-16 04:03:42 -04:00
|
|
|
expect(screen.queryByRole('alert')).to.be.null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should reset the expiration date when it is closed', function () {
|
|
|
|
const tomorrow = Date.now() + 1000 * 60 * 60 * 24
|
2022-05-18 09:45:31 -04:00
|
|
|
localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
|
|
|
|
localStorage.setItem('hideInstitutionalLeaversSurvey', false)
|
2022-05-24 03:47:08 -04:00
|
|
|
renderWithProvider()
|
2022-05-16 04:03:42 -04:00
|
|
|
screen.getByRole('alert')
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByRole('button'))
|
|
|
|
expect(screen.queryByRole('alert')).to.be.null
|
|
|
|
|
|
|
|
expect(localStorage.getItem('showInstitutionalLeaversSurveyUntil')).to.be
|
|
|
|
.null
|
|
|
|
})
|
2022-05-24 07:12:21 -04:00
|
|
|
|
|
|
|
describe('event tracking', function () {
|
2022-05-31 05:08:49 -04:00
|
|
|
let sendMBSpy: sinon.SinonSpy
|
2022-05-24 07:12:21 -04:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
sendMBSpy = sinon.spy(eventTracking, 'sendMB')
|
|
|
|
const tomorrow = Date.now() + 1000 * 60 * 60 * 24
|
|
|
|
localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
|
|
|
|
localStorage.setItem('hideInstitutionalLeaversSurvey', false)
|
|
|
|
renderWithProvider()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
sendMBSpy.restore()
|
|
|
|
localStorage.clear()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should sent a `view` event on load', function () {
|
|
|
|
expect(sendMBSpy).to.be.calledOnce
|
|
|
|
expect(sendMBSpy).calledWith(
|
|
|
|
'institutional-leavers-survey-notification',
|
2023-01-09 11:21:06 -05:00
|
|
|
{ type: 'view', page: '/' }
|
2022-05-24 07:12:21 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should sent a `click` event when the link is clicked', function () {
|
|
|
|
fireEvent.click(screen.getByRole('link'))
|
|
|
|
expect(sendMBSpy).to.be.calledTwice
|
|
|
|
expect(sendMBSpy).calledWith(
|
|
|
|
'institutional-leavers-survey-notification',
|
2023-01-09 11:21:06 -05:00
|
|
|
{ type: 'click', page: '/' }
|
2022-05-24 07:12:21 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should sent a `close` event when it is closed', function () {
|
|
|
|
fireEvent.click(screen.getByRole('button'))
|
|
|
|
expect(sendMBSpy).to.be.calledTwice
|
|
|
|
expect(sendMBSpy).calledWith(
|
|
|
|
'institutional-leavers-survey-notification',
|
2023-01-09 11:21:06 -05:00
|
|
|
{ type: 'close', page: '/' }
|
2022-05-24 07:12:21 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2022-05-16 04:03:42 -04:00
|
|
|
})
|