|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import { driverInstanceMock, storageInstanceMock } from './api.test.mocks'; |
| 10 | +import moment from 'moment'; |
| 11 | +import { httpServiceMock } from '../../../../core/public/mocks'; |
| 12 | +import { getApi } from './api'; |
| 13 | +import { TestScheduler } from 'rxjs/testing'; |
| 14 | +import { FetchResult, NewsfeedPluginBrowserConfig } from '../types'; |
| 15 | +import { take } from 'rxjs/operators'; |
| 16 | + |
| 17 | +const kibanaVersion = '8.0.0'; |
| 18 | +const newsfeedId = 'test'; |
| 19 | + |
| 20 | +const getTestScheduler = () => |
| 21 | + new TestScheduler((actual, expected) => { |
| 22 | + expect(actual).toEqual(expected); |
| 23 | + }); |
| 24 | + |
| 25 | +const createConfig = (mainInternal: number): NewsfeedPluginBrowserConfig => ({ |
| 26 | + mainInterval: moment.duration(mainInternal, 'ms'), |
| 27 | + fetchInterval: moment.duration(mainInternal, 'ms'), |
| 28 | + service: { |
| 29 | + urlRoot: 'urlRoot', |
| 30 | + pathTemplate: 'pathTemplate', |
| 31 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +const createFetchResult = (parts: Partial<FetchResult>): FetchResult => ({ |
| 35 | + feedItems: [], |
| 36 | + hasNew: false, |
| 37 | + error: null, |
| 38 | + kibanaVersion, |
| 39 | + ...parts, |
| 40 | +}); |
| 41 | + |
| 42 | +describe('getApi', () => { |
| 43 | + let http: ReturnType<typeof httpServiceMock.createSetupContract>; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + http = httpServiceMock.createSetupContract(); |
| 47 | + driverInstanceMock.shouldFetch.mockReturnValue(true); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + storageInstanceMock.isAnyUnread$.mockReset(); |
| 52 | + driverInstanceMock.fetchNewsfeedItems.mockReset(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('merges the newsfeed and unread observables', () => { |
| 56 | + getTestScheduler().run(({ expectObservable, cold }) => { |
| 57 | + storageInstanceMock.isAnyUnread$.mockImplementation(() => { |
| 58 | + return cold<boolean>('a|', { |
| 59 | + a: true, |
| 60 | + }); |
| 61 | + }); |
| 62 | + driverInstanceMock.fetchNewsfeedItems.mockReturnValue( |
| 63 | + cold<FetchResult>('a|', { |
| 64 | + a: createFetchResult({ feedItems: ['item' as any] }), |
| 65 | + }) |
| 66 | + ); |
| 67 | + const api = getApi(http, createConfig(1000), kibanaVersion, newsfeedId); |
| 68 | + |
| 69 | + expectObservable(api.fetchResults$.pipe(take(1))).toBe('(a|)', { |
| 70 | + a: createFetchResult({ |
| 71 | + feedItems: ['item' as any], |
| 72 | + hasNew: true, |
| 73 | + }), |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('emits based on the predefined interval', () => { |
| 79 | + getTestScheduler().run(({ expectObservable, cold }) => { |
| 80 | + storageInstanceMock.isAnyUnread$.mockImplementation(() => { |
| 81 | + return cold<boolean>('a|', { |
| 82 | + a: true, |
| 83 | + }); |
| 84 | + }); |
| 85 | + driverInstanceMock.fetchNewsfeedItems.mockReturnValue( |
| 86 | + cold<FetchResult>('a|', { |
| 87 | + a: createFetchResult({ feedItems: ['item' as any] }), |
| 88 | + }) |
| 89 | + ); |
| 90 | + const api = getApi(http, createConfig(2), kibanaVersion, newsfeedId); |
| 91 | + |
| 92 | + expectObservable(api.fetchResults$.pipe(take(2))).toBe('a-(b|)', { |
| 93 | + a: createFetchResult({ |
| 94 | + feedItems: ['item' as any], |
| 95 | + hasNew: true, |
| 96 | + }), |
| 97 | + b: createFetchResult({ |
| 98 | + feedItems: ['item' as any], |
| 99 | + hasNew: true, |
| 100 | + }), |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('re-emits when the unread status changes', () => { |
| 106 | + getTestScheduler().run(({ expectObservable, cold }) => { |
| 107 | + storageInstanceMock.isAnyUnread$.mockImplementation(() => { |
| 108 | + return cold<boolean>('a--b', { |
| 109 | + a: true, |
| 110 | + b: false, |
| 111 | + }); |
| 112 | + }); |
| 113 | + driverInstanceMock.fetchNewsfeedItems.mockReturnValue( |
| 114 | + cold<FetchResult>('(a|)', { |
| 115 | + a: createFetchResult({}), |
| 116 | + }) |
| 117 | + ); |
| 118 | + const api = getApi(http, createConfig(10), kibanaVersion, newsfeedId); |
| 119 | + |
| 120 | + expectObservable(api.fetchResults$.pipe(take(2))).toBe('a--(b|)', { |
| 121 | + a: createFetchResult({ |
| 122 | + hasNew: true, |
| 123 | + }), |
| 124 | + b: createFetchResult({ |
| 125 | + hasNew: false, |
| 126 | + }), |
| 127 | + }); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments