From e28cef6698d8cf46eee1b77fa6195145a73d818a Mon Sep 17 00:00:00 2001 From: Luciano Gorza Date: Wed, 29 May 2024 13:03:59 -0300 Subject: [PATCH] Rollback unit test changes --- .../data-source/hooks/use-data-source.test.ts | 219 +++++++++--------- 1 file changed, 109 insertions(+), 110 deletions(-) diff --git a/plugins/main/public/components/common/data-source/hooks/use-data-source.test.ts b/plugins/main/public/components/common/data-source/hooks/use-data-source.test.ts index 8286456c8c..ed02e32119 100644 --- a/plugins/main/public/components/common/data-source/hooks/use-data-source.test.ts +++ b/plugins/main/public/components/common/data-source/hooks/use-data-source.test.ts @@ -1,129 +1,128 @@ import { useDataSource } from './use-data-source'; import { renderHook } from '@testing-library/react-hooks'; -import { - tDataSourceRepository, - tFilter, - PatternDataSource, - tParsedIndexPattern, +import { + tDataSourceRepository, + tFilter, + PatternDataSource, + tParsedIndexPattern } from '../index'; -import { - IndexPatternsService, - IndexPattern, -} from '../../../../../../../src/plugins/data/common'; +import { IndexPatternsService, IndexPattern } from '../../../../../../../src/plugins/data/common'; jest.mock('../../../../kibana-services', () => ({ - ...(jest.requireActual('../../../../kibana-services') as object), - getDataPlugin: () => ({ - // mock indexPatterns getter - indexPatterns: { - get: jest.fn().mockResolvedValue({ - fields: { - replaceAll: jest.fn(), - map: jest.fn().mockReturnValue([]), + ...(jest.requireActual('../../../../kibana-services') as object), + getDataPlugin: () => ({ + // mock indexPatterns getter + indexPatterns: { + get: jest.fn().mockResolvedValue({ + fields: { + replaceAll: jest.fn(), + map: jest.fn().mockReturnValue([]), + }, + getScriptedFields: jest.fn().mockReturnValue([]), + }), + getFieldsForIndexPattern: jest.fn().mockResolvedValue([]), + updateSavedObject: jest.fn().mockResolvedValue({}), }, - getScriptedFields: jest.fn().mockReturnValue([]), - }), - getFieldsForIndexPattern: jest.fn().mockResolvedValue([]), - updateSavedObject: jest.fn().mockResolvedValue({}), - }, - query: { - filterManager: { - getFilters: jest.fn().mockReturnValue([]), - setFilters: jest.fn(), - getUpdates$: jest.fn().mockReturnValue({ - subscribe: jest.fn(), - }), - }, - }, - }), + query: { + filterManager: { + getFilters: jest.fn().mockReturnValue([]), + setFilters: jest.fn(), + getUpdates$: jest.fn().mockReturnValue({ + subscribe: jest.fn() + }) + } + } + }), })); const mockedGetFilters = jest.fn().mockReturnValue([]); class DataSourceMocked implements PatternDataSource { - constructor(public id: string, public title: string) { - this.id = id; - this.title = title; - } - fields: any[]; - patternService: IndexPatternsService; - indexPattern: IndexPattern; - defaultFixedFilters: tFilter[]; - filters: tFilter[]; - init = jest.fn(); - select = jest.fn(); - fetch = jest.fn(); - getFilters = mockedGetFilters; - setFilters = jest.fn(); - getFields = mockedGetFilters; - getFetchFilters = mockedGetFilters; - toJSON(): tParsedIndexPattern { - return { - id: this.id, - title: this.title, - } as tParsedIndexPattern; - } - getClusterManagerFilters = mockedGetFilters; - getPinnedAgentFilter = mockedGetFilters; - getExcludeManagerFilter = mockedGetFilters; - getAllowAgentsFilter = mockedGetFilters; + constructor(public id: string, public title: string) { + this.id = id; + this.title = title; + } + fields: any[]; + patternService: IndexPatternsService; + indexPattern: IndexPattern; + defaultFixedFilters: tFilter[]; + filters: tFilter[]; + init = jest.fn(); + select = jest.fn(); + fetch = jest.fn(); + getFilters = mockedGetFilters; + setFilters = jest.fn(); + getFields = mockedGetFilters + getFixedFilters = mockedGetFilters + getFetchFilters = mockedGetFilters + toJSON(): tParsedIndexPattern { + return { + id: this.id, + title: this.title, + } as tParsedIndexPattern; + } + getClusterManagerFilters = mockedGetFilters + getPinnedAgentFilter = mockedGetFilters + getExcludeManagerFilter = mockedGetFilters + getAllowAgentsFilter = mockedGetFilters } class ExampleRepository implements tDataSourceRepository { - getDefault = jest.fn(); - setDefault = jest.fn(); - get = jest.fn(); - getAll = jest.fn(); + getDefault = jest.fn(); + setDefault = jest.fn(); + get = jest.fn(); + getAll = jest.fn(); } describe('useDataSource hook', () => { - it('shoudl throw ERROR when the repository is not defined', () => { - try { - renderHook(() => - useDataSource({ - DataSource: DataSourceMocked, - repository: undefined as any, - }), - ); - } catch (error) { - expect(error).toBeDefined(); - expect(error.message).toBe('DataSource and repository are required'); - } - }); - it('should throw ERROR when the DataSource is not defined', () => { - try { - renderHook(() => - useDataSource({ - DataSource: undefined as any, - repository: new ExampleRepository(), - }), - ); - } catch (error) { - expect(error).toBeDefined(); - expect(error.message).toBe('DataSource and repository are required'); - } - }); + it('shoudl throw ERROR when the repository is not defined', () => { + + try { + renderHook(() => useDataSource({ + DataSource: DataSourceMocked, + repository: undefined as any + })); + } catch(error){ + expect(error).toBeDefined(); + expect(error.message).toBe('DataSource and repository are required'); + } + + }) + + it('should throw ERROR when the DataSource is not defined', () => { + + try { + renderHook(() => useDataSource({ + DataSource: undefined as any, + repository: new ExampleRepository() + })); + } catch(error){ + expect(error).toBeDefined(); + expect(error.message).toBe('DataSource and repository are required'); + } + + }) + + it('should initialize the hook with only receiving the dataSource and repository', async () => { + const repository = new ExampleRepository(); + const indexMocked = { + id: 'test', + title: 'Test' + } + jest.spyOn(repository, 'getAll').mockResolvedValueOnce([indexMocked]); + jest.spyOn(repository, 'getDefault').mockResolvedValueOnce(indexMocked); + const { result, waitForNextUpdate } = renderHook(() => useDataSource({ + DataSource: DataSourceMocked, + repository + })); + // wait for the promise to resolve + await waitForNextUpdate(); + expect(result.current.isLoading).toBeFalsy(); + expect(result.current.dataSource).toBeDefined(); + expect(result.current.dataSource?.id).toBe('test'); + expect(result.current.dataSource?.title).toBe('Test'); + }) + - it('should initialize the hook with only receiving the dataSource and repository', async () => { - const repository = new ExampleRepository(); - const indexMocked = { - id: 'test', - title: 'Test', - }; - jest.spyOn(repository, 'getAll').mockResolvedValueOnce([indexMocked]); - jest.spyOn(repository, 'getDefault').mockResolvedValueOnce(indexMocked); - const { result, waitForNextUpdate } = renderHook(() => - useDataSource({ - DataSource: DataSourceMocked, - repository, - }), - ); - // wait for the promise to resolve - await waitForNextUpdate(); - expect(result.current.isLoading).toBeFalsy(); - expect(result.current.dataSource).toBeDefined(); - expect(result.current.dataSource?.id).toBe('test'); - expect(result.current.dataSource?.title).toBe('Test'); - }); -}); +})