grafana/public/app/features/plugins/admin/__mocks__/mockHelpers.ts
Jack Westbrook 3c3cf2eee9
Plugins Catalog: Install and show the latest compatible version of a plugin (#41003)
* fix(catalog): prefer rendering installed version over latest version

* feat(catalog): signify installed version in version history

* feat(catalog): introduce installedVersion and latestVersion

* refactor(catalog): use latestVersion for installation, simplify plugindetails header logic

* refactor(catalog): clean up installedVersion and latestVersion

* feat(catalog): use table-layout so versions list table has consistent column widths

* test(catalog): update failing tests

* removed the need of having a latest version in the plugin catalog type root level.

* fixed flaky test depending on what locale it was being running with.

* added missing test to verify version for a remote plugin.

* fixed version in header.

* preventing the UI from break if no versions are available.

* fixed failing test due to missing mock data.

* added todo as a reminder.

* refactor(catalog): prefer grafana plugin icons over gcom notfound images

* refactor(Plugins/Admin): change constant name

* refactor(Plugins/Admin): add comment to make condition easier to understand

* chore: update go modules

* feat(Backend/Plugins): add "dependencies" field to `PluginListItem`

* feat(Plugins/Admin): show the grafana dependency for the installed version

* refactor(Plugins/Admin): use the local version of links

* refactor(Plugins/Admin): prefer the local version for `.type`

* refactor(Plugins/ADmin): prefer the local `.description` field

* fix(Plugins/Admin): fix tests

* test(plugins/api): update the expected response for the `api/plugins` tests

* chore(Plugins/Admin): add todo comments to check preferation of remote/local values

* feat(backend/api): always send the grafana version as a header when proxying to GCOM

* feat(plugins/admin): use the `isCompatible` flag to get the latest compatible version

* feat(plugins/admin): show the latest compatible version in the versions list

* fix(plugins/admin): show the grafana dependency for the latest compatible version

* fix(plugins/admin): update the version list when installing/uninstalling a plugin

* test(plugins/admin): add some test-cases for the latest-compatible-version

* fix(plugins/admin): show the grafana dependency for the installed version (if installed)

* feat(plugins/backend): add the `dependencies.grafanaDependency` property to the plugin object

* test(plugins/backend): fix tests by adjusting expected response json

Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
2021-11-12 11:07:12 +01:00

118 lines
3.8 KiB
TypeScript

import { mocked } from 'ts-jest/utils';
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 = mocked(permissions);
mock.isDataSourceEditor.mockReturnValue(options.isDataSourceEditor);
mock.isOrgAdmin.mockReturnValue(options.isOrgAdmin);
mock.isGrafanaAdmin.mockReturnValue(options.isAdmin);
}