From 5b028b0b82d8a189aaec61e46c8bd9f13aa23799 Mon Sep 17 00:00:00 2001 From: Angela Chuang Date: Mon, 24 Jul 2023 11:00:02 +0100 Subject: [PATCH] unit test --- .../common/containers/tags/__mocks__/api.ts | 11 ++++++++- .../public/common/containers/tags/api.ts | 2 ++ .../use_fetch_security_tags.test.ts | 23 ++++++++++++------- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/security_solution/public/common/containers/tags/__mocks__/api.ts b/x-pack/plugins/security_solution/public/common/containers/tags/__mocks__/api.ts index fa08f28035b56..d7401ff19d916 100644 --- a/x-pack/plugins/security_solution/public/common/containers/tags/__mocks__/api.ts +++ b/x-pack/plugins/security_solution/public/common/containers/tags/__mocks__/api.ts @@ -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])); diff --git a/x-pack/plugins/security_solution/public/common/containers/tags/api.ts b/x-pack/plugins/security_solution/public/common/containers/tags/api.ts index d1a36160f4f52..479ae07cc4eb7 100644 --- a/x-pack/plugins/security_solution/public/common/containers/tags/api.ts +++ b/x-pack/plugins/security_solution/public/common/containers/tags/api.ts @@ -23,6 +23,8 @@ export const getTagsByName = ( abortSignal?: AbortSignal ): Promise => 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, diff --git a/x-pack/plugins/security_solution/public/dashboards/containers/use_fetch_security_tags.test.ts b/x-pack/plugins/security_solution/public/dashboards/containers/use_fetch_security_tags.test.ts index 24afcb96035a3..4cf79cf4ce10b 100644 --- a/x-pack/plugins/security_solution/public/dashboards/containers/use_fetch_security_tags.test.ts +++ b/x-pack/plugins/security_solution/public/dashboards/containers/use_fetch_security_tags.test.ts @@ -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 () => { @@ -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, @@ -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', }); }); @@ -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)); }); });