Skip to content

Commit

Permalink
fix the routes
Browse files Browse the repository at this point in the history
  • Loading branch information
niyibi250 committed Jul 9, 2024
1 parent 4b8c675 commit 75d9ce6
Show file tree
Hide file tree
Showing 14 changed files with 836 additions and 9 deletions.
98 changes: 98 additions & 0 deletions src/__test__/dashBoard/ConfirmationCard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest'; // Assuming 'vitest' is used for assertions
import ConfirmationCard from '@/components/dashBoard/ConfirmationCard'; // Adjust path as needed

describe('ConfirmationCard Component', () => {
it('does not render when isVisible is false', () => {
render(
<ConfirmationCard
isVisible={false}
onClose={() => {}}
onConfirm={() => {}}
message="Are you sure?"
/>
);

const modal = screen.queryByText('Are you sure?');
expect(modal).not.toBeInTheDocument();
});

it('renders correctly when isVisible is true', () => {
render(
<ConfirmationCard
isVisible
onClose={() => {}}
onConfirm={() => {}}
message="Are you sure?"
/>
);

const modal = screen.getByText('Are you sure?');
expect(modal).toBeInTheDocument();
});

it('calls onClose when the close button is clicked', () => {
const onClose = vi.fn();
render(
<ConfirmationCard
isVisible
onClose={onClose}
onConfirm={() => {}}
message="Are you sure?"
/>
);

const closeButton = screen.getByRole('button', { name: /close modal/i });
fireEvent.click(closeButton);
expect(onClose).toHaveBeenCalledTimes(1);
});

it('calls onClose when the "No, cancel" button is clicked', () => {
const onClose = vi.fn();
render(
<ConfirmationCard
isVisible
onClose={onClose}
onConfirm={() => {}}
message="Are you sure?"
/>
);

const cancelButton = screen.getByRole('button', { name: /no, cancel/i });
fireEvent.click(cancelButton);
expect(onClose).toHaveBeenCalledTimes(1);
});

it('calls onConfirm when the "Yes, I\'m sure" button is clicked', () => {
const onConfirm = vi.fn();
render(
<ConfirmationCard
isVisible
onClose={() => {}}
onConfirm={onConfirm}
message="Are you sure?"
/>
);

const confirmButton = screen.getByRole('button', {
name: /yes, i'm sure/i,
});
fireEvent.click(confirmButton);
expect(onConfirm).toHaveBeenCalledTimes(1);
});

it('displays the correct message', () => {
const message = 'Are you absolutely sure?';
render(
<ConfirmationCard
isVisible
onClose={() => {}}
onConfirm={() => {}}
message={message}
/>
);

const modalMessage = screen.getByText(message);
expect(modalMessage).toBeInTheDocument();
});
});
151 changes: 151 additions & 0 deletions src/__test__/dashBoard/NavigateonPage.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { render, screen, fireEvent } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import CircularPagination from '@/components/dashBoard/NavigateonPage';

