Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Aug 2, 2021
1 parent cbc53b5 commit ba8f4df
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { ActiveCursor } from './active_cursor';

describe('ActiveCursor', () => {
let activeCursor: ActiveCursor;

beforeEach(() => {
activeCursor = new ActiveCursor();
});

test('should initialize activeCursor$ stream on setup hook', () => {
expect(activeCursor.activeCursor$).toBeUndefined();

activeCursor.setup();

expect(activeCursor.activeCursor$).toMatchInlineSnapshot(`
Subject {
"_isScalar": false,
"closed": false,
"hasError": false,
"isStopped": false,
"observers": Array [],
"thrownError": null,
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { parseSyncOptions } from './active_cursor_utils';
import { Datatable } from '../../../../expressions';

describe('active_cursor_utils', () => {
describe('parseSyncOptions', () => {
describe('dateHistogramSyncOption', () => {
test('should return isDateHistogram true in case if that mode is active', () => {
expect(parseSyncOptions({ isDateHistogram: true })).toMatchInlineSnapshot(`
Object {
"isDateHistogram": true,
}
`);
});

test('should return isDateHistogram false for other cases', () => {
expect(parseSyncOptions({ datatables: [] as Datatable[] })).toMatchInlineSnapshot(`
Object {
"accessors": Array [],
"isDateHistogram": false,
}
`);
});
});

describe('datatablesSyncOption', () => {
test('should extract accessors', () => {
expect(
parseSyncOptions({
datatables: ([
{
columns: [
{
meta: {
index: 'foo_index',
field: 'foo_field',
},
},
],
},
] as unknown) as Datatable[],
}).accessors
).toMatchInlineSnapshot(`
Array [
"foo_index:foo_field",
]
`);
});

test('should return isDateHistogram true in case all datatables is time based', () => {
expect(
parseSyncOptions({
datatables: ([
{
columns: [
{
meta: {
index: 'foo_index',
field: 'foo_field',
sourceParams: {
appliedTimeRange: {},
},
},
},
],
},
{
columns: [
{
meta: {
index: 'foo_index1',
field: 'foo_field1',
sourceParams: {
appliedTimeRange: {},
},
},
},
],
},
] as unknown) as Datatable[],
})
).toMatchInlineSnapshot(`
Object {
"accessors": Array [
"foo_index:foo_field",
"foo_index1:foo_field1",
],
"isDateHistogram": true,
}
`);
});

test('should return isDateHistogram false in case of not all datatables is time based', () => {
expect(
parseSyncOptions({
datatables: ([
{
columns: [
{
meta: {
index: 'foo_index',
field: 'foo_field',
sourceParams: {
appliedTimeRange: {},
},
},
},
],
},
{
columns: [
{
meta: {
index: 'foo_index1',
field: 'foo_field1',
},
},
],
},
] as unknown) as Datatable[],
})
).toMatchInlineSnapshot(`
Object {
"accessors": Array [
"foo_index:foo_field",
"foo_index1:foo_field1",
],
"isDateHistogram": false,
}
`);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { uniq } from 'lodash';

import type { Datatable } from '../../../../expressions';
import type { ActiveCursorSyncOption, DateHistogramSyncOption } from './types';
import { ActiveCursorPayload } from './types';

function isDateHistogramSyncOption(
syncOption?: ActiveCursorSyncOption
Expand All @@ -17,9 +18,11 @@ function isDateHistogramSyncOption(
}

const parseDatatable = (dataTables: Datatable[]) => {
const isDateHistogram = dataTables.every((dataTable) =>
dataTable.columns.find((c) => Boolean(c.meta.sourceParams?.appliedTimeRange))
);
const isDateHistogram =
Boolean(dataTables.length) &&
dataTables.every((dataTable) =>
dataTable.columns.find((c) => Boolean(c.meta.sourceParams?.appliedTimeRange))
);

const accessors = uniq(
dataTables
Expand All @@ -36,10 +39,11 @@ const parseDatatable = (dataTables: Datatable[]) => {
};

/** @internal **/
export const parseSyncOptions = (syncOptions: ActiveCursorSyncOption) =>
export const parseSyncOptions = (
syncOptions: ActiveCursorSyncOption
): Partial<ActiveCursorPayload> =>
isDateHistogramSyncOption(syncOptions)
? {
isDateHistogram: syncOptions.isDateHistogram,
accessors: undefined,
}
: parseDatatable(syncOptions.datatables);
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { renderHook } from '@testing-library/react-hooks';
import type { RefObject } from 'react';

import { ActiveCursor } from './active_cursor';
import { useActiveCursor } from './use_active_cursor';

import type { ActiveCursorSyncOption, ActiveCursorPayload } from './types';
import type { Chart, PointerEvent } from '@elastic/charts';
import { Datatable } from '../../../../expressions';

describe('useActiveCursor', () => {
let cursor: ActiveCursorPayload['cursor'];
let dispatchExternalPointerEvent: jest.Mock;

const act = (syncOption: ActiveCursorSyncOption, events: Array<Partial<ActiveCursorPayload>>) =>
new Promise((resolve) => {
const activeCursor = new ActiveCursor();

activeCursor.setup();

dispatchExternalPointerEvent.mockImplementation((pointerEvent) => {
resolve(pointerEvent);
});

renderHook(() =>
useActiveCursor(
activeCursor,
{
current: {
dispatchExternalPointerEvent: dispatchExternalPointerEvent as (
pointerEvent: PointerEvent
) => void,
},
} as RefObject<Chart>,
syncOption
)
);

events.forEach((event) => {
activeCursor.activeCursor$!.next({ cursor, ...event });
});
});

beforeEach(() => {
cursor = {} as ActiveCursorPayload['cursor'];
dispatchExternalPointerEvent = jest.fn();
});

test('should trigger cursor pointer update (chart type: time, event type: time)', async () => {
await act({ isDateHistogram: true }, [{ isDateHistogram: true }]);

expect(dispatchExternalPointerEvent).toHaveBeenCalledTimes(1);
});

test('should trigger cursor pointer update (chart type: datatable - time based, event type: time)', async () => {
await act(
{
datatables: ([
{
columns: [
{
meta: {
index: 'foo_index',
field: 'foo_field',
sourceParams: {
appliedTimeRange: {},
},
},
},
],
},
] as unknown) as Datatable[],
},
[{ isDateHistogram: true }, { accessors: ['foo_index:foo_field'] }]
);

expect(dispatchExternalPointerEvent).toHaveBeenCalledTimes(2);
});

test('should not trigger cursor pointer update (chart type: datatable, event type: time)', async () => {
await act(
{
datatables: [
{
columns: [
{
meta: {
index: 'foo_index',
field: 'foo_field',
},
},
],
},
] as Datatable[],
},
[{ isDateHistogram: true }, { accessors: ['foo_index:foo_field'] }]
);

expect(dispatchExternalPointerEvent).toHaveBeenCalledTimes(1);
});

test('should works with multi datatables (intersection)', async () => {
await act(
{
datatables: [
{
columns: [
{
meta: {
index: 'ia',
field: 'fa',
},
},
],
},
{
columns: [
{
meta: {
index: 'ib',
field: 'fb',
},
},
],
},
] as Datatable[],
},
[{ accessors: ['foo_index:foo_field', 'ib:fb'] }]
);

expect(dispatchExternalPointerEvent).toHaveBeenCalledTimes(1);
});
});
Binary file modified test/functional/screenshots/baseline/tsvb_dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ba8f4df

Please sign in to comment.