Adds test case for use_get_user_cases_permissions module (#121808)

* Adds test case for use_get_user_cases_permissions module

* Extracts mocks

* Fixes import paths

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Claudio Procida 2021-12-28 12:05:39 +01:00 committed by GitHub
parent 4c07aba4a2
commit 3a141352f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 150 additions and 2 deletions

View file

@ -0,0 +1,79 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { renderHook } from '@testing-library/react-hooks';
import { applicationServiceMock } from 'src/core/public/mocks';
import { casesFeatureId } from '../../common';
import { useGetUserCasesPermissions } from './use_get_user_cases_permissions';
import { kibanaStartMock } from '../utils/kibana_react.mock';
let mockUseKibanaReturnValue = kibanaStartMock.startContract();
jest.mock('../utils/kibana_react', () => ({
__esModule: true,
useKibana: jest.fn(() => mockUseKibanaReturnValue),
}));
describe('useGetUserCasesPermissions', function () {
it('returns expected permissions when capabilities entry exists', () => {
mockUseKibanaReturnValue = {
...mockUseKibanaReturnValue,
services: {
...mockUseKibanaReturnValue.services,
application: {
...mockUseKibanaReturnValue.services.application,
capabilities: {
...applicationServiceMock.createStartContract().capabilities,
[casesFeatureId]: { crud_cases: false, read_cases: true },
},
},
},
};
const { result } = renderHook(() => useGetUserCasesPermissions(), {});
expect(result.current?.read).toBe(true);
expect(result.current?.crud).toBe(false);
});
it('returns false when capabilities entry permissions are missing', () => {
mockUseKibanaReturnValue = {
...mockUseKibanaReturnValue,
services: {
...mockUseKibanaReturnValue.services,
application: {
...mockUseKibanaReturnValue.services.application,
capabilities: {
...applicationServiceMock.createStartContract().capabilities,
[casesFeatureId]: {
/* intentionally empty */
},
},
},
},
};
const { result } = renderHook(() => useGetUserCasesPermissions(), {});
expect(result.current?.read).toBe(false);
expect(result.current?.crud).toBe(false);
});
it('returns false when capabilities entry is missing entirely', () => {
mockUseKibanaReturnValue = {
...mockUseKibanaReturnValue,
services: {
...mockUseKibanaReturnValue.services,
application: {
...mockUseKibanaReturnValue.services.application,
capabilities: {
...applicationServiceMock.createStartContract().capabilities,
},
},
},
};
const { result } = renderHook(() => useGetUserCasesPermissions(), {});
expect(result.current?.read).toBe(false);
expect(result.current?.crud).toBe(false);
});
});

View file

@ -20,11 +20,11 @@ export function useGetUserCasesPermissions() {
useEffect(() => {
const capabilitiesCanUserCRUD: boolean =
typeof uiCapabilities[casesFeatureId].crud_cases === 'boolean'
typeof uiCapabilities[casesFeatureId]?.crud_cases === 'boolean'
? (uiCapabilities[casesFeatureId].crud_cases as boolean)
: false;
const capabilitiesCanUserRead: boolean =
typeof uiCapabilities[casesFeatureId].read_cases === 'boolean'
typeof uiCapabilities[casesFeatureId]?.read_cases === 'boolean'
? (uiCapabilities[casesFeatureId].read_cases as boolean)
: false;
setCasesPermissions({

View file

@ -0,0 +1,46 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
const casesUiStartMock = {
createStart() {
return {
getCases: jest.fn(),
getAllCasesSelectorModal: jest.fn(),
getCreateCaseFlyout: jest.fn(),
getRecentCases: jest.fn(),
};
},
};
const embeddableStartMock = {
createStart() {
return {
getEmbeddableFactory: jest.fn(),
getEmbeddableFactories: jest.fn(),
EmbeddablePanel: jest.fn(),
getStateTransfer: jest.fn(),
getAttributeService: jest.fn(),
telemetry: null,
inject: jest.fn(),
extract: jest.fn(),
getAllMigrations: jest.fn(),
};
},
};
export const observabilityPublicPluginsStartMock = {
createStart() {
return {
cases: casesUiStartMock.createStart(),
embeddable: embeddableStartMock.createStart(),
triggersActionsUi: null,
data: null,
lens: null,
discover: null,
};
},
};

View file

@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { coreMock, notificationServiceMock, overlayServiceMock } from 'src/core/public/mocks';
import { observabilityPublicPluginsStartMock } from '../observability_public_plugins_start.mock';
export const kibanaStartMock = {
startContract() {
return {
notifications: notificationServiceMock.createStartContract(),
overlays: overlayServiceMock.createStartContract(),
services: {
...coreMock.createStart(),
...observabilityPublicPluginsStartMock.createStart(),
storage: coreMock.createStorage(),
},
};
},
};