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

'Visit' Button Feature Added to Joined Organizations Filter #3232

Open
wants to merge 20 commits into
base: develop-postgres
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
214 changes: 188 additions & 26 deletions src/components/OrganizationCard/OrganizationCard.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { render, screen, fireEvent } from '@testing-library/react';
import { MockedProvider } from '@apollo/client/testing';
import { BrowserRouter } from 'react-router-dom';
import { I18nextProvider } from 'react-i18next';
import OrganizationCard from './OrganizationCard';
import i18nForTest from 'utils/i18nForTest';

/**
* This file contains unit tests for the `OrganizationCard` component.
Expand All @@ -12,38 +16,196 @@
* These tests utilize the React Testing Library for rendering and querying DOM elements.
*/

describe('Testing the Organization Card', () => {
it('should render props and text elements test for the page component', () => {
const props = {
id: '123',
image: 'https://via.placeholder.com/80',
firstName: 'John',
lastName: 'Doe',
name: 'Sample',
};
const mockNavigate = jest.fn();

Check failure on line 19 in src/components/OrganizationCard/OrganizationCard.spec.tsx

View workflow job for this annotation

GitHub Actions / Test Application

src/components/OrganizationCard/OrganizationCard.spec.tsx

ReferenceError: jest is not defined ❯ src/components/OrganizationCard/OrganizationCard.spec.tsx:19:22
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockNavigate,
}));

const defaultProps = {
id: '123',
name: 'Test Organization',
image: 'test-image.jpg',
description: 'Test Description',
admins: [{ id: '1' }],
members: [{ id: '1' }, { id: '2' }],
address: {
city: 'Test City',
countryCode: 'TC',
line1: 'Test Line 1',
postalCode: '12345',
state: 'Test State',
},
userRegistrationRequired: false,
membershipRequests: [],
};

describe('OrganizationCard', () => {
beforeEach(() => {
jest.clearAllMocks();
});

render(<OrganizationCard {...props} />);
test('renders organization card with image', () => {
render(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...defaultProps} membershipRequestStatus="" />
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByText(props.name)).toBeInTheDocument();
expect(screen.getByText(/Owner:/i)).toBeInTheDocument();
expect(screen.getByText(props.firstName)).toBeInTheDocument();
expect(screen.getByText(props.lastName)).toBeInTheDocument();
expect(screen.getByText(defaultProps.name)).toBeInTheDocument();
expect(screen.getByText(/admins: 1/i)).toBeInTheDocument();
expect(screen.getByText(/members: 2/i)).toBeInTheDocument();
expect(screen.getByRole('img')).toBeInTheDocument();
});

it('Should render text elements when props value is not passed', () => {
const props = {
id: '123',
test('renders organization card without image', () => {
const propsWithoutImage = {
...defaultProps,
image: '',
firstName: 'John',
lastName: 'Doe',
name: 'Sample',
};

render(<OrganizationCard {...props} />);
render(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...propsWithoutImage}
membershipRequestStatus=""
/>
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByTestId('emptyContainerForImage')).toBeInTheDocument();
});

test('renders "Join Now" button when membershipRequestStatus is empty', () => {
render(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...defaultProps} membershipRequestStatus="" />
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByTestId('joinBtn')).toBeInTheDocument();
});

test('renders "Visit" button when membershipRequestStatus is accepted', () => {
render(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...defaultProps}
membershipRequestStatus="accepted"
/>
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

const visitButton = screen.getByTestId('manageBtn');
expect(visitButton).toBeInTheDocument();

fireEvent.click(visitButton);
expect(mockNavigate).toHaveBeenCalledWith('/user/organization/123');
});

test('renders "Withdraw" button when membershipRequestStatus is pending', () => {
render(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...defaultProps}
membershipRequestStatus="pending"
/>
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByTestId('withdrawBtn')).toBeInTheDocument();
});

test('displays address when provided', () => {
render(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...defaultProps} membershipRequestStatus="" />
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByText(/Test City/i)).toBeInTheDocument();
expect(screen.getByText(/TC/i)).toBeInTheDocument();
});

test('displays organization description', () => {
render(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...defaultProps} membershipRequestStatus="" />
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);

expect(screen.getByText('Test Description')).toBeInTheDocument();
});

test('displays correct button based on membership status', () => {
// Test for empty status (Join Now button)
const { rerender } = render(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...defaultProps} membershipRequestStatus="" />
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);
expect(screen.getByTestId('joinBtn')).toBeInTheDocument();

// Test for accepted status (Visit button)
rerender(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...defaultProps}
membershipRequestStatus="accepted"
/>
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);
expect(screen.getByTestId('manageBtn')).toBeInTheDocument();

expect(screen.getByText(props.name)).toBeInTheDocument();
expect(screen.getByText(/Owner:/i)).toBeInTheDocument();
expect(screen.getByText(props.firstName)).toBeInTheDocument();
expect(screen.getByText(props.lastName)).toBeInTheDocument();
// Test for pending status (Withdraw button)
rerender(
<MockedProvider>
<BrowserRouter>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...defaultProps}
membershipRequestStatus="pending"
/>
</I18nextProvider>
</BrowserRouter>
</MockedProvider>,
);
expect(screen.getByTestId('withdrawBtn')).toBeInTheDocument();
});
});
Loading
Loading