-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Showing
5 changed files
with
324 additions
and
5 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/plugins/charts/public/services/active_cursor/active_cursor.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 |
---|---|---|
@@ -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, | ||
} | ||
`); | ||
}); | ||
}); |
141 changes: 141 additions & 0 deletions
141
src/plugins/charts/public/services/active_cursor/active_cursor_utils.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 |
---|---|---|
@@ -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, | ||
} | ||
`); | ||
}); | ||
}); | ||
}); | ||
}); |
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
140 changes: 140 additions & 0 deletions
140
src/plugins/charts/public/services/active_cursor/use_active_cursor.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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.