[Exploratory view] Fixed bug in series storage hook (#105495)

This commit is contained in:
Shahzad 2021-07-13 23:31:34 +02:00 committed by GitHub
parent 5dc1c8f53a
commit 139326a06f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 132 additions and 1 deletions

View file

@ -0,0 +1,131 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { useEffect } from 'react';
import { UrlStorageContextProvider, useSeriesStorage } from './use_series_storage';
import { render } from '@testing-library/react';
const mockSingleSeries = {
'performance-distribution': {
reportType: 'data-distribution',
dataType: 'ux',
breakdown: 'user_agent.name',
time: { from: 'now-15m', to: 'now' },
},
};
const mockMultipleSeries = {
'performance-distribution': {
reportType: 'data-distribution',
dataType: 'ux',
breakdown: 'user_agent.name',
time: { from: 'now-15m', to: 'now' },
},
'kpi-over-time': {
reportType: 'kpi-over-time',
dataType: 'synthetics',
breakdown: 'user_agent.name',
time: { from: 'now-15m', to: 'now' },
},
};
describe('userSeries', function () {
function setupTestComponent(seriesData: any) {
const setData = jest.fn();
function TestComponent() {
const data = useSeriesStorage();
useEffect(() => {
setData(data);
}, [data]);
return <span>Test</span>;
}
render(
<UrlStorageContextProvider
storage={{ get: jest.fn().mockReturnValue(seriesData), set: jest.fn() }}
>
<TestComponent />
</UrlStorageContextProvider>
);
return setData;
}
it('should return expected result when there is one series', function () {
const setData = setupTestComponent(mockSingleSeries);
expect(setData).toHaveBeenCalledTimes(2);
expect(setData).toHaveBeenLastCalledWith(
expect.objectContaining({
allSeries: {
'performance-distribution': {
breakdown: 'user_agent.name',
dataType: 'ux',
reportType: 'data-distribution',
time: { from: 'now-15m', to: 'now' },
},
},
allSeriesIds: ['performance-distribution'],
firstSeries: {
breakdown: 'user_agent.name',
dataType: 'ux',
reportType: 'data-distribution',
time: { from: 'now-15m', to: 'now' },
},
firstSeriesId: 'performance-distribution',
})
);
});
it('should return expected result when there are multiple series series', function () {
const setData = setupTestComponent(mockMultipleSeries);
expect(setData).toHaveBeenCalledTimes(2);
expect(setData).toHaveBeenLastCalledWith(
expect.objectContaining({
allSeries: {
'performance-distribution': {
breakdown: 'user_agent.name',
dataType: 'ux',
reportType: 'data-distribution',
time: { from: 'now-15m', to: 'now' },
},
'kpi-over-time': {
reportType: 'kpi-over-time',
dataType: 'synthetics',
breakdown: 'user_agent.name',
time: { from: 'now-15m', to: 'now' },
},
},
allSeriesIds: ['performance-distribution', 'kpi-over-time'],
firstSeries: {
breakdown: 'user_agent.name',
dataType: 'ux',
reportType: 'data-distribution',
time: { from: 'now-15m', to: 'now' },
},
firstSeriesId: 'performance-distribution',
})
);
});
it('should return expected result when there are no series', function () {
const setData = setupTestComponent({});
expect(setData).toHaveBeenCalledTimes(2);
expect(setData).toHaveBeenLastCalledWith(
expect.objectContaining({
allSeries: {},
allSeriesIds: [],
firstSeries: undefined,
firstSeriesId: undefined,
})
);
});
});

View file

@ -67,7 +67,7 @@ export function UrlStorageContextProvider({
setAllSeries(allSeriesN);
setFirstSeriesId(allSeriesIds?.[0]);
setFirstSeries(allSeriesN?.[0]);
setFirstSeries(allSeriesN?.[allSeriesIds?.[0]]);
(storage as IKbnUrlStateStorage).set(allSeriesKey, allShortSeries);
}, [allShortSeries, storage]);