Skip to content

Commit

Permalink
feat: [P4PU-384] test and config refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoio committed Sep 26, 2024
1 parent 19b617e commit fd445ab
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ VERSION="test"
PAYMENT_RETURN_URL="http://localhost:1234"
# Cause our webapp will served in a subpath of a domain
DEPLOY_PATH="/pagamenti"
TZ="utc"
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"type-check": "tsc",
"storybook": "storybook dev -p 6006",
"build-storybook": "yarn generate && storybook build",
"test": "TZ=UTC vitest --run",
"coverage": "vitest --coverage --run",
"test": "vitest",
"coverage": "vitest --coverage",
"axe": "axe localhost:1234, localhost:1234/transactions, localhost:1234/transaction/1, localhost:1234/user, --include '#root'",
"serve-coverage-html-report": "npx serve coverage/lcov-report -c ./../../serve.json"
},
Expand Down
33 changes: 18 additions & 15 deletions src/routes/Dashboard/Dashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { fireEvent, render, waitFor, screen } from '@testing-library/react';
import Dashboard from '.';
import '@testing-library/jest-dom';
import { useStore } from 'store/GlobalStore';
import { useMediaQuery } from '@mui/material';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import { useUserInfo } from 'hooks/useUserInfo';
Expand All @@ -13,6 +12,8 @@ import storage from 'utils/storage';
import { Mock } from 'vitest';
import { Signal } from '@preact/signals-react';
import { i18nTestSetup } from '__tests__/i18nTestSetup';
import { ThemeProvider } from '@mui/material';
import { theme } from '@pagopa/mui-italia';

i18nTestSetup({
app: {
Expand All @@ -30,11 +31,6 @@ i18nTestSetup({
vi.mock('utils/converters');
vi.mock('utils/loaders');

vi.mock('@mui/material', async (importActual) => ({
...(await importActual()),
useMediaQuery: vi.fn()
}));

vi.mock('store/GlobalStore', () => ({
useStore: vi.fn()
}));
Expand All @@ -48,11 +44,9 @@ vi.mock('hooks/useUserInfo', () => ({
useUserInfo: vi.fn()
}));

const queryClient = new QueryClient();
describe('DashboardRoute', () => {
(useMediaQuery as Mock).mockReturnValue(false);
const queryClient = new QueryClient();
const navigate = vi.fn();
(useNavigate as Mock).mockReturnValue(navigate);
const setState = vi.fn();
const mockTransactions = {
transactions: [
Expand All @@ -66,13 +60,20 @@ describe('DashboardRoute', () => {
];

const mockPrepareRowsData = vi.fn().mockReturnValue(preparedData);
converters.prepareRowsData = mockPrepareRowsData;

(loaders.getTransactions as Mock).mockReturnValue({
data: mockTransactions,
isError: false
beforeAll(() => {
vi.mocked(useNavigate).mockReturnValue(navigate);
converters.prepareRowsData = mockPrepareRowsData;

vi.mocked(loaders.getTransactions as Mock).mockReturnValue({
data: mockTransactions,
isError: false
});

Object.defineProperty(document.documentElement, 'lang', { value: 'it', configurable: true });
});
Object.defineProperty(document.documentElement, 'lang', { value: 'it', configurable: true });

afterAll(() => {});

beforeEach(() => {
(useStore as Mock).mockReturnValue({ setState });
Expand All @@ -91,7 +92,9 @@ describe('DashboardRoute', () => {
const DashboardWithState = () => {
return (
<QueryClientProvider client={queryClient}>
<Dashboard />
<ThemeProvider theme={theme}>
<Dashboard />
</ThemeProvider>
</QueryClientProvider>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/converters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('return a transactionDetail object', () => {
transactionId: 'string',
authCode: 'string',
rrn: 'string',
transactionDate: 'string',
transactionDate: new Date().toISOString(),
pspName: 'string',
walletInfo: { accountHolder: 'string', brand: 'string', blurredNumber: 'string' },
paymentMethod: 'BBT',
Expand Down
12 changes: 8 additions & 4 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ export default defineConfig({
setupFiles: './vitest.setup.mts',
environment: 'jsdom',
clearMocks: true,
watch: false,
silent: true,
coverage: {
provider: 'v8',
reportOnFailure: true,
exclude: [
...configDefaults.exclude,
'/node_modules/',
'**/*.test.ts?(x)',
'src/__tests__/',
'src/stories/',
'src/index.tsx',
'src/App.tsx',
'src/global.d.ts',
'src/components/Layout',
'src/utils/style'
'src/components/Layout.tsx',
'src/utils/style.tsx',
'src/models/',
],
include: ['**/*.test.ts?(x)'],
include: ['src/**/*.ts?(x)'],
thresholds: {
lines: 80,
branches: 80
Expand Down
3 changes: 3 additions & 0 deletions vitest.setup.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { vi } from 'vitest';
import { i18nTestSetup } from './src/__tests__/i18nTestSetup';

i18nTestSetup({});

vi.mock('@preact/signals-react', () => {
return {
Expand Down

0 comments on commit fd445ab

Please sign in to comment.