forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomize_panel_modal.test.tsx
181 lines (153 loc) · 6.76 KB
/
customize_panel_modal.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
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// @ts-ignore
import { findTestSubject } from '@elastic/eui/lib/test';
import * as React from 'react';
import { Container, isErrorEmbeddable } from '../lib';
import {
ContactCardEmbeddable,
ContactCardEmbeddableInput,
ContactCardEmbeddableOutput,
} from '../lib/test_samples/embeddables/contact_card/contact_card_embeddable';
import {
CONTACT_CARD_EMBEDDABLE,
ContactCardEmbeddableFactory,
} from '../lib/test_samples/embeddables/contact_card/contact_card_embeddable_factory';
import { HelloWorldContainer } from '../lib/test_samples/embeddables/hello_world_container';
// eslint-disable-next-line
import { coreMock } from '../../../../core/public/mocks';
import { testPlugin } from './test_plugin';
import { CustomizePanelModal } from '../lib/panel/panel_header/panel_actions/customize_title/customize_panel_modal';
import { mount } from 'enzyme';
import { EmbeddableStart } from '../plugin';
let api: EmbeddableStart;
let container: Container;
let embeddable: ContactCardEmbeddable;
beforeEach(async () => {
const { doStart, coreStart, uiActions, setup } = testPlugin(
coreMock.createSetup(),
coreMock.createStart()
);
const contactCardFactory = new ContactCardEmbeddableFactory(
uiActions.executeTriggerActions,
{} as any
);
setup.registerEmbeddableFactory(contactCardFactory.type, contactCardFactory);
api = doStart();
container = new HelloWorldContainer(
{ id: '123', panels: {} },
{
getActions: uiActions.getTriggerCompatibleActions,
getEmbeddableFactory: api.getEmbeddableFactory,
getAllEmbeddableFactories: api.getEmbeddableFactories,
overlays: coreStart.overlays,
notifications: coreStart.notifications,
application: coreStart.application,
inspector: {} as any,
SavedObjectFinder: () => null,
}
);
const contactCardEmbeddable = await container.addNewEmbeddable<
ContactCardEmbeddableInput,
ContactCardEmbeddableOutput,
ContactCardEmbeddable
>(CONTACT_CARD_EMBEDDABLE, {
firstName: 'Joe',
});
if (isErrorEmbeddable(contactCardEmbeddable)) {
throw new Error('Error creating new hello world embeddable');
} else {
embeddable = contactCardEmbeddable;
}
});
test('Is initialized with the embeddables title', async () => {
const component = mount(<CustomizePanelModal embeddable={embeddable} updateTitle={() => {}} />);
const inputField = findTestSubject(component, 'customEmbeddablePanelTitleInput').find('input');
expect(inputField.props().placeholder).toBe(embeddable.getOutput().title);
expect(inputField.props().placeholder).toBe(embeddable.getOutput().defaultTitle);
expect(inputField.props().value).toBe('');
});
test('Calls updateTitle with a new title', async () => {
const updateTitle = jest.fn();
const component = mount(
<CustomizePanelModal embeddable={embeddable} updateTitle={updateTitle} />
);
const inputField = findTestSubject(component, 'customEmbeddablePanelTitleInput').find('input');
const event = { target: { value: 'new title' } };
inputField.simulate('change', event);
findTestSubject(component, 'saveNewTitleButton').simulate('click');
expect(updateTitle).toBeCalledWith('new title');
});
test('Input value shows custom title if one given', async () => {
embeddable.updateInput({ title: 'new title' });
const updateTitle = jest.fn();
const component = mount(
<CustomizePanelModal embeddable={embeddable} updateTitle={updateTitle} />
);
const inputField = findTestSubject(component, 'customEmbeddablePanelTitleInput').find('input');
expect(inputField.props().value).toBe('new title');
findTestSubject(component, 'saveNewTitleButton').simulate('click');
expect(inputField.props().value).toBe('new title');
});
test('Reset updates the input with the default title when the embeddable has no title override', async () => {
const updateTitle = jest.fn();
embeddable.updateInput({ title: 'my custom title' });
const component = mount(
<CustomizePanelModal embeddable={embeddable} updateTitle={updateTitle} />
);
const inputField = findTestSubject(component, 'customEmbeddablePanelTitleInput').find('input');
const event = { target: { value: 'another custom title' } };
inputField.simulate('change', event);
findTestSubject(component, 'resetCustomEmbeddablePanelTitle').simulate('click');
expect(inputField.props().placeholder).toBe(embeddable.getOutput().defaultTitle);
});
test('Reset updates the input with the default title when the embeddable has a title override', async () => {
const updateTitle = jest.fn();
const component = mount(
<CustomizePanelModal embeddable={embeddable} updateTitle={updateTitle} />
);
const inputField = findTestSubject(component, 'customEmbeddablePanelTitleInput').find('input');
const event = { target: { value: 'new title' } };
inputField.simulate('change', event);
findTestSubject(component, 'resetCustomEmbeddablePanelTitle').simulate('click');
expect(inputField.props().placeholder).toBe(embeddable.getOutput().defaultTitle);
});
test('Reset calls updateTitle with undefined', async () => {
const updateTitle = jest.fn();
const component = mount(
<CustomizePanelModal embeddable={embeddable} updateTitle={updateTitle} />
);
const inputField = findTestSubject(component, 'customEmbeddablePanelTitleInput').find('input');
const event = { target: { value: 'new title' } };
inputField.simulate('change', event);
findTestSubject(component, 'resetCustomEmbeddablePanelTitle').simulate('click');
findTestSubject(component, 'saveNewTitleButton').simulate('click');
expect(updateTitle).toBeCalledWith(undefined);
});
test('Can set title to an empty string', async () => {
const updateTitle = jest.fn();
const component = mount(
<CustomizePanelModal embeddable={embeddable} updateTitle={updateTitle} />
);
const inputField = findTestSubject(component, 'customizePanelHideTitle');
inputField.simulate('click');
findTestSubject(component, 'saveNewTitleButton').simulate('click');
expect(inputField.props().value).toBeUndefined();
expect(updateTitle).toBeCalledWith('');
});