describe('CircularPagination Component', () => {
const onPageChange = vi.fn();

it('renders the correct number of pages when totalPages <= 5', () => {
render(
<CircularPagination
totalPages={5}
currentPage={1}
onPageChange={onPageChange}
/>
);

const pageButtons = screen.getAllByRole('button');
expect(pageButtons).toHaveLength(7);
expect(screen.getByText('1')).toBeInTheDocument();
expect(screen.getByText('2')).toBeInTheDocument();
expect(screen.getByText('3')).toBeInTheDocument();
expect(screen.getByText('4')).toBeInTheDocument();
expect(screen.getByText('5')).toBeInTheDocument();
});

it('renders correctly when currentPage is at the beginning', () => {
render(
<CircularPagination
totalPages={10}
currentPage={1}
onPageChange={onPageChange}
/>
);

const pageButtons = screen.getAllByRole('button');
expect(pageButtons).toHaveLength(9);
expect(screen.getByText('1')).toBeInTheDocument();
expect(screen.getByText('2')).toBeInTheDocument();
expect(screen.getByText('3')).toBeInTheDocument();
expect(screen.getByText('4')).toBeInTheDocument();
expect(screen.getByText('...')).toBeInTheDocument();
expect(screen.getByText('9')).toBeInTheDocument();
expect(screen.getByText('10')).toBeInTheDocument();
});

it('renders correctly when currentPage is at the end', () => {
render(
<CircularPagination
totalPages={10}
currentPage={10}
onPageChange={onPageChange}
/>
);

const pageButtons = screen.getAllByRole('button');
expect(pageButtons).toHaveLength(9);
expect(screen.getByText('1')).toBeInTheDocument();
expect(screen.getByText('2')).toBeInTheDocument();
expect(screen.getByText('...')).toBeInTheDocument();
expect(screen.getByText('7')).toBeInTheDocument();
expect(screen.getByText('8')).toBeInTheDocument();
expect(screen.getByText('9')).toBeInTheDocument();
expect(screen.getByText('10')).toBeInTheDocument();
});

it('renders correctly when currentPage is in the middle', () => {
render(
<CircularPagination
totalPages={10}
currentPage={5}
onPageChange={onPageChange}
/>
);

const pageButtons = screen.getAllByRole('button');
expect(pageButtons).toHaveLength(10);
expect(screen.getByText('1')).toBeInTheDocument();
expect(screen.getByText('2')).toBeInTheDocument();
expect(screen.getByText('4')).toBeInTheDocument();
expect(screen.getByText('5')).toBeInTheDocument();
expect(screen.getByText('6')).toBeInTheDocument();
expect(screen.getByText('...')).toBeInTheDocument();
expect(screen.getByText('9')).toBeInTheDocument();
expect(screen.getByText('10')).toBeInTheDocument();
});

it('calls onPageChange with the correct page number when a page button is clicked', () => {
render(
<CircularPagination
totalPages={10}
currentPage={5}
onPageChange={onPageChange}
/>
);

fireEvent.click(screen.getByText('6'));
expect(onPageChange).toHaveBeenCalledWith(6);
});

it('calls onPageChange with the correct page number when the next button is clicked', () => {
render(
<CircularPagination
totalPages={10}
currentPage={5}
onPageChange={onPageChange}
/>
);

fireEvent.click(screen.getByLabelText('next page'));
expect(onPageChange).toHaveBeenCalledWith(6);
});

it('calls onPageChange with the correct page number when the previous button is clicked', () => {
render(
<CircularPagination
totalPages={10}
currentPage={5}
onPageChange={onPageChange}
/>
);

fireEvent.click(screen.getByLabelText('Previous page'));
expect(onPageChange).toHaveBeenCalledWith(4);
});

it('disables the previous button when on the first page', () => {
render(
<CircularPagination
totalPages={10}
currentPage={1}
onPageChange={onPageChange}
/>
);

const prevButton = screen.getByLabelText('Previous page');
expect(prevButton).toBeDisabled();
});

it('disables the next button when on the last page', () => {
render(
<CircularPagination
totalPages={10}
currentPage={10}
onPageChange={onPageChange}
/>
);

const nextButton = screen.getByLabelText('next page');
expect(nextButton).toBeDisabled();
});
});
51 changes: 51 additions & 0 deletions src/__test__/dashBoard/dashboardProductSlice.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, it, expect } from 'vitest';
import DeshboardProductsSlice, {
initialState,
fetchDashboardProduct,
} from '@/features/Dashboard/dashboardProductsSlice'; // Adjust path as needed

