import React from 'react' ; import { Entity } from '@backstage/catalog-model' ; import { render } from '@testing-library/react' ; import { entityStub , dependabotAlertsResponseMock } from '../../mocks/mocks' ; import { graphql } from 'msw' ; import { setupRequestMockHandlers , wrapInTestApp } from '@backstage/test-utils' ; import { setupServer } from 'msw/node' ; import { DependabotAlertsWidget } from './DependabotAlertsWidget' ; import { ApiProvider , ApiRegistry } from '@backstage/core-app-api' ; import { configApiRef , githubAuthApiRef } from '@backstage/core-plugin-api' ; let entity ; jest . mock ( '@backstage/plugin-catalog-react' , ( ) => ( { useEntity : ( ) => { return entity ; } , } ) ) ; jest . mock ( '@octokit/graphql' , ( ) => ( { graphql : { defaults : ( ) => { const gqlEndpoint = ( _query , _params ) => { return new Promise ( resolve => { resolve ( dependabotAlertsResponseMock ) ; } ) ; } ; return gqlEndpoint ; } , } , } ) ) ; const GRAPHQL_GITHUB_API = graphql . link ( 'https://api.github.com/graphql' ) ; const mockGithubAuth = { getAccessToken : async ( _ ) => 'test-token' , } ; const config = { getOptionalConfigArray : ( _ ) => [ { getOptionalString : ( _other ) => undefined } , ] , getOptionalStringArray : ( _ ) => [ 'all' ] , } ; const configForLowSeverity = { getOptionalConfigArray : ( _ ) => [ { getOptionalString : ( _other ) => undefined } , ] , getOptionalStringArray : ( _ ) => [ 'low' ] , } ; const apis = ApiRegistry . from ( [ [ configApiRef , config ] , [ githubAuthApiRef , mockGithubAuth ] , ] ) ; const apisLowSeverity = ApiRegistry . from ( [ [ configApiRef , configForLowSeverity ] , [ githubAuthApiRef , mockGithubAuth ] , ] ) ; describe ( 'Dependabot alerts overview' , ( ) => { const worker = setupServer ( ) ; setupRequestMockHandlers ( worker ) ; beforeEach ( ( ) => { worker . resetHandlers ( ) ; jest . resetAllMocks ( ) ; } ) ; describe ( 'GithubDependabotAlertsTable' , ( ) => { beforeEach ( ( ) => { entity = entityStub ; } ) ; it ( 'check if critical severity alerts are shown' , async ( ) => { worker . use ( GRAPHQL_GITHUB_API . query ( 'GetDependabotAlertsWidget' , ( _ , res , ctx ) => { res ( ctx . data ( dependabotAlertsResponseMock ) ) ; } ) ) ; const rendered = render ( wrapInTestApp ( < ApiProvider apis = { apis } > < DependabotAlertsWidget /> < /ApiProvider>)); expect ( await rendered . findByTestId ( 'severitiesContainer' ) ) . toBeInTheDocument ( ) ; expect ( ( await rendered . findAllByText ( 'Critical severity' ) ) . length ) . toBeGreaterThan ( 0 ) ; expect ( ( await rendered . findAllByText ( 'High severity' ) ) . length ) . toBeGreaterThan ( 0 ) ; expect ( ( await rendered . findAllByText ( 'Medium severity' ) ) . length ) . toBeGreaterThan ( 0 ) ; expect ( ( await rendered . findAllByText ( 'Low severity' ) ) . length ) . toBeGreaterThan ( 0 ) ; expect ( ( await rendered . findAllByTestId ( 'severityLevel' ) ) . length ) . toBeGreaterThanOrEqual ( 4 ) ; } ) ; it ( 'check if only low severity is shown' , async ( ) => { worker . use ( GRAPHQL_GITHUB_API . query ( 'GetDependabotAlertsWidget' , ( _ , res , ctx ) => { res ( ctx . data ( dependabotAlertsResponseMock ) ) ; } ) ) ; const rendered = render ( wrapInTestApp ( < ApiProvider apis = { apisLowSeverity } > < DependabotAlertsWidget /> < /ApiProvider>)); expect ( ( await rendered . findAllByText ( '0' ) ) . length ) . toBeGreaterThan ( 0 ) ; expect ( ( await rendered . findAllByTestId ( 'severityLevel' ) ) . length ) . toBeLessThanOrEqual ( 1 ) ; } ) ; } ) ; } ) ;