-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60f1182
commit e28cef6
Showing
1 changed file
with
109 additions
and
110 deletions.
There are no files selected for viewing
219 changes: 109 additions & 110 deletions
219
plugins/main/public/components/common/data-source/hooks/use-data-source.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<tParsedIndexPattern> { | ||
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'); | ||
}); | ||
}); | ||
}) |