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
/
delete_post_modal.test.jsx
129 lines (108 loc) · 3.96 KB
/
delete_post_modal.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {shallow} from 'enzyme';
import {Modal} from 'react-bootstrap';
import {browserHistory} from 'utils/browser_history';
import DeletePostModal from 'components/delete_post_modal/delete_post_modal.jsx';
describe('components/delete_post_modal', () => {
const post = {
id: '123',
message: 'test',
channel_id: '5',
type: '',
};
const baseProps = {
post,
commentCount: 0,
isRHS: false,
actions: {
deleteAndRemovePost: jest.fn(),
},
onHide: jest.fn(),
};
test('should match snapshot for delete_post_modal with 0 comments', () => {
const wrapper = shallow(
<DeletePostModal {...baseProps}/>
);
expect(wrapper).toMatchSnapshot();
});
test('should match snapshot for delete_post_modal with 1 comment', () => {
const commentCount = 1;
const props = {...baseProps, commentCount};
const wrapper = shallow(
<DeletePostModal {...props}/>
);
expect(wrapper).toMatchSnapshot();
});
test('should focus delete button on enter', () => {
const wrapper = shallow(
<DeletePostModal {...baseProps}/>
);
const deletePostBtn = {focus: jest.fn()};
wrapper.instance().deletePostBtn = deletePostBtn;
wrapper.instance().handleEntered();
expect(deletePostBtn.focus).toHaveBeenCalled();
});
test('should match state when onHide is called', () => {
const wrapper = shallow(
<DeletePostModal {...baseProps}/>
);
wrapper.setState({show: true});
wrapper.instance().onHide();
expect(wrapper.state('show')).toEqual(false);
});
test('should match state when the cancel button is clicked', () => {
const wrapper = shallow(
<DeletePostModal {...baseProps}/>
);
wrapper.setState({show: true});
wrapper.find('button').at(0).simulate('click');
expect(wrapper.state('show')).toEqual(false);
});
test('should have called actions.deleteAndRemovePost when handleDelete is called', async () => {
browserHistory.push = jest.fn();
const deleteAndRemovePost = jest.fn().mockReturnValueOnce({data: true});
const props = {
...baseProps,
actions: {
deleteAndRemovePost,
},
};
const wrapper = shallow(
<DeletePostModal {...props}/>
);
wrapper.setState({show: true});
wrapper.instance().handleDelete();
await expect(deleteAndRemovePost).toHaveBeenCalledTimes(1);
expect(deleteAndRemovePost).toHaveBeenCalledWith(props.post);
expect(wrapper.state('show')).toEqual(false);
});
test('should call browserHistory.push on handleDelete with post.id === focusedPostId && channelName', async () => {
browserHistory.push = jest.fn();
const props = {
...baseProps,
focusedPostId: '123',
channelName: 'channel_name',
teamName: 'team_name',
actions: {
deleteAndRemovePost: jest.fn().mockReturnValueOnce({data: true}),
},
};
const wrapper = shallow(
<DeletePostModal {...props}/>
);
await wrapper.instance().handleDelete();
expect(browserHistory.push).toHaveBeenCalledTimes(1);
expect(browserHistory.push).toHaveBeenCalledWith('/team_name/channels/channel_name');
});
test('should have called props.onHide when Modal.onExited is called', () => {
const onHide = jest.fn();
const props = {...baseProps, onHide};
const wrapper = shallow(
<DeletePostModal {...props}/>
);
wrapper.find(Modal).props().onExited();
expect(onHide).toHaveBeenCalledTimes(1);
});
});