-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VAS-1133] chore: Api key unit test coverage
- Loading branch information
Showing
2 changed files
with
109 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { API_KEY_PRODUCTS, getApiKeyProducts } from '../ApiKey'; | ||
describe("Test ApiKey model methods", ()=> { | ||
test("Test getApiKeysProducts as PSP", () => { | ||
expect(getApiKeyProducts(true, true)).toEqual([API_KEY_PRODUCTS.NODOAUTH, API_KEY_PRODUCTS.BO_EXT_PSP, API_KEY_PRODUCTS.FDR_PSP]) | ||
}) | ||
test("Test getApiKeysProducts as EC", () => { | ||
expect(getApiKeyProducts(false, true)).toEqual([ | ||
API_KEY_PRODUCTS.NODOAUTH, | ||
API_KEY_PRODUCTS.GPD, | ||
API_KEY_PRODUCTS.GPD_PAY, | ||
API_KEY_PRODUCTS.GPD_REP, | ||
API_KEY_PRODUCTS.BIZ, | ||
API_KEY_PRODUCTS.BO_EXT_EC, | ||
API_KEY_PRODUCTS.FDR_ORG, | ||
API_KEY_PRODUCTS.PRINT_NOTICE | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,105 @@ | ||
import {ThemeProvider} from '@mui/system'; | ||
import {theme} from '@pagopa/mui-italia'; | ||
import {fireEvent, render, screen} from '@testing-library/react'; | ||
import { ThemeProvider } from '@mui/system'; | ||
import { theme } from '@pagopa/mui-italia'; | ||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
import React from 'react'; | ||
import {Provider} from 'react-redux'; | ||
import {MemoryRouter, Route} from 'react-router-dom'; | ||
import {partiesActions} from '../../../redux/slices/partiesSlice'; | ||
import {store} from '../../../redux/store'; | ||
import { Provider } from 'react-redux'; | ||
import { MemoryRouter, Route } from 'react-router-dom'; | ||
import { partiesActions } from '../../../redux/slices/partiesSlice'; | ||
import { store } from '../../../redux/store'; | ||
import routes from '../../../routes'; | ||
import {mockedKeys} from '../../../services/__mocks__/apiKeyService'; | ||
import {ecAdminSignedDirect} from '../../../services/__mocks__/partyService'; | ||
import { createMockedKeys, mockedKeys } from '../../../services/__mocks__/apiKeyService'; | ||
import { ecAdminSignedDirect } from '../../../services/__mocks__/partyService'; | ||
import * as ApiKeyService from '../../../services/apiKeyService'; | ||
import AddApiKeyPage from '../AddApiKeyPage'; | ||
import { API_KEY_PRODUCTS } from '../../../model/ApiKey'; | ||
|
||
let getInstitutionApiKeysSpy: jest.SpyInstance; | ||
let createInstitutionApiKeysSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
getInstitutionApiKeysSpy = jest.spyOn(ApiKeyService, 'getInstitutionApiKeys'); | ||
createInstitutionApiKeysSpy = jest.spyOn(ApiKeyService, 'createInstitutionApiKeys'); | ||
jest.spyOn(console, 'error').mockImplementation(() => { | ||
}); | ||
jest.spyOn(console, 'warn').mockImplementation(() => { | ||
}); | ||
getInstitutionApiKeysSpy = jest.spyOn(ApiKeyService, 'getInstitutionApiKeys'); | ||
createInstitutionApiKeysSpy = jest.spyOn(ApiKeyService, 'createInstitutionApiKeys'); | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
}); | ||
|
||
describe('<AddApiKeyPage />', () => { | ||
test('render component AddApiKeyPage', async () => { | ||
store.dispatch(partiesActions.setPartySelected(ecAdminSignedDirect)); | ||
getInstitutionApiKeysSpy.mockResolvedValue(mockedKeys); | ||
|
||
render( | ||
<Provider store={store}> | ||
<MemoryRouter initialEntries={[routes.APIKEYS]}> | ||
<Route path={routes.APIKEYS}> | ||
<ThemeProvider theme={theme}> | ||
<AddApiKeyPage/> | ||
</ThemeProvider> | ||
</Route> | ||
</MemoryRouter> | ||
</Provider> | ||
); | ||
|
||
const selectApiKey = screen | ||
.getByTestId('product-test-id') | ||
.querySelector('input') as HTMLInputElement; | ||
|
||
fireEvent.mouseDown(selectApiKey); | ||
fireEvent.select(selectApiKey, { | ||
target: {value: mockedKeys.institution_api_key_list![0].displayName}, | ||
}); | ||
expect(selectApiKey.value).toBe(mockedKeys.institution_api_key_list![0].displayName); | ||
|
||
const confirmBtn = await screen.findByTestId('api-key-btn-test-id'); | ||
fireEvent.click(confirmBtn); | ||
|
||
const backBtn = await screen.findByTestId('api-key-btn-back-test-id'); | ||
fireEvent.click(backBtn); | ||
test('render component AddApiKeyPage', async () => { | ||
store.dispatch(partiesActions.setPartySelected(ecAdminSignedDirect)); | ||
getInstitutionApiKeysSpy.mockResolvedValue(mockedKeys); | ||
createInstitutionApiKeysSpy.mockResolvedValue(createMockedKeys); | ||
|
||
render( | ||
<Provider store={store}> | ||
<MemoryRouter initialEntries={[routes.APIKEYS]}> | ||
<Route path={routes.APIKEYS}> | ||
<ThemeProvider theme={theme}> | ||
<AddApiKeyPage /> | ||
</ThemeProvider> | ||
</Route> | ||
</MemoryRouter> | ||
</Provider> | ||
); | ||
|
||
const confirmBtn = await screen.findByTestId('api-key-btn-test-id'); | ||
expect(confirmBtn).toBeDisabled(); | ||
|
||
const selectApiKey = screen.getByTestId('product-test-id'); | ||
|
||
fireEvent.mouseDown(screen.getByLabelText('addApiKeyPage.addForm.product.title')); | ||
fireEvent.click( | ||
screen.getByText(new RegExp(`addApiKeyPage.products.${API_KEY_PRODUCTS.BIZ.id}`, 'i')) | ||
); | ||
|
||
expect(selectApiKey).toHaveTextContent(API_KEY_PRODUCTS.BIZ.id); | ||
|
||
await waitFor(() => { | ||
expect(confirmBtn).toBeEnabled(); | ||
}); | ||
|
||
fireEvent.click(confirmBtn); | ||
|
||
await waitFor(() => { | ||
expect(createInstitutionApiKeysSpy).toBeCalled(); | ||
}); | ||
}); | ||
test('render component AddApiKeyPage createInstitutionApiKeysSpy error', async () => { | ||
store.dispatch(partiesActions.setPartySelected(ecAdminSignedDirect)); | ||
getInstitutionApiKeysSpy.mockResolvedValue(mockedKeys); | ||
createInstitutionApiKeysSpy.mockRejectedValue(""); | ||
|
||
render( | ||
<Provider store={store}> | ||
<MemoryRouter initialEntries={[routes.APIKEYS]}> | ||
<Route path={routes.APIKEYS}> | ||
<ThemeProvider theme={theme}> | ||
<AddApiKeyPage /> | ||
</ThemeProvider> | ||
</Route> | ||
</MemoryRouter> | ||
</Provider> | ||
); | ||
|
||
const confirmBtn = await screen.findByTestId('api-key-btn-test-id'); | ||
expect(confirmBtn).toBeDisabled(); | ||
|
||
const selectApiKey = screen.getByTestId('product-test-id'); | ||
|
||
fireEvent.mouseDown(screen.getByLabelText('addApiKeyPage.addForm.product.title')); | ||
fireEvent.click( | ||
screen.getByText(new RegExp(`addApiKeyPage.products.${API_KEY_PRODUCTS.BIZ.id}`, 'i')) | ||
); | ||
|
||
expect(selectApiKey).toHaveTextContent(API_KEY_PRODUCTS.BIZ.id); | ||
|
||
await waitFor(() => { | ||
expect(confirmBtn).toBeEnabled(); | ||
}); | ||
|
||
fireEvent.click(confirmBtn); | ||
|
||
await waitFor(() => { | ||
expect(createInstitutionApiKeysSpy).toBeCalled(); | ||
}); | ||
}); | ||
}); |