1 import { fireEvent, screen } from '@testing-library/react';
3 import { getModelState } from '@proton/account/test';
4 import { TopNavbarUpsell } from '@proton/components';
5 import type { FeatureCode } from '@proton/features';
6 import { APPS } from '@proton/shared/lib/constants';
7 import { setBit } from '@proton/shared/lib/helpers/bitset';
8 import { NEWSLETTER_SUBSCRIPTIONS_BITS } from '@proton/shared/lib/helpers/newsletter';
9 import type { UserModel, UserSettings } from '@proton/shared/lib/interfaces';
11 import { renderWithProviders } from '../contacts/tests/render';
12 import { OffersTestProvider } from './Offers.test.helpers';
13 import type { OfferConfig } from './interface';
15 const OFFER_CONTENT = 'deal deal deal deal deal';
17 const offerConfig: OfferConfig = {
19 featureCode: 'testOffer2022' as FeatureCode,
20 layout: () => <div>{OFFER_CONTENT}</div>,
21 ID: 'test-offer-2022' as OfferConfig['ID'],
25 const offerConfigAutopopup: OfferConfig = {
27 featureCode: 'testOffer2022' as FeatureCode,
28 layout: () => <div>{OFFER_CONTENT}</div>,
29 ID: 'test-offer-2022' as OfferConfig['ID'],
31 autoPopUp: 'one-time',
34 jest.mock('./hooks/useOfferConfig', function () {
39 .mockReturnValueOnce([undefined, false])
40 .mockReturnValueOnce([undefined, false])
41 .mockReturnValueOnce([offerConfig, false])
42 .mockReturnValueOnce([offerConfig, false])
43 .mockReturnValue([offerConfigAutopopup, false]),
47 jest.mock('./hooks/useFetchOffer', function () {
50 default: jest.fn(({ offerConfig }) => [offerConfig, false]),
54 jest.mock('./hooks/useOfferFlags', function () {
57 default: jest.fn(() => ({
61 handleVisit: () => {},
67 const TopNavbarComponent = () => (
69 <TopNavbarUpsell app={APPS.PROTONMAIL} />
73 const getUserSettings = ({ offersEnabled }: { offersEnabled: boolean }): UserSettings => {
75 News: offersEnabled ? setBit(0, NEWSLETTER_SUBSCRIPTIONS_BITS.IN_APP_NOTIFICATIONS) : 0,
79 const getUser = ({ isFree }: { isFree: boolean }) => {
85 describe('Offers', () => {
86 describe('Offers display', () => {
87 it('Should display upgrade button for free users', () => {
88 renderWithProviders(<TopNavbarComponent />, {
90 user: getModelState(getUser({ isFree: true })),
91 userSettings: getModelState(getUserSettings({ offersEnabled: true })),
95 const link = screen.getByTestId('cta:upgrade-plan');
97 expect(link.textContent).toContain('Upgrade');
100 it('Should display nothing for paid users with offers', () => {
101 renderWithProviders(<TopNavbarComponent />, {
103 user: getModelState(getUser({ isFree: false })),
104 userSettings: getModelState(getUserSettings({ offersEnabled: true })),
108 expect(screen.queryByTestId('cta:upgrade-plan')).toBeNull();
111 describe('Non free user with valid offer', () => {
112 it('Should not display an offer button with setting turned off', () => {
113 renderWithProviders(<TopNavbarComponent />, {
115 user: getModelState(getUser({ isFree: false })),
116 userSettings: getModelState(getUserSettings({ offersEnabled: false })),
120 expect(screen.queryByTestId('cta:special-offer')).toBeNull();
123 it('Should display an offer button', () => {
124 renderWithProviders(<TopNavbarComponent />, {
126 user: getModelState(getUser({ isFree: false })),
127 userSettings: getModelState(getUserSettings({ offersEnabled: true })),
131 expect(screen.getByTestId('cta:special-offer')?.textContent).toBe('Special offer');
134 it('Should open a modal with offer content', () => {
135 renderWithProviders(<TopNavbarComponent />, {
137 user: getModelState(getUser({ isFree: false })),
138 userSettings: getModelState(getUserSettings({ offersEnabled: true })),
142 const specialOfferCta = screen.getByTestId('cta:special-offer');
144 fireEvent.click(specialOfferCta);
146 screen.getByText(OFFER_CONTENT);
149 it.skip('Should open a modal when autopopup', () => {
150 renderWithProviders(<TopNavbarComponent />, {
152 user: getModelState(getUser({ isFree: false })),
153 userSettings: getModelState(getUserSettings({ offersEnabled: true })),
157 screen.getByText(OFFER_CONTENT);