3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import { shallow } from 'enzyme';
21 import React from 'react';
22 import { dismissNotice } from '../../../../api/users';
23 import { mockCurrentUser, mockLoggedInUser } from '../../../../helpers/testMocks';
24 import { waitAndUpdate } from '../../../../helpers/testUtils';
25 import { NoticeType } from '../../../../types/users';
26 import { CurrentUserContextInterface } from '../../current-user/CurrentUserContext';
27 import { PromotionNotification } from '../PromotionNotification';
29 jest.mock('../../../../api/users', () => ({
30 dismissNotice: jest.fn().mockResolvedValue({}),
37 it('should render correctly', () => {
38 expect(shallowRender()).toMatchSnapshot('anonymous');
41 currentUser: mockLoggedInUser({ dismissedNotices: { [NoticeType.SONARLINT_AD]: true } }),
43 ).toMatchSnapshot('adAlreadySeen');
44 expect(shallowRender({ currentUser: mockLoggedInUser() })).toMatchSnapshot('loggedIn');
47 it('should remove the toaster when click on dismiss', async () => {
48 const updateDismissedNotices = jest.fn();
49 const wrapper = shallowRender({
50 currentUser: mockLoggedInUser({ dismissedNotices: { [NoticeType.SONARLINT_AD]: false } }),
51 updateDismissedNotices,
53 wrapper.find('.toaster-actions ButtonLink').simulate('click');
54 expect(dismissNotice).toHaveBeenCalled();
55 await waitAndUpdate(wrapper);
56 expect(updateDismissedNotices).toHaveBeenCalled();
59 it('should remove the toaster and navigate to sonarlint when click on learn more', async () => {
60 const updateDismissedNotices = jest.fn();
61 const wrapper = shallowRender({
62 currentUser: mockLoggedInUser({ dismissedNotices: { [NoticeType.SONARLINT_AD]: false } }),
63 updateDismissedNotices,
65 wrapper.find('.toaster-actions .button-primary').simulate('click');
66 expect(dismissNotice).toHaveBeenCalled();
67 await waitAndUpdate(wrapper);
68 expect(updateDismissedNotices).toHaveBeenCalled();
71 function shallowRender(props: Partial<CurrentUserContextInterface> = {}) {
73 <PromotionNotification
74 currentUser={mockCurrentUser()}
75 updateDismissedNotices={jest.fn()}
76 updateCurrentUserHomepage={jest.fn()}