Skip to content

Commit

Permalink
[VAS-1133] chore: Api key unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
svariant committed Jun 27, 2024
1 parent 181ee88 commit f2dbc89
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 46 deletions.
18 changes: 18 additions & 0 deletions src/model/__tests__/ApiKey.test.tsx
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
])
})
})
137 changes: 91 additions & 46 deletions src/pages/apiKeys/__tests__/AddApiKeyPage.test.tsx
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();
});
});
});

0 comments on commit f2dbc89

Please sign in to comment.