import React from 'react' ; import { render } from '@testing-library/react' ; import { IdentityApi , errorApiRef , configApiRef , } from '@backstage/core-plugin-api' ; import { ApiRegistry , ApiProvider , UrlPatternDiscovery , } from '@backstage/core-app-api' ; import { rest } from 'msw' ; import { setupRequestMockHandlers , wrapInTestApp } from '@backstage/test-utils' ; import { setupServer } from 'msw/node' ; import { TravisCIApiClient , travisCIApiRef } from '../api' ; import { RecentTravisCIBuildsWidget } from '..' ; import { buildsResponseMock , entityMock } from '../mocks/mocks' ; import { EntityProvider } from '@backstage/plugin-catalog-react' ; const discoveryApi = UrlPatternDiscovery . compile ( 'http://exampleapi.com' ) ; const errorApiMock = { post : jest . fn ( ) , error$ : jest . fn ( ) } ; const identityApi = { getUserId ( ) { return 'jane-fonda' ; } , getProfile ( ) { return { email : 'jane-fonda@spotify.com' } ; } , async getIdToken ( ) { return Promise . resolve ( 'fake-id-token' ) ; } , async signOut ( ) { return Promise . resolve ( ) ; } , getProfileInfo : jest . fn ( ) , getBackstageIdentity : jest . fn ( ) , getCredentials : jest . fn ( ) , } ; const config = { getString : ( _ ) => undefined , } ; const apis = ApiRegistry . from ( [ [ configApiRef , config ] , [ errorApiRef , errorApiMock ] , [ travisCIApiRef , new TravisCIApiClient ( { discoveryApi , identityApi } ) ] , ] ) ; describe ( 'LastBuildCard' , ( ) => { const worker = setupServer ( ) ; setupRequestMockHandlers ( worker ) ; beforeEach ( ( ) => { worker . use ( rest . get ( 'http://exampleapi.com/travisci/api/repo/RoadieHQ%2Fsample-service/builds' , ( _ , res , ctx ) => res ( ctx . json ( buildsResponseMock ) ) ) , rest . get ( 'http://exampleapi.com/travisci/api/repo/RoadieHQ%2Fsample-service/builds' , ( _ , res , ctx ) => res ( ctx . json ( buildsResponseMock ) ) ) ) ; } ) ; it ( 'should display widget with data' , async ( ) => { const rendered = render ( wrapInTestApp ( < ApiProvider apis = { apis } > < EntityProvider entity = { entityMock } > < RecentTravisCIBuildsWidget entity = { entityMock } / > < /EntityProvider> < /ApiProvider>)); expect ( await rendered . findByText ( buildsResponseMock . builds [ 2 ] . commit . message ) ) . toBeInTheDocument ( ) ; expect ( ( await rendered . findAllByText ( buildsResponseMock . builds [ 2 ] . state ) ) [ 0 ] ) . toBeInTheDocument ( ) ; expect ( ( await rendered . findAllByText ( buildsResponseMock . builds [ 2 ] . branch . name ) ) [ 0 ] ) . toBeInTheDocument ( ) ; } ) ; } ) ;