3 * Copyright (C) 2009-2021 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 DismissableAlert from '../../../../components/ui/DismissableAlert';
24 import { mockUpgrades } from '../../../../helpers/mocks/system-upgrades';
25 import { mockAppState, mockCurrentUser, mockLoggedInUser } from '../../../../helpers/testMocks';
26 import { waitAndUpdate } from '../../../../helpers/testUtils';
27 import { Permissions } from '../../../../types/permissions';
28 import { UpdateNotification } from '../UpdateNotification';
30 jest.mock('../../../../api/system', () => {
31 const { mockUpgrades } = jest.requireActual('../../../../helpers/mocks/system-upgrades');
33 getSystemUpgrades: jest
35 .mockResolvedValue({ upgrades: [mockUpgrades({ version: '9.1' })], latestLTS: '8.9' })
39 function formatDate(date: Date): string {
40 return `${date.getFullYear()}-${date.getMonth()}-${date.getDay()}`;
43 it('should render correctly', async () => {
44 const wrapper = shallowRender({
45 appState: mockAppState({ version: '9.0' }),
46 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })
48 await waitAndUpdate(wrapper);
49 expect(wrapper).toMatchSnapshot('default');
50 expect(wrapper.setProps({ currentUser: mockCurrentUser() })).toMatchSnapshot('anonymous user');
53 it('should not show prompt when not admin', async () => {
55 const wrapper = shallowRender();
56 await waitAndUpdate(wrapper);
57 expect(wrapper.type()).toBeNull();
60 wrapper.setProps({ currentUser: mockLoggedInUser() });
61 await waitAndUpdate(wrapper);
62 expect(wrapper.type()).toBeNull();
65 it('should not show prompt when no current version', async () => {
66 const wrapper = shallowRender({ appState: mockAppState({ version: 'NOVERSION' }) });
67 await waitAndUpdate(wrapper);
68 expect(wrapper.type()).toBeNull();
71 it('should not show prompt when no upgrade', async () => {
72 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({ upgrades: [], latestLTS: '8.9' });
73 const wrapper = shallowRender({
74 appState: mockAppState({ version: '9.1' }),
75 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })
77 await waitAndUpdate(wrapper);
78 expect(wrapper.type()).toBeNull();
81 it('should show prompt when no lts date', async () => {
82 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
83 upgrades: [mockUpgrades({ version: '8.9', releaseDate: 'INVALID' })],
86 const wrapper = shallowRender({
87 appState: mockAppState({ version: '8.1' }),
88 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })
90 await waitAndUpdate(wrapper);
91 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('previous_lts8.9');
92 expect(wrapper.contains('admin_notification.update.previous_lts')).toBe(true);
95 it('should show prompt when minor upgrade', async () => {
96 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
97 upgrades: [mockUpgrades({ version: '9.2' }), mockUpgrades({ version: '9.1' })],
100 const wrapper = shallowRender({
101 appState: mockAppState({ version: '9.1' }),
102 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })
104 await waitAndUpdate(wrapper);
105 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('new_minor_version9.2');
106 expect(wrapper.contains('admin_notification.update.new_minor_version')).toBe(true);
109 it('should show prompt when patch upgrade', async () => {
110 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
111 upgrades: [mockUpgrades({ version: '9.2' }), mockUpgrades({ version: '9.1.1' })],
114 const wrapper = shallowRender({
115 appState: mockAppState({ version: '9.1' }),
116 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })
118 await waitAndUpdate(wrapper);
119 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('new_patch9.2');
120 expect(wrapper.contains('admin_notification.update.new_patch')).toBe(true);
123 it('should show prompt when lts upgrade', async () => {
124 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
126 mockUpgrades({ version: '8.9', releaseDate: formatDate(new Date(Date.now())) }),
127 mockUpgrades({ version: '9.2' }),
128 mockUpgrades({ version: '9.1.1' })
132 const wrapper = shallowRender({
133 appState: mockAppState({ version: '8.8' }),
134 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })
136 await waitAndUpdate(wrapper);
137 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('pre_lts8.9');
138 expect(wrapper.contains('admin_notification.update.pre_lts')).toBe(true);
141 it('should show prompt when lts upgrade is more than 6 month', async () => {
142 const ltsDate = new Date(Date.now());
143 ltsDate.setMonth(ltsDate.getMonth() - 7);
144 (getSystemUpgrades as jest.Mock).mockResolvedValueOnce({
146 mockUpgrades({ version: '8.9', releaseDate: formatDate(ltsDate) }),
147 mockUpgrades({ version: '9.2' }),
148 mockUpgrades({ version: '9.1.1' })
152 const wrapper = shallowRender({
153 appState: mockAppState({ version: '8.8' }),
154 currentUser: mockLoggedInUser({ permissions: { global: [Permissions.Admin] } })
156 await waitAndUpdate(wrapper);
157 expect(wrapper.find(DismissableAlert).props().alertKey).toBe('previous_lts8.9');
158 expect(wrapper.contains('admin_notification.update.previous_lts')).toBe(true);
161 function shallowRender(props: Partial<UpdateNotification['props']> = {}) {
163 <UpdateNotification appState={mockAppState()} currentUser={mockCurrentUser()} {...props} />