Skip to content

Commit

Permalink
Rollback unit test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianogorza committed May 29, 2024
1 parent 60f1182 commit e28cef6
Showing 1 changed file with 109 additions and 110 deletions.
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');
});
});
})

0 comments on commit e28cef6

Please sign in to comment.