[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:
Nathan Reese 2023-07-11 08:58:39 -06:00 committed by GitHub
parent 9ab807796c
commit a92432869f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);
});
});