@ -14,6 +14,7 @@ const setup = (propOverrides: Partial<Props>) => {
const loadApiKeysMock = jest . fn ( ) ;
const deleteApiKeyMock = jest . fn ( ) ;
const addApiKeyMock = jest . fn ( ) ;
const toggleIncludeExpiredMock = jest . fn ( ) ;
const setSearchQueryMock = mockToolkitActionCreator ( setSearchQuery ) ;
const props : Props = {
navModel : {
@ -33,21 +34,31 @@ const setup = (propOverrides: Partial<Props>) => {
addApiKey : addApiKeyMock ,
apiKeysCount : 0 ,
timeZone : 'utc' ,
includeExpired : false ,
includeExpiredDisabled : false ,
toggleIncludeExpired : toggleIncludeExpiredMock ,
} ;
Object . assign ( props , propOverrides ) ;
const { rerender } = render ( < ApiKeysPageUnconnected { ...props } / > ) ;
return { rerender , props , loadApiKeysMock , setSearchQueryMock , deleteApiKeyMock , addApiKeyMock } ;
return {
rerender ,
props ,
loadApiKeysMock ,
setSearchQueryMock ,
deleteApiKeyMock ,
addApiKeyMock ,
toggleIncludeExpiredMock ,
} ;
} ;
describe ( 'ApiKeysPage' , ( ) = > {
silenceConsoleOutput ( ) ;
describe ( 'when mounted' , ( ) = > {
it ( 'then it should call loadApiKeys without expired' , ( ) = > {
it ( 'then it should call loadApiKeys' , ( ) = > {
const { loadApiKeysMock } = setup ( { } ) ;
expect ( loadApiKeysMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( loadApiKeysMock ) . toHaveBeenCalledWith ( false ) ;
} ) ;
} ) ;
@ -82,19 +93,12 @@ describe('ApiKeysPage', () => {
} ) ;
describe ( 'when a user toggles the Show expired toggle' , ( ) = > {
it ( 'then it should call loadApiKeys with correct parameters ' , async ( ) = > {
it ( 'then it should dispatch toggleIncludeExpired ' , async ( ) = > {
const apiKeys = getMultipleMockKeys ( 3 ) ;
const { loadApiKeys Mock } = setup ( { apiKeys , apiKeysCount : apiKeys.length , hasFetched : true } ) ;
const { toggleIncludeExpired Mock } = setup ( { apiKeys , apiKeysCount : apiKeys.length , hasFetched : true } ) ;
loadApiKeysMock . mockClear ( ) ;
toggleShowExpired ( ) ;
expect ( loadApiKeysMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( loadApiKeysMock ) . toHaveBeenCalledWith ( true ) ;
loadApiKeysMock . mockClear ( ) ;
toggleShowExpired ( ) ;
expect ( loadApiKeysMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( loadApiKeysMock ) . toHaveBeenCalledWith ( false ) ;
expect ( toggleIncludeExpiredMock ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
} ) ;
@ -128,7 +132,7 @@ describe('ApiKeysPage', () => {
expect ( within ( firstRow ) . getByRole ( 'button' , { name : /delete$/i } ) ) . toBeInTheDocument ( ) ;
userEvent . click ( within ( firstRow ) . getByRole ( 'button' , { name : /delete$/i } ) ) ;
expect ( deleteApiKeyMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( deleteApiKeyMock ) . toHaveBeenCalledWith ( 1 , false ) ;
expect ( deleteApiKeyMock ) . toHaveBeenCalledWith ( 1 ) ;
toggleShowExpired ( ) ;
@ -140,7 +144,7 @@ describe('ApiKeysPage', () => {
skipPointerEventsCheck : true ,
} ) ;
expect ( deleteApiKeyMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( deleteApiKeyMock ) . toHaveBeenCalledWith ( 2 , true ) ;
expect ( deleteApiKeyMock ) . toHaveBeenCalledWith ( 2 ) ;
} ) ;
} ) ;
@ -151,7 +155,7 @@ describe('ApiKeysPage', () => {
addApiKeyMock . mockClear ( ) ;
userEvent . click ( screen . getByTestId ( selectors . components . CallToActionCard . buttonV2 ( 'New API key' ) ) ) ;
await addAndVerifyApiKey ( addApiKeyMock , false ) ;
await addAndVerifyApiKey ( addApiKeyMock ) ;
} ) ;
} ) ;
@ -162,13 +166,13 @@ describe('ApiKeysPage', () => {
addApiKeyMock . mockClear ( ) ;
userEvent . click ( screen . getByRole ( 'button' , { name : /add api key/i } ) ) ;
await addAndVerifyApiKey ( addApiKeyMock , false ) ;
await addAndVerifyApiKey ( addApiKeyMock ) ;
toggleShowExpired ( ) ;
addApiKeyMock . mockClear ( ) ;
userEvent . click ( screen . getByRole ( 'button' , { name : /add api key/i } ) ) ;
await addAndVerifyApiKey ( addApiKeyMock , true ) ;
await addAndVerifyApiKey ( addApiKeyMock ) ;
} ) ;
} ) ;
@ -190,11 +194,11 @@ describe('ApiKeysPage', () => {
} ) ;
function toggleShowExpired() {
expect ( screen . queryByLabelText ( /show expired /i ) ) . toBeInTheDocument ( ) ;
userEvent . click ( screen . getByLabelText ( /show expired /i ) ) ;
expect ( screen . queryByLabelText ( /include expired keys /i ) ) . toBeInTheDocument ( ) ;
userEvent . click ( screen . getByLabelText ( /include expired keys /i ) ) ;
}
async function addAndVerifyApiKey ( addApiKeyMock : jest.Mock , includeExpired : boolean ) {
async function addAndVerifyApiKey ( addApiKeyMock : jest.Mock ) {
expect ( screen . getByRole ( 'heading' , { name : /add api key/i } ) ) . toBeInTheDocument ( ) ;
expect ( screen . getByPlaceholderText ( /name/i ) ) . toBeInTheDocument ( ) ;
expect ( screen . getByPlaceholderText ( /1d/i ) ) . toBeInTheDocument ( ) ;
@ -204,9 +208,5 @@ async function addAndVerifyApiKey(addApiKeyMock: jest.Mock, includeExpired: bool
userEvent . type ( screen . getByPlaceholderText ( /1d/i ) , '60s' ) ;
userEvent . click ( screen . getByRole ( 'button' , { name : /^add$/i } ) ) ;
expect ( addApiKeyMock ) . toHaveBeenCalledTimes ( 1 ) ;
expect ( addApiKeyMock ) . toHaveBeenCalledWith (
{ name : 'Test' , role : 'Viewer' , secondsToLive : 60 } ,
expect . anything ( ) ,
includeExpired
) ;
expect ( addApiKeyMock ) . toHaveBeenCalledWith ( { name : 'Test' , role : 'Viewer' , secondsToLive : 60 } , expect . anything ( ) ) ;
}