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
/
post_utils.test.js
144 lines (127 loc) · 5.61 KB
/
post_utils.test.js
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import thunk from 'redux-thunk';
import configureStore from 'redux-mock-store';
import {receivedNewPost} from 'mattermost-redux/actions/posts';
import {Posts} from 'mattermost-redux/constants';
import * as PostActionsUtils from 'actions/post_utils';
import {Constants} from 'utils/constants';
const mockStore = configureStore([thunk]);
jest.mock('mattermost-redux/actions/channels', () => ({
markChannelAsUnread: (...args) => ({type: 'MOCK_MARK_CHANNEL_AS_UNREAD', args}),
markChannelAsRead: (...args) => ({type: 'MOCK_MARK_CHANNEL_AS_READ', args}),
markChannelAsViewed: (...args) => ({type: 'MOCK_MARK_CHANNEL_AS_VIEWED', args}),
}));
const EMPTY_ACTION = [];
const MARK_CHANNEL_AS_UNREAD = {
args: ['team_id', 'current_channel_id', ['current_user_id']],
type: 'MOCK_MARK_CHANNEL_AS_UNREAD',
};
const MARK_CHANNEL_AS_READ = {
args: ['current_channel_id', null, false],
type: 'MOCK_MARK_CHANNEL_AS_READ',
};
const MARK_CHANNEL_AS_VIEWED = {
args: ['current_channel_id'],
type: 'MOCK_MARK_CHANNEL_AS_VIEWED',
};
const POST_CREATED_TIME = Date.now();
// This mocks the Date.now() function so it returns a constant value
global.Date.now = jest.fn(() => POST_CREATED_TIME);
const INCREASED_POST_VISIBILITY = {amount: 1, data: 'current_channel_id', type: 'INCREASE_POST_VISIBILITY'};
const STOP_TYPING = {type: 'stop_typing', data: {id: 'current_channel_idundefined', now: POST_CREATED_TIME, userId: 'some_user_id'}};
describe('actions/post_utils', () => {
const latestPost = {
id: 'latest_post_id',
user_id: 'current_user_id',
message: 'test msg',
channel_id: 'current_channel_id',
};
const initialState = {
entities: {
posts: {
posts: {
[latestPost.id]: latestPost,
},
postsInChannel: {
current_channel_id: [
{order: [latestPost.id], recent: true},
],
},
postsInThread: {},
messagesHistory: {
index: {
[Posts.MESSAGE_TYPES.COMMENT]: 0,
},
messages: ['test message'],
},
},
channels: {
currentChannelId: 'current_channel_id',
myMembers: {[latestPost.channel_id]: {channel_id: 'current_channel_id', user_id: 'current_user_id'}},
},
teams: {
currentTeamId: 'team-1',
teams: {
team_id: {
id: 'team_id',
name: 'team-1',
displayName: 'Team 1',
},
},
},
users: {
currentUserId: 'current_user_id',
},
general: {license: {IsLicensed: 'false'}},
},
views: {
posts: {
editingPost: {},
},
},
};
test('completePostReceive', async () => {
const testStore = await mockStore(initialState);
const newPost = {id: 'new_post_id', channel_id: 'current_channel_id', message: 'new message', type: Constants.PostTypes.ADD_TO_CHANNEL, user_id: 'some_user_id', create_at: POST_CREATED_TIME};
const websocketProps = {team_id: 'team_id', mentions: ['current_user_id']};
await testStore.dispatch(PostActionsUtils.completePostReceive(newPost, websocketProps));
expect(testStore.getActions()).toEqual([
INCREASED_POST_VISIBILITY,
{
meta: {batch: true},
payload: [receivedNewPost(newPost), STOP_TYPING],
type: 'BATCHING_REDUCER.BATCH',
},
]);
});
test('lastPostActions', async () => {
const testStore = await mockStore(initialState);
const newPost = {id: 'new_post_id', channel_id: 'current_channel_id', message: 'new message', type: Constants.PostTypes.ADD_TO_CHANNEL, user_id: 'some_user_id', create_at: POST_CREATED_TIME};
const websocketProps = {team_id: 'team_id', mentions: ['current_user_id']};
await testStore.dispatch(PostActionsUtils.lastPostActions(newPost, websocketProps));
expect(testStore.getActions()).toEqual([
INCREASED_POST_VISIBILITY,
{
meta: {batch: true},
payload: [receivedNewPost(newPost), STOP_TYPING],
type: 'BATCHING_REDUCER.BATCH',
},
]);
});
test('setChannelReadAndView', async () => {
let testStore = await mockStore(initialState);
const newPost = {id: 'new_post_id', channel_id: 'current_channel_id', message: 'new message', type: Constants.PostTypes.ADD_TO_CHANNEL};
const websocketProps = {team_id: 'team_id', mentions: ['current_user_id']};
await testStore.dispatch(PostActionsUtils.setChannelReadAndView(newPost, websocketProps));
expect(testStore.getActions()).toEqual(EMPTY_ACTION);
testStore = await mockStore(initialState);
newPost.type = '';
await testStore.dispatch(PostActionsUtils.setChannelReadAndView(newPost, websocketProps));
expect(testStore.getActions()).toEqual([MARK_CHANNEL_AS_UNREAD]);
testStore = await mockStore(initialState);
newPost.user_id = 'current_user_id';
await testStore.dispatch(PostActionsUtils.setChannelReadAndView(newPost, websocketProps));
expect(testStore.getActions()).toEqual([MARK_CHANNEL_AS_READ, MARK_CHANNEL_AS_VIEWED]);
});
});