Skip to content

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

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

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 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
219 changes: 193 additions & 26 deletions src/components/OrganizationCard/OrganizationCard.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { jest } from '@jest/globals';
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 { I18nextProvider } from 'react-i18next';
import OrganizationCard from './OrganizationCard';
import i18nForTest from 'utils/i18nForTest';
import { vi } from 'vitest'; // Import vi from vitest instead of jest

/**
* This file contains unit tests for the `OrganizationCard` component.
Expand All @@ -12,38 +17,200 @@ import OrganizationCard from './OrganizationCard';
* 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',
};
<<<<<<< HEAD
const mockNavigate = vi.fn(); // Use vitest.fn() instead of jest.fn()

vi.mock('react-router-dom', async () => {
const actual = await vi.importActual('react-router-dom');
return {
...actual,
BrowserRouter: ({ children }: { children: React.ReactNode }) => children,
=======
const mockNavigate = jest.fn();
jest.mock('react-router-dom', () => {
const actual = jest.requireActual('react-router-dom') as object;
return {
...actual,
>>>>>>> 480a62d786891fb0043eeb06d3a4e47ecc00626b
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(() => {
vi.clearAllMocks(); // Use vitest.clearAllMocks() instead of jest.clearAllMocks()
});

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

render(<OrganizationCard {...props} />);
expect(screen.getByText(defaultProps.name)).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();
// Find the h6 element with className orgadmin
const statsContainer = screen.getByText((content) => {
const normalizedContent = content
.toLowerCase()
.replace(/\s+/g, ' ')
.trim();
return (
normalizedContent.includes('admins') &&
normalizedContent.includes('members')
);
});

expect(statsContainer).toBeInTheDocument();
expect(statsContainer.textContent).toContain('1'); // Check for admin count
expect(statsContainer.textContent).toContain('2'); // Check for member count
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>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...propsWithoutImage} membershipRequestStatus="" />
</I18nextProvider>
</MockedProvider>,
);

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

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

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

test('renders "Visit" button when membershipRequestStatus is accepted', () => {
render(
<MockedProvider>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...defaultProps}
membershipRequestStatus="accepted"
/>
</I18nextProvider>
</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>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...defaultProps}
membershipRequestStatus="pending"
/>
</I18nextProvider>
</MockedProvider>,
);

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

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

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

test('displays organization description', () => {
render(
<MockedProvider>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...defaultProps} membershipRequestStatus="" />
</I18nextProvider>
</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>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard {...defaultProps} membershipRequestStatus="" />
</I18nextProvider>
</MockedProvider>,
);
expect(screen.getByTestId('joinBtn')).toBeInTheDocument();

// Test for accepted status (Visit button)
rerender(
<MockedProvider>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...defaultProps}
membershipRequestStatus="accepted"
/>
</I18nextProvider>
</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>
<I18nextProvider i18n={i18nForTest}>
<OrganizationCard
{...defaultProps}
membershipRequestStatus="pending"
/>
</I18nextProvider>
</MockedProvider>,
);
expect(screen.getByTestId('withdrawBtn')).toBeInTheDocument();
});
});
Loading
Loading