Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor src/components/OrgListCard/OrgListCard.test.tsx from Jest to Vitest #3005

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import React, { act } from 'react';
import React from 'react';
import { act } from 'react-dom/test-utils';
import { render, screen } from '@testing-library/react';
import 'jest-location-mock';
import userEvent from '@testing-library/user-event';
import { describe, test, expect, vi, beforeEach } from 'vitest';
import { I18nextProvider } from 'react-i18next';
import { BrowserRouter } from 'react-router-dom';
import { MockedProvider } from '@apollo/client/testing';

import i18nForTest from 'utils/i18nForTest';
import type { InterfaceOrgListCardProps } from './OrgListCard';
import OrgListCard from './OrgListCard';
import userEvent from '@testing-library/user-event';
import { BrowserRouter } from 'react-router-dom';
import { IS_SAMPLE_ORGANIZATION_QUERY } from 'GraphQl/Queries/Queries';
import { StaticMockLink } from 'utils/StaticMockLink';
import { MockedProvider } from '@apollo/react-testing';
import useLocalStorage from 'utils/useLocalstorage';

const { setItem, removeItem } = useLocalStorage();

// Mock window.location
const mockAssign = vi.fn();
Object.defineProperty(window, 'location', {
value: { assign: mockAssign },
writable: true,
});

const MOCKS = [
{
request: {
Expand Down Expand Up @@ -75,6 +83,10 @@ const props: InterfaceOrgListCardProps = {
};

describe('Testing the Super Dash List', () => {
beforeEach(() => {
vi.clearAllMocks();
});

test('should render props and text elements test for the page component', async () => {
removeItem('id');
setItem('id', '123'); // Means the user is an admin
Expand All @@ -88,22 +100,24 @@ describe('Testing the Super Dash List', () => {
</BrowserRouter>
</MockedProvider>,
);

await wait();
expect(screen.getByAltText(/Dogs Care image/i)).toBeInTheDocument();
expect(screen.getByText(/Admins:/i)).toBeInTheDocument();
expect(screen.getByText(/Members:/i)).toBeInTheDocument();
expect(screen.getByText('Dogs Care')).toBeInTheDocument();
expect(screen.getByText(/Sample City/i)).toBeInTheDocument();
expect(screen.getByText(/123 Sample Street/i)).toBeInTheDocument();
expect(screen.getByTestId(/manageBtn/i)).toBeInTheDocument();
expect(screen.getByTestId(/flaskIcon/i)).toBeInTheDocument();
userEvent.click(screen.getByTestId(/manageBtn/i));

expect(screen.getByAltText(/Dogs Care image/i)).toBeDefined();
expect(screen.getByText(/Admins:/i)).toBeDefined();
expect(screen.getByText(/Members:/i)).toBeDefined();
expect(screen.getByText('Dogs Care')).toBeDefined();
expect(screen.getByText(/Sample City/i)).toBeDefined();
expect(screen.getByText(/123 Sample Street/i)).toBeDefined();
expect(screen.getByTestId(/manageBtn/i)).toBeDefined();
expect(screen.getByTestId(/flaskIcon/i)).toBeDefined();

await userEvent.click(screen.getByTestId(/manageBtn/i));
removeItem('id');
});

test('Testing if the props data is not provided', () => {
window.location.assign('/orgdash');

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
Expand All @@ -114,14 +128,15 @@ describe('Testing the Super Dash List', () => {
</MockedProvider>,
);

expect(window.location).toBeAt('/orgdash');
expect(mockAssign).toHaveBeenCalledWith('/orgdash');
});

test('Testing if component is rendered properly when image is null', () => {
const imageNullProps = {
...props,
...{ data: { ...props.data, ...{ image: null } } },
};

render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
Expand All @@ -131,10 +146,11 @@ describe('Testing the Super Dash List', () => {
</BrowserRouter>
</MockedProvider>,
);
expect(screen.getByTestId(/emptyContainerForImage/i)).toBeInTheDocument();

expect(screen.getByTestId(/emptyContainerForImage/i)).toBeDefined();
});

test('Testing if user is redirected to orgDash screen', () => {
test('Testing if user is redirected to orgDash screen', async () => {
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
Expand All @@ -144,6 +160,7 @@ describe('Testing the Super Dash List', () => {
</BrowserRouter>
</MockedProvider>,
);
userEvent.click(screen.getByTestId('manageBtn'));

await userEvent.click(screen.getByTestId('manageBtn'));
});
});
Loading