forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apps_form_component.test.tsx
227 lines (200 loc) · 6.69 KB
/
apps_form_component.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {Provider} from 'react-redux';
import {shallow} from 'enzyme';
import configureStore from 'redux-mock-store';
import {Modal} from 'react-bootstrap';
import {mountWithIntl} from 'tests/helpers/intl-test-helper';
import {AppCallResponseTypes} from 'mattermost-redux/constants/apps';
import Markdown from 'components/markdown';
import {AppsForm, Props} from './apps_form_component';
describe('AppsFormComponent', () => {
const baseProps: Props = {
intl: {} as any,
onExited: jest.fn(),
isEmbedded: false,
actions: {
performLookupCall: jest.fn(),
refreshOnSelect: jest.fn(),
submit: jest.fn().mockResolvedValue({
data: {
type: 'ok',
},
}),
},
form: {
title: 'Title',
footer: 'Footer',
header: 'Header',
icon: 'Icon',
call: {
path: '/create',
},
fields: [
{
name: 'bool1',
type: 'bool',
},
{
name: 'bool2',
type: 'bool',
value: false,
},
{
name: 'bool3',
type: 'bool',
value: true,
},
{
name: 'text1',
type: 'text',
value: 'initial text',
},
{
name: 'select1',
type: 'static_select',
options: [
{label: 'Label1', value: 'Value1'},
{label: 'Label2', value: 'Value2'},
],
value: {label: 'Label1', value: 'Value1'},
},
],
},
};
test('should set match snapshot', () => {
const wrapper = shallow<AppsForm>(
<AppsForm
{...baseProps}
/>,
);
expect(wrapper).toMatchSnapshot();
});
test('should set initial form values', () => {
const wrapper = shallow<AppsForm>(
<AppsForm
{...baseProps}
/>,
);
expect(wrapper.state().values).toEqual({
bool1: false,
bool2: false,
bool3: true,
text1: 'initial text',
select1: {label: 'Label1', value: 'Value1'},
});
});
test('it should submit and close the modal', async () => {
const submit = jest.fn().mockResolvedValue({data: {type: 'ok'}});
const props: Props = {
...baseProps,
actions: {
...baseProps.actions,
submit,
},
};
const wrapper = shallow<AppsForm>(
<AppsForm
{...props}
/>,
);
const hide = jest.fn();
wrapper.instance().handleHide = hide;
await wrapper.instance().handleSubmit({preventDefault: jest.fn()} as any);
expect(submit).toHaveBeenCalledWith({
values: {
bool1: false,
bool2: false,
bool3: true,
text1: 'initial text',
select1: {label: 'Label1', value: 'Value1'},
},
});
expect(hide).toHaveBeenCalled();
});
describe('generic error message', () => {
test('should appear when submit returns an error', async () => {
const props = {
...baseProps,
actions: {
...baseProps.actions,
submit: jest.fn().mockResolvedValue({
error: {error: 'This is an error.', type: AppCallResponseTypes.ERROR},
}),
},
};
const wrapper = shallow<AppsForm>(<AppsForm {...props}/>);
await wrapper.instance().handleSubmit({preventDefault: jest.fn()} as any);
const expected = (
<div className='error-text'>
<Markdown message={'This is an error.'}/>
</div>
);
expect(wrapper.find(Modal.Footer).containsMatchingElement(expected)).toBe(true);
});
test('should not appear when submit does not return an error', async () => {
const wrapper = shallow<AppsForm>(<AppsForm {...baseProps}/>);
await wrapper.instance().handleSubmit({preventDefault: jest.fn()} as any);
expect(wrapper.find(Modal.Footer).find('.error-text').exists()).toBeFalsy();
});
});
describe('default select element', () => {
const mockStore = configureStore();
test('should be enabled by default', () => {
const selectField = {
type: 'static_select',
value: {label: 'Option3', value: 'opt3'},
modal_label: 'Option Selector',
name: 'someoptionselector',
is_required: true,
options: [
{label: 'Option1', value: 'opt1'},
{label: 'Option2', value: 'opt2'},
{label: 'Option3', value: 'opt3'},
],
min_length: 2,
max_length: 1024,
hint: '',
subtype: '',
description: '',
};
const fields = [selectField];
const props = {
...baseProps,
call: {},
form: {
fields,
},
};
const state = {
entities: {
general: {
config: {},
},
channels: {},
teams: {
teams: {},
},
users: {
profiles: {},
},
groups: {
myGroups: [],
},
emojis: {},
preferences: {
myPreferences: {},
},
},
};
const store = mockStore(state);
const wrapper = mountWithIntl(
<Provider store={store}>
<AppsForm {...props}/>
</Provider>,
);
expect(wrapper.find(Modal.Body).find('.react-select__single-value').text()).toEqual('Option3');
});
});
});