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_list.test.jsx
165 lines (129 loc) · 5.61 KB
/
post_list.test.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {shallow} from 'enzyme';
import {DATE_LINE} from 'mattermost-redux/utils/post_list';
import LoadingScreen from 'components/loading_screen';
import {PostListRowListIds} from 'utils/constants';
import NewMessagesBelow from './new_messages_below';
import PostList from './post_list';
import PostListRow from './post_list_row';
describe('PostList', () => {
const baseProps = {
channel: {id: 'channel'},
focusedPostId: '',
lastViewedAt: 0,
postListIds: [
'post1',
'post2',
'post3',
DATE_LINE + 1551711600000,
],
postVisibility: 10,
actions: {
checkAndSetMobileView: jest.fn(),
increasePostVisibility: jest.fn(),
loadInitialPosts: jest.fn(() => ({posts: {posts: {}, order: []}, hasMoreBefore: false})),
},
};
test('should render loading screen while loading posts', () => {
const props = {
...baseProps,
postListIds: null,
};
const wrapper = shallow(<PostList {...props}/>);
expect(wrapper.state('loadingFirstSetOfPosts')).toBe(true);
expect(wrapper.find(LoadingScreen).exists()).toBe(true);
wrapper.setState({loadingFirstSetOfPosts: false});
expect(wrapper.find(LoadingScreen).exists()).toBe(false);
});
describe('renderRow', () => {
const postListIds = ['a', 'b', 'c', 'd'];
test('should get previous item ID correctly for oldest row', () => {
const wrapper = shallow(<PostList {...baseProps}/>);
const row = shallow(wrapper.instance().renderRow({
data: postListIds,
itemId: 'd',
}));
expect(row.find(PostListRow).prop('previousListId')).toEqual('');
});
test('should get previous item ID correctly for other rows', () => {
const wrapper = shallow(<PostList {...baseProps}/>);
const row = shallow(wrapper.instance().renderRow({
data: postListIds,
itemId: 'b',
}));
expect(row.find(PostListRow).prop('previousListId')).toEqual('c');
});
test('should highlight the focused post', () => {
const props = {
...baseProps,
focusedPostId: 'b',
};
const wrapper = shallow(<PostList {...props}/>);
let row = shallow(wrapper.instance().renderRow({
data: postListIds,
itemId: 'c',
}));
expect(row.find(PostListRow).prop('shouldHighlight')).toEqual(false);
row = shallow(wrapper.instance().renderRow({
data: postListIds,
itemId: 'b',
}));
expect(row.find(PostListRow).prop('shouldHighlight')).toEqual(true);
});
});
describe('new messages below', () => {
test('should mount outside of permalink view', () => {
const wrapper = shallow(<PostList {...baseProps}/>);
wrapper.setState({loadingFirstSetOfPosts: false});
expect(wrapper.find(NewMessagesBelow).exists()).toBe(true);
});
test('should not mount when in permalink view', () => {
const props = {
...baseProps,
focusedPostId: '1234',
};
const wrapper = shallow(<PostList {...props}/>);
wrapper.setState({loadingFirstSetOfPosts: false});
expect(wrapper.find(NewMessagesBelow).exists()).toBe(false);
});
});
describe('initScrollToIndex', () => {
test('should return index of start of new messages and call increasePostVisibility when all posts are unread', () => {
baseProps.actions.increasePostVisibility.mockResolvedValue({moreToLoad: false});
const postListIds = [];
for (let i = 0; i < 30; i++) {
postListIds.push(`post${i}`);
}
postListIds.push(PostListRowListIds.START_OF_NEW_MESSAGES);
const props = {
...baseProps,
postListIds,
};
const wrapper = shallow(<PostList {...props}/>);
const initScrollToIndex = wrapper.instance().initScrollToIndex();
expect(initScrollToIndex).toEqual({index: 31, position: 'start'});
expect(baseProps.actions.increasePostVisibility).toHaveBeenCalledTimes(1);
});
});
describe('loadMorePosts', () => {
test('should set state.atEnd to true after loading all posts in the channel', async () => {
baseProps.actions.increasePostVisibility.mockResolvedValue({moreToLoad: false});
const wrapper = shallow(<PostList {...baseProps}/>);
await wrapper.instance().loadMorePosts();
expect(baseProps.actions.increasePostVisibility).toHaveBeenCalledTimes(1);
expect(baseProps.actions.increasePostVisibility).toHaveBeenCalledWith(baseProps.channel.id, 'post3');
wrapper.update();
expect(wrapper.state('atEnd')).toEqual(true);
});
});
describe('onItemsRendered', () => {
test('should set state atBottom false when visibleStopIndex is not 0', async () => {
const wrapper = shallow(<PostList {...baseProps}/>);
wrapper.setState({atBottom: true});
wrapper.instance().onItemsRendered({visibleStartIndex: 4, visibleStopIndex: 1});
expect(wrapper.state('atBottom')).toEqual(false);
});
});
});