error-assistant-ai-paywall-notification.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { FC, PropsWithChildren, useState } from 'react'
  2. import { DetachCompileContext } from '@/shared/context/detach-compile-context'
  3. import ErrorAssistantAiPaywallNotification from '@/features/pdf-preview/components/error-assistant-ai-paywall-notification'
  4. import {
  5. EditorProviders,
  6. makeEditorProvider,
  7. } from '../../helpers/editor-providers'
  8. const PAYWALL_TEXT = 'You’ve reached the fair usage limit on your plan'
  9. const futureDate = () => new Date(Date.now() + 60 * 60 * 1000)
  10. function dispatchSuggestFixPaywall() {
  11. cy.window().then(win => {
  12. win.dispatchEvent(
  13. new CustomEvent('aiAssist:showPaywall', {
  14. detail: { origin: 'suggest-fix' },
  15. })
  16. )
  17. })
  18. }
  19. function makeShowLogsCompileProvider(initialShowLogs: boolean) {
  20. const Provider: FC<PropsWithChildren> = ({ children }) => {
  21. const [showLogs, setShowLogs] = useState(initialShowLogs)
  22. return (
  23. <DetachCompileContext.Provider value={{ showLogs, setShowLogs } as any}>
  24. <button
  25. type="button"
  26. data-testid="toggle-logs"
  27. onClick={() => setShowLogs(prev => !prev)}
  28. >
  29. toggle logs
  30. </button>
  31. {children}
  32. </DetachCompileContext.Provider>
  33. )
  34. }
  35. return Provider
  36. }
  37. function mountSuggestFixPaywall(initialShowLogs = true) {
  38. cy.window().then(win => {
  39. win.metaAttributesCache.set('ol-showAiFeatures', true)
  40. })
  41. cy.mount(
  42. <EditorProviders
  43. features={{ aiErrorAssistant: true }}
  44. providers={{
  45. EditorProvider: makeEditorProvider({
  46. hasSuggestionsLeft: false,
  47. premiumSuggestionResetDate: futureDate(),
  48. }),
  49. DetachCompileProvider: makeShowLogsCompileProvider(initialShowLogs),
  50. }}
  51. >
  52. <ErrorAssistantAiPaywallNotification />
  53. </EditorProviders>
  54. )
  55. }
  56. describe('<ErrorAssistantAiPaywallNotification />', function () {
  57. it('does not render the paywall before the suggest-fix paywall event fires', function () {
  58. mountSuggestFixPaywall()
  59. cy.contains(PAYWALL_TEXT).should('not.exist')
  60. })
  61. it('renders the paywall after the suggest-fix paywall event fires', function () {
  62. mountSuggestFixPaywall()
  63. dispatchSuggestFixPaywall()
  64. cy.contains(PAYWALL_TEXT).should('be.visible')
  65. })
  66. it('ignores paywall events from other origins', function () {
  67. mountSuggestFixPaywall()
  68. cy.window().then(win => {
  69. win.dispatchEvent(
  70. new CustomEvent('aiAssist:showPaywall', {
  71. detail: { origin: 'workbench' },
  72. })
  73. )
  74. })
  75. cy.contains(PAYWALL_TEXT).should('not.exist')
  76. })
  77. it('hides the paywall when the logs panel is closed', function () {
  78. mountSuggestFixPaywall()
  79. dispatchSuggestFixPaywall()
  80. cy.contains(PAYWALL_TEXT).should('be.visible')
  81. cy.findByTestId('toggle-logs').click()
  82. cy.contains(PAYWALL_TEXT).should('not.exist')
  83. })
  84. it('does not re-show the paywall when the logs panel is reopened', function () {
  85. mountSuggestFixPaywall()
  86. dispatchSuggestFixPaywall()
  87. cy.findByTestId('toggle-logs').click()
  88. cy.findByTestId('toggle-logs').click()
  89. cy.contains(PAYWALL_TEXT).should('not.exist')
  90. })
  91. })