-
Notifications
You must be signed in to change notification settings - Fork 11.6k
/
mockHelpers.ts
116 lines (102 loc) · 3.82 KB
/
mockHelpers.ts
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
import { setBackendSrv } from '@grafana/runtime';
import { API_ROOT, GCOM_API_ROOT } from '../constants';
import {
CatalogPlugin,
LocalPlugin,
RemotePlugin,
Version,
ReducerState,
RequestStatus,
PluginListDisplayMode,
} from '../types';
import * as permissions from '../permissions';
import remotePluginMock from './remotePlugin.mock';
import localPluginMock from './localPlugin.mock';
import catalogPluginMock from './catalogPlugin.mock';
// Returns a sample mock for a CatalogPlugin plugin with the possibility to extend it
export const getCatalogPluginMock = (overrides?: Partial<CatalogPlugin>) => ({ ...catalogPluginMock, ...overrides });
// Returns a sample mock for a local (installed) plugin with the possibility to extend it
export const getLocalPluginMock = (overrides?: Partial<LocalPlugin>) => ({ ...localPluginMock, ...overrides });
// Returns a sample mock for a remote plugin with the possibility to extend it
export const getRemotePluginMock = (overrides?: Partial<RemotePlugin>) => ({ ...remotePluginMock, ...overrides });
// Returns a mock for the Redux store state of plugins
export const getPluginsStateMock = (plugins: CatalogPlugin[] = []): ReducerState => ({
// @ts-ignore - We don't need the rest of the properties here as we are using the "new" reducer (public/app/features/plugins/admin/state/reducer.ts)
items: {
ids: plugins.map(({ id }) => id),
entities: plugins.reduce((prev, current) => ({ ...prev, [current.id]: current }), {}),
},
requests: {
'plugins/fetchAll': {
status: RequestStatus.Fulfilled,
},
'plugins/fetchDetails': {
status: RequestStatus.Fulfilled,
},
},
settings: {
displayMode: PluginListDisplayMode.Grid,
},
// Backward compatibility
plugins: [],
errors: [],
searchQuery: '',
hasFetched: false,
dashboards: [],
isLoadingPluginDashboards: false,
panels: {},
});
// Mocks a plugin by considering what needs to be mocked from GCOM and what needs to be mocked locally (local Grafana API)
export const mockPluginApis = ({
remote: remoteOverride,
local: localOverride,
versions,
}: {
remote?: Partial<RemotePlugin>;
local?: Partial<LocalPlugin>;
versions?: Version[];
}) => {
const remote = getRemotePluginMock(remoteOverride);
const local = getLocalPluginMock(localOverride);
const original = jest.requireActual('@grafana/runtime');
const originalBackendSrv = original.getBackendSrv();
setBackendSrv({
...originalBackendSrv,
get: (path: string) => {
// Mock GCOM plugins (remote) if necessary
if (remote && path === `${GCOM_API_ROOT}/plugins`) {
return Promise.resolve({ items: [remote] });
}
// Mock GCOM single plugin page (remote) if necessary
if (remote && path === `${GCOM_API_ROOT}/plugins/${remote.slug}`) {
return Promise.resolve(remote);
}
// Mock versions
if (versions && path === `${GCOM_API_ROOT}/plugins/${remote.slug}/versions`) {
return Promise.resolve({ items: versions });
}
// Mock local plugin settings (installed) if necessary
if (local && path === `${API_ROOT}/${local.id}/settings`) {
return Promise.resolve(local);
}
// Mock local plugin listing (of necessary)
if (local && path === API_ROOT) {
return Promise.resolve([local]);
}
// Fall back to the original .get() in other cases
return originalBackendSrv.get(path);
},
});
};
type UserAccessTestContext = {
isAdmin: boolean;
isOrgAdmin: boolean;
isDataSourceEditor: boolean;
};
jest.mock('../permissions');
export function mockUserPermissions(options: UserAccessTestContext): void {
const mock = jest.mocked(permissions);
mock.isDataSourceEditor.mockReturnValue(options.isDataSourceEditor);
mock.isOrgAdmin.mockReturnValue(options.isOrgAdmin);
mock.isGrafanaAdmin.mockReturnValue(options.isAdmin);
}