describe('DeshboardProductsSlice reducer', () => {
it('should return the initial state', () => {
const action = { type: 'unknown/action' };
expect(DeshboardProductsSlice(undefined, action)).toEqual(initialState);
});

it('should handle fetchDashboardProduct.pending', () => {
expect(
DeshboardProductsSlice(initialState, {
type: fetchDashboardProduct.pending.type,
})
).toEqual({
...initialState,
status: 'loading',
});
});

it('should handle fetchDashboardProduct.fulfilled', () => {
const mockProducts = [
{ id: 1, title: 'Product A' },
{ id: 2, title: 'Product B' },
];
expect(
DeshboardProductsSlice(initialState, {
type: fetchDashboardProduct.fulfilled.type,
payload: mockProducts,
})
).toEqual({
...initialState,
status: 'succeeded',
DashboardProduct: mockProducts,
});
});

it('should handle fetchDashboardProduct.rejected', () => {
expect(
DeshboardProductsSlice(initialState, {
type: fetchDashboardProduct.rejected.type,
})
).toEqual({
...initialState,
status: 'failed',
});
});
});
2 changes: 2 additions & 0 deletions src/app/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import productsReducer from '@/app/slices/ProductSlice';
import categoriesReducer from '@/app/slices/categorySlice';
import bannerReducer from '@/app/bannerAds/BannerSlice';
import availableProductsSlice from '@/features/Popular/availableProductSlice';
import DeshboardProductsSlice from '../features/Dashboard/dashboardProductsSlice';
import subscribeReducer from '@/app/Footer/Subscribe';

export const store = configureStore({
Expand All @@ -16,6 +17,7 @@ export const store = configureStore({
banners: bannerReducer,
availableProducts: availableProductsSlice,
footer: subscribeReducer,
DeshboardProducts: DeshboardProductsSlice,
},
});

Expand Down
76 changes: 76 additions & 0 deletions src/components/dashBoard/ConfirmationCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
interface ConfirmationCardProps {
isVisible: boolean;
onClose: () => void;
onConfirm: () => void;
message: string;
}

function ConfirmationCard({
isVisible,
onClose,
onConfirm,
message,
}: ConfirmationCardProps) {
if (!isVisible) return null;

return (
<div className="fixed inset-0 flex justify-center items-center z-50 bg-black bg-opacity-50">
<div className="relative p-4 w-full max-w-md h-full md:h-auto">
<div className="relative p-4 text-center bg-white rounded-lg shadow dark:bg-gray-800 sm:p-5">
<button
type="button"
className="text-gray-400 absolute top-2.5 right-2.5 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center dark:hover:bg-gray-600 dark:hover:text-white"
onClick={onClose}
>
<svg
aria-hidden="true"
className="w-5 h-5"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
clipRule="evenodd"
/>
</svg>
<span className="sr-only">Close modal</span>
</button>
<svg
className="text-gray-400 dark:text-gray-500 w-11 h-11 mb-3.5 mx-auto"
aria-hidden="true"
fill="currentColor"
viewBox="0 0 20 20"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
d="M9 2a1 1 0 00-.894.553L7.382 4H4a1 1 0 000 2v10a2 2 0 002 2h8a2 2 0 002-2V6a1 1 0 100-2h-3.382l-.724-1.447A1 1 0 0011 2H9zM7 8a1 1 0 012 0v6a1 1 0 11-2 0V8zm5-1a1 1 0 00-1 1v6a1 1 0 102 0V8a1 1 0 00-1-1z"
clipRule="evenodd"
/>
</svg>
<p className="mb-4 text-gray-500 dark:text-gray-300">{message}</p>
<div className="flex justify-center items-center space-x-4">
<button
type="button"
className="py-2 px-3 text-sm font-medium text-gray-500 bg-white rounded-lg border border-gray-200 hover:bg-gray-100 focus:ring-4 focus:outline-none focus:ring-primary-300 hover:text-gray-900 focus:z-10 dark:bg-gray-700 dark:text-gray-300 dark:border-gray-500 dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-gray-600"
onClick={onClose}
>
No, cancel
</button>
<button
type="button"
className="py-2 px-3 text-sm font-medium text-center text-white bg-red-600 rounded-lg hover:bg-red-700 focus:ring-4 focus:outline-none focus:ring-red-300 dark:bg-red-500 dark:hover:bg-red-600 dark:focus:ring-red-900"
onClick={onConfirm}
>
Yes, I&apos;m sure
</button>
</div>
</div>
</div>
</div>
);
}

export default ConfirmationCard;
Loading

0 comments on commit 75d9ce6

Please sign in to comment.