Files
overleaf-cep/services/web/test/frontend/components/pdf-preview/error-assistant-ai-paywall-notification.spec.tsx
Mathias Jakobsen 76fbb56107 [web] Delay suggest fix paywall until suggest button has been clicked (#33458)
GitOrigin-RevId: 11d2ec0c9c33aea3fedff57d5f1a74d6ce774017
2026-05-07 08:08:36 +00:00

104 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { FC, PropsWithChildren, useState } from 'react'
import { DetachCompileContext } from '@/shared/context/detach-compile-context'
import ErrorAssistantAiPaywallNotification from '@/features/pdf-preview/components/error-assistant-ai-paywall-notification'
import {
EditorProviders,
makeEditorProvider,
} from '../../helpers/editor-providers'
const PAYWALL_TEXT = 'Youve reached the fair usage limit on your plan'
const futureDate = () => new Date(Date.now() + 60 * 60 * 1000)
function dispatchSuggestFixPaywall() {
cy.window().then(win => {
win.dispatchEvent(
new CustomEvent('aiAssist:showPaywall', {
detail: { origin: 'suggest-fix' },
})
)
})
}
function makeShowLogsCompileProvider(initialShowLogs: boolean) {
const Provider: FC<PropsWithChildren> = ({ children }) => {
const [showLogs, setShowLogs] = useState(initialShowLogs)
return (
<DetachCompileContext.Provider value={{ showLogs, setShowLogs } as any}>
<button
type="button"
data-testid="toggle-logs"
onClick={() => setShowLogs(prev => !prev)}
>
toggle logs
</button>
{children}
</DetachCompileContext.Provider>
)
}
return Provider
}
function mountSuggestFixPaywall(initialShowLogs = true) {
cy.window().then(win => {
win.metaAttributesCache.set('ol-showAiFeatures', true)
})
cy.mount(
<EditorProviders
features={{ aiErrorAssistant: true }}
providers={{
EditorProvider: makeEditorProvider({
hasSuggestionsLeft: false,
premiumSuggestionResetDate: futureDate(),
}),
DetachCompileProvider: makeShowLogsCompileProvider(initialShowLogs),
}}
>
<ErrorAssistantAiPaywallNotification />
</EditorProviders>
)
}
describe('<ErrorAssistantAiPaywallNotification />', function () {
it('does not render the paywall before the suggest-fix paywall event fires', function () {
mountSuggestFixPaywall()
cy.contains(PAYWALL_TEXT).should('not.exist')
})
it('renders the paywall after the suggest-fix paywall event fires', function () {
mountSuggestFixPaywall()
dispatchSuggestFixPaywall()
cy.contains(PAYWALL_TEXT).should('be.visible')
})
it('ignores paywall events from other origins', function () {
mountSuggestFixPaywall()
cy.window().then(win => {
win.dispatchEvent(
new CustomEvent('aiAssist:showPaywall', {
detail: { origin: 'workbench' },
})
)
})
cy.contains(PAYWALL_TEXT).should('not.exist')
})
it('hides the paywall when the logs panel is closed', function () {
mountSuggestFixPaywall()
dispatchSuggestFixPaywall()
cy.contains(PAYWALL_TEXT).should('be.visible')
cy.findByTestId('toggle-logs').click()
cy.contains(PAYWALL_TEXT).should('not.exist')
})
it('does not re-show the paywall when the logs panel is reopened', function () {
mountSuggestFixPaywall()
dispatchSuggestFixPaywall()
cy.findByTestId('toggle-logs').click()
cy.findByTestId('toggle-logs').click()
cy.contains(PAYWALL_TEXT).should('not.exist')
})
})