Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
angorayc committed Jul 24, 2023
1 parent df1000c commit 5b028b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ export const DEFAULT_TAGS_RESPONSE = [
},
];

export const DEFAULT_CREATE_TAGS_RESPONSE = [
{
id: MOCK_TAG_ID,
name: MOCK_TAG_NAME,
description: 'test tag description',
color: '#2c7b82',
},
];

export const getTagsByName = jest
.fn()
.mockImplementation(() => Promise.resolve(DEFAULT_TAGS_RESPONSE));
export const createTag = jest
.fn()
.mockImplementation(() => Promise.resolve(DEFAULT_TAGS_RESPONSE[0]));
.mockImplementation(() => Promise.resolve(DEFAULT_CREATE_TAGS_RESPONSE[0]));
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const getTagsByName = (
abortSignal?: AbortSignal
): Promise<Tag[]> => http.get(INTERNAL_TAGS_URL, { query: { name: tagName }, signal: abortSignal });

// Dashboard listing needs savedObjectsTaggingClient to work correctly with cache.
// https://github.com/elastic/kibana/issues/160723#issuecomment-1641904984
export const createTag = ({
savedObjectsTaggingClient,
tag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import {
import { useKibana } from '../../common/lib/kibana';
import { useFetchSecurityTags } from './use_fetch_security_tags';
import { DEFAULT_TAGS_RESPONSE } from '../../common/containers/tags/__mocks__/api';
import type { ITagsClient } from '@kbn/saved-objects-tagging-plugin/common';
import type { SavedObjectsTaggingApi } from '@kbn/saved-objects-tagging-oss-plugin/public';

jest.mock('../../common/lib/kibana');
jest.mock('../../../common/utils/get_ramdom_color', () => ({
getRandomColor: jest.fn().mockReturnValue('#FFFFFF'),
}));

const mockGet = jest.fn();
const mockPut = jest.fn();
const mockAbortSignal = {} as unknown as AbortSignal;

const mockCreateTag = jest.fn();
const renderUseCreateSecurityDashboardLink = () => renderHook(() => useFetchSecurityTags(), {});

const asyncRenderUseCreateSecurityDashboardLink = async () => {
Expand All @@ -34,8 +38,10 @@ const asyncRenderUseCreateSecurityDashboardLink = async () => {

describe('useFetchSecurityTags', () => {
beforeAll(() => {
useKibana().services.http = { get: mockGet, put: mockPut } as unknown as HttpStart;

useKibana().services.http = { get: mockGet } as unknown as HttpStart;
useKibana().services.savedObjectsTagging = {
client: { create: mockCreateTag } as unknown as ITagsClient,
} as unknown as SavedObjectsTaggingApi;
global.AbortController = jest.fn().mockReturnValue({
abort: jest.fn(),
signal: mockAbortSignal,
Expand All @@ -60,9 +66,10 @@ describe('useFetchSecurityTags', () => {
mockGet.mockResolvedValue([]);
await asyncRenderUseCreateSecurityDashboardLink();

expect(mockPut).toHaveBeenCalledWith(INTERNAL_TAGS_URL, {
body: JSON.stringify({ name: SECURITY_TAG_NAME, description: SECURITY_TAG_DESCRIPTION }),
signal: mockAbortSignal,
expect(mockCreateTag).toHaveBeenCalledWith({
name: SECURITY_TAG_NAME,
description: SECURITY_TAG_DESCRIPTION,
color: '#FFFFFF',
});
});

Expand All @@ -76,7 +83,7 @@ describe('useFetchSecurityTags', () => {
}));
const { result } = await asyncRenderUseCreateSecurityDashboardLink();

expect(mockPut).not.toHaveBeenCalled();
expect(mockCreateTag).not.toHaveBeenCalled();
expect(result.current.tags).toEqual(expect.objectContaining(expected));
});
});

0 comments on commit 5b028b0

Please sign in to comment.