mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[embeddable] add should_fetch unit test (#161596)
While messing around with time slider control and custom time ranges, I noticed shouldFetch$ is triggered (turns out search session id is changing - maybe a separate issue). During investigation, I found it difficult to explore without unit test for shouldFetch$. --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
9ab807796c
commit
a92432869f
1 changed files with 65 additions and 0 deletions
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* 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 { BehaviorSubject, Subscription } from 'rxjs';
|
||||
import type { FilterableEmbeddableInput } from './types';
|
||||
import { shouldFetch$ } from './should_fetch';
|
||||
|
||||
describe('shouldFetch$', () => {
|
||||
let shouldFetchCount = 0;
|
||||
let subscription: Subscription;
|
||||
let updateInput: (inputFragment: Partial<FilterableEmbeddableInput>) => void;
|
||||
beforeAll(() => {
|
||||
let input: FilterableEmbeddableInput = {
|
||||
id: '1',
|
||||
timeRange: {
|
||||
to: 'now',
|
||||
from: 'now-15m',
|
||||
},
|
||||
};
|
||||
const subject = new BehaviorSubject(input);
|
||||
updateInput = (inputFragment: Partial<FilterableEmbeddableInput>) => {
|
||||
input = {
|
||||
...input,
|
||||
...inputFragment,
|
||||
};
|
||||
subject.next(input);
|
||||
};
|
||||
|
||||
subscription = shouldFetch$<FilterableEmbeddableInput>(subject, () => {
|
||||
return input;
|
||||
}).subscribe(() => {
|
||||
shouldFetchCount++;
|
||||
});
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
subscription.unsubscribe();
|
||||
});
|
||||
|
||||
test('should not fire on initial subscription', () => {
|
||||
expect(shouldFetchCount).toBe(0);
|
||||
});
|
||||
|
||||
test('should not fire when there are no changes', () => {
|
||||
const initialCount = shouldFetchCount;
|
||||
updateInput({});
|
||||
expect(shouldFetchCount).toBe(initialCount);
|
||||
});
|
||||
|
||||
test('should fire when timeRange changes', () => {
|
||||
const initialCount = shouldFetchCount;
|
||||
updateInput({
|
||||
timeRange: {
|
||||
to: 'now',
|
||||
from: 'now-25m',
|
||||
},
|
||||
});
|
||||
expect(shouldFetchCount).toBe(initialCount + 1);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue