feat: add testing setup with Vitest and Testing Library
- Updated package.json to include Vitest and Testing Library dependencies and scripts for testing. - Created a test suite for the ProvinceProvider context to validate cityId and province detection based on subdomains. - Implemented unit tests for utility functions in helper/index.js, including phone number formatting and validation. - Added tests for state information retrieval in lib/getStateInfo.js, ensuring correct city and state matching based on subdomains. - Developed tests for appointment slot adaptation and availability checks in lib/appointmentSlots.js. - Created tests for token storage functionality in lib/tokenStore.js. - Implemented sanitization and JSON parsing tests in lib/sanitize.js. - Added CASL ability tests in lib/ability.js to verify user access rights. - Created tests for cookie management in lib/refreshCookie.js. - Developed tests for patient user representation in lib/representationAdapters.js. - Implemented client-side state information retrieval tests in lib/getStateInfoClient.js. - Created tests for canonical URL generation in lib/getCanonicalUrl.js. - Developed tests for clinic API service functions in services/clinicApi.js. - Added request wrapper tests in services/response.js to ensure correct API interaction. - Set up Vitest configuration in vitest.config.mjs for JSX support and alias resolution. - Created setup and utility files for testing environment in test/setup.js and test/utils.jsx.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import { ProvinceProvider, useProvince } from '@/context/ProvinceProvider';
|
||||
|
||||
function Probe() {
|
||||
const { cityId, isProvinceInclude } = useProvince();
|
||||
return <span data-testid="out">{cityId ? `${cityId}:${isProvinceInclude.name}` : 'none'}</span>;
|
||||
}
|
||||
|
||||
function setHostname(hostname) {
|
||||
Object.defineProperty(window, 'location', {
|
||||
value: { hostname },
|
||||
writable: true,
|
||||
configurable: true,
|
||||
});
|
||||
}
|
||||
|
||||
describe('ProvinceProvider / useProvince', () => {
|
||||
it('host شهر شناختهشده → cityId و شهر را از useEffect ست میکند', async () => {
|
||||
setHostname('arak-nobat.ir');
|
||||
render(
|
||||
<ProvinceProvider>
|
||||
<Probe />
|
||||
</ProvinceProvider>,
|
||||
);
|
||||
await waitFor(() => expect(screen.getByTestId('out')).toHaveTextContent('129:اراک'));
|
||||
});
|
||||
|
||||
it('host ناشناخته → cityId null', async () => {
|
||||
setHostname('example.com');
|
||||
render(
|
||||
<ProvinceProvider>
|
||||
<Probe />
|
||||
</ProvinceProvider>,
|
||||
);
|
||||
await waitFor(() => expect(screen.getByTestId('out')).toHaveTextContent('none'));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user