import { take } from 'rxjs/operators' ; import { coreMock } from '../../../core/public/mocks' ; import { NewsfeedPublicPlugin } from './plugin' ; import { NewsfeedApiEndpoint } from './lib/api' ; import { screenshotModePluginMock } from '../../screenshot_mode/public/mocks' ; describe ( 'Newsfeed plugin' , ( ) => { let plugin ; beforeAll ( ( ) => { jest . useFakeTimers ( ) ; } ) ; afterAll ( ( ) => { jest . useRealTimers ( ) ; } ) ; beforeEach ( ( ) => { plugin = new NewsfeedPublicPlugin ( coreMock . createPluginInitializerContext ( ) ) ; } ) ; describe ( '#start' , ( ) => { beforeEach ( ( ) => { plugin . setup ( coreMock . createSetup ( ) ) ; } ) ; beforeEach ( ( ) => { /**
             * We assume for these tests that the newsfeed stream exposed by start will fetch newsfeed items
             * on the first tick for new subscribers
             */ jest . spyOn ( window , 'fetch' ) ; } ) ; afterEach ( ( ) => { jest . clearAllMocks ( ) ; } ) ; describe ( 'base case' , ( ) => { it ( 'makes fetch requests' , ( ) => { const startContract = plugin . start ( coreMock . createStart ( ) , { screenshotMode : screenshotModePluginMock . createSetupContract ( ) , } ) ; const sub = startContract . createNewsFeed$ ( NewsfeedApiEndpoint . KIBANA ) . pipe ( take ( 1 ) ) . subscribe ( ( ) => { } ) ; jest . runOnlyPendingTimers ( ) ; expect ( window . fetch ) . toHaveBeenCalled ( ) ; sub . unsubscribe ( ) ; } ) ; } ) ; describe ( 'when in screenshot mode' , ( ) => { it ( 'makes no fetch requests in screenshot mode' , ( ) => { const screenshotMode = screenshotModePluginMock . createSetupContract ( ) ; screenshotMode . isScreenshotMode . mockReturnValue ( true ) ; const startContract = plugin . start ( coreMock . createStart ( ) , { screenshotMode , } ) ; const sub = startContract . createNewsFeed$ ( NewsfeedApiEndpoint . KIBANA ) . pipe ( take ( 1 ) ) . subscribe ( ( ) => { } ) ; jest . runOnlyPendingTimers ( ) ; expect ( window . fetch ) . not . toHaveBeenCalled ( ) ; sub . unsubscribe ( ) ; } ) ; } ) ; } ) ; } ) ;