Wanted to migrate the test file from jest to vitest, so modified the code a bit accordingly, but facing an error.
I guess what would work is mocking a storage mock which i was not able to come up with.
// SKIP_LOCALSTORAGE_CHECK
import React from 'react';
import { MockedProvider } from '@apollo/react-testing';
import {
act,
render,
screen,
fireEvent,
cleanup,
waitFor,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
// import 'jest-localstorage-mock';
// import 'jest-location-mock';
import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import { store } from 'state/store';
import { StaticMockLink } from 'utils/StaticMockLink';
import i18nForTest from 'utils/i18nForTest';
import OrgList from './OrgList';
import {
MOCKS,
MOCKS_ADMIN,
MOCKS_EMPTY,
MOCKS_WITH_ERROR,
} from './OrgListMocks';
import { ToastContainer, toast } from 'react-toastify';
// jest.setTimeout(30000);
import useLocalStorage from 'utils/useLocalstorage';
import { vi } from 'vitest';
const { setItem } = useLocalStorage();
async function wait(ms = 100): Promise<void> {
await act(() => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
});
}
afterEach(() => {
localStorage.clear();
cleanup();
vi.clearAllMocks();
});
describe('Organisations Page testing as SuperAdmin', () => {
setItem('id', '123');
const link = new StaticMockLink(MOCKS, true);
const link2 = new StaticMockLink(MOCKS_EMPTY, true);
const link3 = new StaticMockLink(MOCKS_WITH_ERROR, true);
const formData = {
name: 'Dummy Organization',
description: 'This is a dummy organization',
address: {
city: 'Kingston',
countryCode: 'JM',
dependentLocality: 'Sample Dependent Locality',
line1: '123 Jamaica Street',
line2: 'Apartment 456',
postalCode: 'JM12345',
sortingCode: 'ABC-123',
state: 'Kingston Parish',
},
image: new File(['hello'], 'hello.png', { type: 'image/png' }),
};
test('Should display organisations for superAdmin even if admin For field is empty', async () => {
window.location.assign('/');
setItem('id', '123');
setItem('SuperAdmin', true);
setItem('AdminFor', []);
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<OrgList />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);
await wait();
expect(
screen.queryByText('Organizations Not Found'),
).not.toBeInTheDocument();
});
});
This error arises with all the test cases sharing one here
I tried Mocking a vi.fn() but wasn’t able to work it put can anyone help me with this.