Skip to content

Commit

Permalink
feat: add shimmer cards for task loading in dev mode (#1300)
Browse files Browse the repository at this point in the history
- Added shimmer cards to display while tasks are being fetched in dev mode.
- Implemented tests to ensure correct rendering and behavior of shimmer cards.
  • Loading branch information
VinuB-Dev authored Dec 24, 2024
1 parent c3a1a0b commit 1c06b65
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
41 changes: 41 additions & 0 deletions __tests__/Unit/pages/Tasks/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Tasks from '@/pages/tasks';
import { store } from '@/app/store';
import { Provider } from 'react-redux';
import { AppTreeType } from 'next/dist/shared/lib/utils';
import { useGetAllTasksQuery } from '@/app/services/tasksApi';

type TaskPageProps = {
dev: boolean;
Expand All @@ -21,7 +22,47 @@ jest.mock('next/router', () => ({
}),
}));

jest.mock('@/app/services/tasksApi', () => ({
useGetAllTasksQuery: jest.fn(),
}));

describe('Tasks', () => {
it('should display shimmer cards when isLoading is true and dev is true', () => {
(useGetAllTasksQuery as jest.Mock).mockReturnValue({
data: { tasks: [], next: '' },
isError: false,
isLoading: true,
isFetching: false,
});

render(
<Provider store={store()}>
<Tasks dev={true} />
</Provider>
);
const skeletonContainer = screen.getByTestId('task-skeleton-container');
expect(skeletonContainer).toBeInTheDocument();

const shimmerCards = screen.getAllByTestId('task-shimmer-card');
expect(shimmerCards).toHaveLength(5);
});

it('should display loading state when isLoading is true', () => {
(useGetAllTasksQuery as jest.Mock).mockReturnValue({
data: { tasks: [], next: '' },
isError: false,
isLoading: true,
isFetching: false,
});

render(
<Provider store={store()}>
<Tasks />
</Provider>
);
expect(screen.getByText('Loading...')).toBeInTheDocument();
});

it('should render the Tasks component', () => {
render(
<Provider store={store()}>
Expand Down
13 changes: 13 additions & 0 deletions src/components/tasks/TasksContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {

import useIntersection from '@/hooks/useIntersection';
import TaskSearchDev from './TaskSearch/TaskSearch';
import TaskCardShimmer from '../Loaders/taskCardShimmer';

export const TasksContent = ({ dev }: { dev?: boolean }) => {
const router = useRouter();
Expand Down Expand Up @@ -127,6 +128,18 @@ export const TasksContent = ({ dev }: { dev?: boolean }) => {
earlyReturn: loadedTasks[selectedTab].length === 0,
});

if (isLoading && dev)
return (
<div
className={styles.taskSkeletonContainer}
data-testid="task-skeleton-container"
>
{[...Array(5)].map((n: number, index) => (
<TaskCardShimmer key={index} />
))}
</div>
);

if (isLoading) return <p>Loading...</p>;

if (isError) return <p>{TASKS_FETCH_ERROR_MESSAGE}</p>;
Expand Down
11 changes: 11 additions & 0 deletions src/styles/tasks.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
width: 64%;
}

.taskSkeletonContainer {
padding: 2rem 0 0;
display: flex;
flex-direction: column;
align-items: center;

@media (max-width: 448px) {
padding: 2rem 1rem 0;
}
}

.container {
text-align: center;
display: flex;
Expand Down

0 comments on commit 1c06b65

Please sign in to comment.