mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2025-07-28 20:00:10 +02:00

* Update fetch-mock to version 12 * Replace `fetchMock.done` by `fetchMock.callHistory.done` * Replace `…Mock.called` by `…Mock.callHistory.called` * Replace `fetchMock.reset` by `fetchMock.hardReset` * Replace `fetchMock.restore` by `fetchMock.hardReset` * Replace `fetchMock.resetHistory` by `fetchMock.clearHistory` * Replace `fetchMock.calls` by `fetchMock.callHistory.calls` * Replace `fetchMock.flush` by `fetchMock.callHistory.flush` * Update tests for fetch-mock version 12 See https://www.wheresrhys.co.uk/fetch-mock/docs/Usage/upgrade-guide * Update stories for fetch-mock version 12 * Remove `overwriteRoutes` option * Add `fetchMock.spyGlobal()` to storybook * Remove deprecated `sendAsJson` param * Replace `fetchMock.hardReset()` by `fetchMock.removeRoutes().clearHistory()` * Fixup fetch-mock in storybook: Call `mockGlobal` inside the hook, call `removeRoutes` and `unmockGlobal` on cleanup Behaviour can be tested by navigating between https://storybook.dev-overleaf.com/main/?path=/story/editor-ai-error-assistant-compile-log-entries--first-log-entry https://storybook.dev-overleaf.com/main/?path=/story/editor-ai-error-assistant-compile-log-entries--rate-limited https://storybook.dev-overleaf.com/main/?path=/story/project-list-notifications--project-invite https://storybook.dev-overleaf.com/main/?path=/story/project-list-notifications--project-invite-network-error And clicking the buttons GitOrigin-RevId: 35611b4430259e4c21c3d819ad18b2e6dab66242
109 lines
3.8 KiB
TypeScript
109 lines
3.8 KiB
TypeScript
import { expect } from 'chai'
|
|
import sinon from 'sinon'
|
|
import { fireEvent, screen, render } from '@testing-library/react'
|
|
import { UserEmailsProvider } from '../../../../../frontend/js/features/settings/context/user-email-context'
|
|
import { LeaversSurveyAlert } from '../../../../../frontend/js/features/settings/components/leavers-survey-alert'
|
|
import * as eventTracking from '@/infrastructure/event-tracking'
|
|
import localStorage from '@/infrastructure/local-storage'
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
function renderWithProvider() {
|
|
render(<LeaversSurveyAlert />, {
|
|
wrapper: ({ children }) => (
|
|
<UserEmailsProvider>{children}</UserEmailsProvider>
|
|
),
|
|
})
|
|
}
|
|
|
|
describe('<LeaversSurveyAlert/>', function () {
|
|
beforeEach(function () {
|
|
fetchMock.get('/user/emails?ensureAffiliation=true', [])
|
|
})
|
|
|
|
afterEach(function () {
|
|
fetchMock.removeRoutes().clearHistory()
|
|
})
|
|
|
|
it('should render before the expiration date', function () {
|
|
const tomorrow = Date.now() + 1000 * 60 * 60 * 24
|
|
localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
|
|
localStorage.setItem('hideInstitutionalLeaversSurvey', false)
|
|
renderWithProvider()
|
|
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
|
|
localStorage.setItem('showInstitutionalLeaversSurveyUntil', yesterday)
|
|
localStorage.setItem('hideInstitutionalLeaversSurvey', false)
|
|
renderWithProvider()
|
|
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)
|
|
renderWithProvider()
|
|
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
|
|
localStorage.setItem('showInstitutionalLeaversSurveyUntil', tomorrow)
|
|
localStorage.setItem('hideInstitutionalLeaversSurvey', false)
|
|
renderWithProvider()
|
|
screen.getByRole('alert')
|
|
|
|
fireEvent.click(screen.getByRole('button'))
|
|
expect(screen.queryByRole('alert')).to.be.null
|
|
|
|
expect(localStorage.getItem('showInstitutionalLeaversSurveyUntil')).to.be
|
|
.null
|
|
})
|
|
|
|
describe('event tracking', function () {
|
|
let sendMBSpy: sinon.SinonSpy
|
|
|
|
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',
|
|
{ type: 'view', page: '/' }
|
|
)
|
|
})
|
|
|
|
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',
|
|
{ type: 'click', page: '/' }
|
|
)
|
|
})
|
|
|
|
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',
|
|
{ type: 'close', page: '/' }
|
|
)
|
|
})
|
|
})
|
|
})
|