forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compatibility_shim.test.js
172 lines (133 loc) · 6.04 KB
/
compatibility_shim.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
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { once } from 'lodash';
import { compatibilityShimFactory } from './compatibility_shim';
const createMockServer = () => {
return {
expose: jest.fn(), //fool once_per_server
log: jest.fn()
};
};
const createMockRequest = () => {
return {
getSavedObjectsClient: once(function () {
return {
get: jest.fn()
};
})
};
};
test(`passes title through if provided`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const title = 'test title';
const createJobMock = jest.fn();
await compatibilityShim(createJobMock)({ title, relativeUrl: '/something' }, null, createMockRequest());
expect(createJobMock.mock.calls.length).toBe(1);
expect(createJobMock.mock.calls[0][0].title).toBe(title);
});
test(`gets the title from the savedObject`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const createJobMock = jest.fn();
const mockRequest = createMockRequest();
const title = 'savedTitle';
mockRequest.getSavedObjectsClient().get.mockReturnValue({
attributes: {
title
}
});
await compatibilityShim(createJobMock)({ objectType: 'search', savedObjectId: 'abc' }, null, mockRequest);
expect(createJobMock.mock.calls.length).toBe(1);
expect(createJobMock.mock.calls[0][0].title).toBe(title);
});
test(`passes the objectType and savedObjectId to the savedObjectsClient`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const createJobMock = jest.fn();
const mockRequest = createMockRequest();
mockRequest.getSavedObjectsClient().get.mockReturnValue({
attributes: {
title: ''
}
});
const objectType = 'search';
const savedObjectId = 'abc';
await compatibilityShim(createJobMock)({ objectType, savedObjectId, }, null, mockRequest);
const getMock = mockRequest.getSavedObjectsClient().get.mock;
expect(getMock.calls.length).toBe(1);
expect(getMock.calls[0][0]).toBe(objectType);
expect(getMock.calls[0][1]).toBe(savedObjectId);
});
test(`logs deprecations when generating the title/relativeUrl using the savedObject`, async () => {
const mockServer = createMockServer();
const compatibilityShim = compatibilityShimFactory(mockServer);
const createJobMock = jest.fn();
const mockRequest = createMockRequest();
mockRequest.getSavedObjectsClient().get.mockReturnValue({
attributes: {
title: ''
}
});
await compatibilityShim(createJobMock)({ objectType: 'search', savedObjectId: 'abc' }, null, mockRequest);
expect(mockServer.log.mock.calls.length).toBe(2);
expect(mockServer.log.mock.calls[0][0]).toEqual(['warning', 'reporting', 'deprecation']);
expect(mockServer.log.mock.calls[1][0]).toEqual(['warning', 'reporting', 'deprecation']);
});
test(`passes objectType through`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const createJobMock = jest.fn();
const mockRequest = createMockRequest();
const objectType = 'foo';
await compatibilityShim(createJobMock)({ title: 'test', relativeUrl: '/something', objectType }, null, mockRequest);
expect(createJobMock.mock.calls.length).toBe(1);
expect(createJobMock.mock.calls[0][0].objectType).toBe(objectType);
});
test(`passes the relativeUrls through`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const createJobMock = jest.fn();
const relativeUrls = ['/app/kibana#something', '/app/kibana#something-else'];
await compatibilityShim(createJobMock)({ title: 'test', relativeUrls }, null, null);
expect(createJobMock.mock.calls.length).toBe(1);
expect(createJobMock.mock.calls[0][0].relativeUrls).toBe(relativeUrls);
});
const testSavedObjectRelativeUrl = (objectType, expectedUrl) => {
test(`generates the saved object relativeUrl for ${objectType}`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const createJobMock = jest.fn();
await compatibilityShim(createJobMock)({ title: 'test', objectType, savedObjectId: 'abc', }, null, null);
expect(createJobMock.mock.calls.length).toBe(1);
expect(createJobMock.mock.calls[0][0].relativeUrls).toEqual([expectedUrl]);
});
};
testSavedObjectRelativeUrl('search', '/app/kibana#/discover/abc?');
testSavedObjectRelativeUrl('visualization', '/app/kibana#/visualize/edit/abc?');
testSavedObjectRelativeUrl('dashboard', '/app/kibana#/dashboard/abc?');
test(`appends the queryString to the relativeUrl when generating from the savedObject`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const createJobMock = jest.fn();
await compatibilityShim(createJobMock)({ title: 'test', objectType: 'search', savedObjectId: 'abc', queryString: 'foo=bar' }, null, null);
expect(createJobMock.mock.calls.length).toBe(1);
expect(createJobMock.mock.calls[0][0].relativeUrls).toEqual(['/app/kibana#/discover/abc?foo=bar']);
});
test(`throw an Error if the objectType, savedObjectId and relativeUrls are provided`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const createJobMock = jest.fn();
const promise = compatibilityShim(createJobMock)({
title: 'test',
objectType: 'something',
relativeUrls: ['/something'],
savedObjectId: 'abc',
}, null, null);
await expect(promise).rejects.toBeDefined();
});
test(`passes headers and request through`, async () => {
const compatibilityShim = compatibilityShimFactory(createMockServer());
const createJobMock = jest.fn();
const headers = {};
const request = createMockRequest();
await compatibilityShim(createJobMock)({ title: 'test', relativeUrl: '/something' }, headers, request);
expect(createJobMock.mock.calls.length).toBe(1);
expect(createJobMock.mock.calls[0][1]).toBe(headers);
expect(createJobMock.mock.calls[0][2]).toBe(request);
});