import React from 'react' ; import { render } from '@testing-library/react' ; import { ApiProvider , UrlPatternDiscovery } from '@backstage/core-app-api' ; import { EntityProvider } from '@backstage/plugin-catalog-react' ; import { rest } from 'msw' ; import { setupRequestMockHandlers , TestApiRegistry , } from '@backstage/test-utils' ; import { setupServer } from 'msw/node' ; import { MemoryRouter } from 'react-router-dom' ; import { JiraAPI , jiraApiRef } from '../../api' ; import { JiraCard } from './JiraCard' ; import { activityResponseStub , entityStub , projectResponseStub , searchResponseStub , statusesResponseStub , } from '../../responseStubs' ; const discoveryApi = UrlPatternDiscovery . compile ( 'http://exampleapi.com' ) ; const apis = TestApiRegistry . from ( [ jiraApiRef , new JiraAPI ( { discoveryApi } ) ] ) ; describe ( 'JiraCard' , ( ) => { const worker = setupServer ( ) ; setupRequestMockHandlers ( worker ) ; beforeEach ( ( ) => { worker . resetHandlers ( ) ; jest . resetAllMocks ( ) ; } ) ; it ( 'should display board and component data' , async ( ) => { worker . use ( rest . get ( 'http://exampleapi.com/jira/api/rest/api/latest/project/BT' , ( _ , res , ctx ) => res ( ctx . json ( projectResponseStub ) ) ) , rest . post ( 'http://exampleapi.com/jira/api/rest/api/latest/search' , ( _ , res , ctx ) => res ( ctx . json ( searchResponseStub ) ) ) , rest . get ( 'http://exampleapi.com/jira/api/rest/api/latest/project/BT/statuses' , ( _ , res , ctx ) => res ( ctx . json ( statusesResponseStub ) ) ) , rest . get ( 'http://exampleapi.com/jira/api/activity' , ( _ , res , ctx ) => res ( ctx . xml ( activityResponseStub ) ) ) ) ; const rendered = render ( < MemoryRouter > < ApiProvider apis = { apis } > < EntityProvider entity = { entityStub } > < JiraCard /> < /EntityProvider> < /ApiProvider> < /MemoryRouter>); expect ( await rendered . findByText ( /backstage-test/ ) ) . toBeInTheDocument ( ) ; expect ( ( await rendered . findAllByText ( /testComponent/ ) ) . length ) . toBeGreaterThan ( 0 ) ; expect ( await rendered . findByText ( /changed the status to Selected for Development/ ) ) . toBeInTheDocument ( ) ; expect ( await rendered . findByText ( /Add basic test/ ) ) . toBeInTheDocument ( ) ; expect ( await rendered . getByAltText ( /Page/ ) ) . toHaveAttribute ( 'src' , expect . stringContaining ( 'mocked_icon_filename.gif' ) ) ; } ) ; it ( 'should display an error on fetch failure' , async ( ) => { worker . use ( rest . get ( 'http://exampleapi.com/jira/api/rest/api/latest/project/BT' , ( _ , res , ctx ) => res ( ctx . status ( 403 ) ) ) , rest . post ( 'http://exampleapi.com/jira/api/rest/api/latest/search' , ( _ , res , ctx ) => res ( ctx . status ( 403 ) ) ) , rest . get ( 'http://exampleapi.com/jira/api/rest/api/latest/project/BT/statuses' , ( _ , res , ctx ) => res ( ctx . status ( 403 ) ) ) , rest . get ( 'http://exampleapi.com/jira/api/activity' , ( _ , res , ctx ) => res ( ctx . status ( 403 ) ) ) ) ; const rendered = render ( < MemoryRouter > < ApiProvider apis = { apis } > < EntityProvider entity = { entityStub } > < JiraCard /> < /EntityProvider> < /ApiProvider> < /MemoryRouter>); const text = await rendered . findByText ( /status 403: Forbidden/ ) ; expect ( text ) . toBeInTheDocument ( ) ; } ) ; } ) ;