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 * as React from 'react';
22 import { getSystemUpgrades } from '../../../../api/system';
23 import { Alert } from '../../../../components/ui/Alert';
24 import DismissableAlert from '../../../../components/ui/DismissableAlert';
25 import { mockSystemUpgrade } from '../../../../helpers/mocks/system-upgrades';
26 import { mockAppState, mockCurrentUser, mockLoggedInUser } from '../../../../helpers/testMocks';
27 import { waitAndUpdate } from '../../../../helpers/testUtils';
28 import { Permissions } from '../../../../types/permissions';
29 import { UpdateNotification } from '../UpdateNotification';
31 jest.mock('../../../../api/system', () => {
32 const { mockSystemUpgrade } = jest.requireActual('../../../../helpers/mocks/system-upgrades');
34 getSystemUpgrades: jest
36 .mockResolvedValue({ upgrades: [mockSystemUpgrade({ version: '9.1' })], latestLTS: '8.9' }),
40 function formatDate(date: Date): string {
41 return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
44 it('should render correctly', async () => {
45 let wrapper = shallowRender({
46 appState: mockAppState({ version: '9.0' }),
47 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } }),
49 await waitAndUpdate(wrapper);
50 expect(wrapper).toMatchSnapshot('default');
52 wrapper = shallowRender({
53 appState: mockAppState({ version: '9.0' }),
54 currentUser: mockCurrentUser(),
56 expect(wrapper.type()).toBeNull();
59 it('should not show prompt when not admin', async () => {
61 const wrapper = shallowRender();
62 await waitAndUpdate(wrapper);
63 expect(wrapper.type()).toBeNull();
66 wrapper.setProps({ currentUser: mockLoggedInUser() });
67 await waitAndUpdate(wrapper);
68 expect(wrapper.type()).toBeNull();
71 it('should not show prompt when no current version', async () => {
72 const wrapper = shallowRender({ appState: mockAppState({ version: 'NOVERSION' }) });
73 await waitAndUpdate(wrapper);
74 expect(wrapper.type()).toBeNull();
77 it('should not show prompt when no upgrade', async () => {
78 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({ upgrades: [], latestLTS: '8.9' });
79 const wrapper = shallowRender({
80 appState: mockAppState({ version: '9.1' }),
81 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } }),
83 await waitAndUpdate(wrapper);
84 expect(wrapper.type()).toBeNull();
87 it('should show prompt when no lts date', async () => {
88 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
89 upgrades: [mockSystemUpgrade({ version: '8.9', releaseDate: 'INVALID' })],
92 const wrapper = shallowRender({
93 appState: mockAppState({ version: '8.1' }),
94 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } }),
96 await waitAndUpdate(wrapper);
97 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('previous_lts8.9');
98 expect(wrapper.contains('admin_notification.update.previous_lts')).toBe(true);
101 it('should show prompt when minor upgrade', async () => {
102 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
103 upgrades: [mockSystemUpgrade({ version: '9.2' }), mockSystemUpgrade({ version: '9.1' })],
106 const wrapper = shallowRender({
107 appState: mockAppState({ version: '9.1' }),
108 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } }),
110 await waitAndUpdate(wrapper);
111 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('new_minor_version9.2');
112 expect(wrapper.contains('admin_notification.update.new_minor_version')).toBe(true);
115 it('should show prompt when patch upgrade', async () => {
116 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
117 upgrades: [mockSystemUpgrade({ version: '9.2' }), mockSystemUpgrade({ version: '9.1.1' })],
120 const wrapper = shallowRender({
121 appState: mockAppState({ version: '9.1' }),
122 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } }),
124 await waitAndUpdate(wrapper);
125 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('new_patch9.2');
126 expect(wrapper.contains('admin_notification.update.new_patch')).toBe(true);
129 it('should show prompt when lts upgrade', async () => {
130 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
132 mockSystemUpgrade({ version: '8.9', releaseDate: formatDate(new Date(Date.now())) }),
133 mockSystemUpgrade({ version: '9.2' }),
134 mockSystemUpgrade({ version: '9.1.1' }),
138 const wrapper = shallowRender({
139 appState: mockAppState({ version: '8.8' }),
140 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } }),
142 await waitAndUpdate(wrapper);
143 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('pre_lts8.9');
144 expect(wrapper.contains('admin_notification.update.pre_lts')).toBe(true);
147 it('should show prompt when lts upgrade is more than 6 month', async () => {
148 const ltsDate = new Date(Date.now());
149 ltsDate.setMonth(ltsDate.getMonth() - 7);
150 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
152 mockSystemUpgrade({ version: '8.9', releaseDate: formatDate(ltsDate) }),
153 mockSystemUpgrade({ version: '9.2' }),
154 mockSystemUpgrade({ version: '9.1.1' }),
158 const wrapper = shallowRender({
159 appState: mockAppState({ version: '8.8' }),
160 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } }),
162 await waitAndUpdate(wrapper);
163 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('previous_lts8.9');
164 expect(wrapper.contains('admin_notification.update.previous_lts')).toBe(true);
167 it('should show correct alert when not dismissable', async () => {
168 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
170 mockSystemUpgrade({ version: '8.9', releaseDate: formatDate(new Date(Date.now())) }),
171 mockSystemUpgrade({ version: '9.2' }),
172 mockSystemUpgrade({ version: '9.1.1' }),
176 const wrapper = shallowRender({
177 appState: mockAppState({ version: '8.8' }),
178 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } }),
180 await waitAndUpdate(wrapper);
181 expect(wrapper.find(DismissableAlert).type).toBeDefined();
182 wrapper.setProps({ dismissable: false });
183 expect(wrapper.find(Alert).type).toBeDefined();
186 function shallowRender(props: Partial<UpdateNotification['props']> = {}) {
190 appState={mockAppState()}
191 currentUser={mockCurrentUser()}