This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
product_notices.test.tsx
176 lines (152 loc) · 7.12 KB
/
product_notices.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {shallow} from 'enzyme';
import GenericModal from 'components/generic_modal';
import {isDesktopApp, getDesktopVersion} from 'utils/user_agent';
import ProductNoticesModal from './product_notices_modal';
jest.mock('utils/user_agent');
describe('ProductNoticesModal', () => {
const noticesData = [{
id: '124',
title: 'for sysadmin',
description: 'your eyes only! [test](https://test.com)',
image: 'https://raw.githubusercontent.com/reflog/notices-experiment/master/images/2020-08-11_11-42.png',
actionText: 'Download',
actionParam: 'http:https://download.com/path',
sysAdminOnly: true,
teamAdminOnly: false,
},
{
id: '123',
title: 'title',
description: 'descr',
actionText: 'Download',
actionParam: 'http:https://download.com/path',
sysAdminOnly: false,
teamAdminOnly: false,
}];
const baseProps = {
version: '5.28.0',
currentTeamId: 'currentTeamId',
socketStatus: {
connected: true,
lastConnectAt: 1599760193593,
lastDisconnectAt: 0,
},
actions: {
getInProductNotices: jest.fn().mockResolvedValue({data: noticesData}),
updateNoticesAsViewed: jest.fn().mockResolvedValue({}),
},
};
test('Should match snapshot when there are no notices', async () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
expect(wrapper).toMatchSnapshot();
});
test('Should match snapshot for system admin notice', async () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices();
expect(wrapper).toMatchSnapshot();
});
test('Match snapshot for user notice', async () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices();
wrapper.setState({presentNoticeIndex: 1});
expect(wrapper).toMatchSnapshot();
});
test('Match snapshot for single notice', async () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices();
wrapper.setState({noticesData: [noticesData[1]]});
expect(wrapper).toMatchSnapshot();
});
test('Should change the state of presentNoticeIndex on click of next, previous button', async () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices();
expect(wrapper.state('presentNoticeIndex')).toBe(0);
wrapper.find(GenericModal).prop('handleConfirm')?.();
expect(wrapper.state('presentNoticeIndex')).toBe(1);
wrapper.find(GenericModal).prop('handleCancel')?.();
expect(wrapper.state('presentNoticeIndex')).toBe(0);
});
test('Should not have previous button if there is only one notice', async () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices();
expect(wrapper.find(GenericModal).props().handleCancel).toEqual(undefined);
});
test('Should not have previous button if there is only one notice', async () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices(baseProps.currentTeamId, 'web', baseProps.version);
expect(wrapper.find(GenericModal).props().handleCancel).toEqual(undefined);
});
test('Should open url in a new window on click of handleConfirm for single notice', async () => {
window.open = jest.fn();
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices();
wrapper.setState({noticesData: [noticesData[1]]});
wrapper.find(GenericModal).prop('handleConfirm')?.();
expect(window.open).toHaveBeenCalledWith(noticesData[1].actionParam, '_blank');
});
test('Should call for getInProductNotices and updateNoticesAsViewed on mount', async () => {
shallow(<ProductNoticesModal {...baseProps}/>);
expect(baseProps.actions.getInProductNotices).toHaveBeenCalledWith(baseProps.currentTeamId, 'web', baseProps.version);
await baseProps.actions.getInProductNotices();
expect(baseProps.actions.updateNoticesAsViewed).toHaveBeenCalledWith([noticesData[0].id]);
});
test('Should call for updateNoticesAsViewed on click of next button', async () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices();
wrapper.find(GenericModal).prop('handleConfirm')?.();
expect(baseProps.actions.updateNoticesAsViewed).toHaveBeenCalledWith([noticesData[1].id]);
});
test('Should clear state on onHide with a timer', async () => {
jest.useFakeTimers();
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
await baseProps.actions.getInProductNotices();
wrapper.find(GenericModal).prop('onHide')?.();
jest.runOnlyPendingTimers();
expect(wrapper.state('noticesData')).toEqual([]);
expect(wrapper.state('presentNoticeIndex')).toEqual(0);
});
test('Should call for getInProductNotices if socket reconnects for the first time in a day', () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
Date.now = jest.fn().mockReturnValue(1599807605628);
wrapper.setProps({
socketStatus: {
...baseProps.socketStatus,
connected: false,
},
});
wrapper.setProps({
socketStatus: {
...baseProps.socketStatus,
connected: true,
},
});
expect(baseProps.actions.getInProductNotices).toHaveBeenCalledWith(baseProps.currentTeamId, 'web', baseProps.version);
expect(baseProps.actions.getInProductNotices).toHaveBeenCalledTimes(2);
});
test('Should call for getInProductNotices with desktop as client if isDesktopApp returns true', () => {
(getDesktopVersion as any).mockReturnValue('4.5.0');
(isDesktopApp as any).mockReturnValue(true);
shallow(<ProductNoticesModal {...baseProps}/>);
expect(baseProps.actions.getInProductNotices).toHaveBeenCalledWith(baseProps.currentTeamId, 'desktop', '4.5.0');
});
test('Should not call for getInProductNotices if socket reconnects on the same day', () => {
const wrapper = shallow(<ProductNoticesModal {...baseProps}/>);
Date.now = jest.fn().mockReturnValue(1599760196593);
wrapper.setProps({
socketStatus: {
...baseProps.socketStatus,
connected: false,
},
});
wrapper.setProps({
socketStatus: {
...baseProps.socketStatus,
connected: true,
},
});
expect(baseProps.actions.getInProductNotices).toHaveBeenCalledTimes(1);
});
});