Skip to content

Commit

Permalink
[#10] Update unittest for components
Browse files Browse the repository at this point in the history
  • Loading branch information
manh-t committed Jul 4, 2023
1 parent 517d8c0 commit a8c3fa8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/components/Dashboard/Content/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

import { render, screen } from '@testing-library/react';

import DashboardContent from '.';

describe('DashboardContent', () => {
const dataTestId = 'dashboard-content';
it('renders DashboardContent and its components', () => {
render(<DashboardContent data-test-id={dataTestId} />);

const dashboardContent = screen.getByTestId(dataTestId);

expect(dashboardContent).toBeVisible();
});
});
8 changes: 6 additions & 2 deletions src/components/Dashboard/Content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import React from 'react';

import Shimmer from 'components/Shimmer';

const DashboardContent = (): JSX.Element => {
interface DashboardContentProps {
'data-test-id'?: string;
}

const DashboardContent = ({ ...attributes }: DashboardContentProps): JSX.Element => {
return (
<div className="flex flex-col w-full">
<div className="flex flex-col w-full" {...attributes}>
<Shimmer classAttributes="h-[302px] rounded-[12px]" />
<div className="flex flex-row justify-between mt-[38px]">
<div className="flex flex-col justify-between">
Expand Down
30 changes: 26 additions & 4 deletions src/components/Dashboard/Header/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import DashboardHeader from '.';

describe('DashboardHeader', () => {
const dataTestId = 'dashboard-header';
it('renders DashboardHeader and its components', () => {
it('renders DashboardHeader and its components without shimmers', () => {
const dateTime = 'Monday, JUNE 15';
const daysAgo = 'Today';
const profileUrl = 'test url';
render(
<DashboardHeader dateTime={dateTime} daysAgo={daysAgo} profileUrl={profileUrl} data-test-id={dataTestId}>
<DashboardHeader
dateTime={dateTime}
daysAgo={daysAgo}
profileUrl={profileUrl}
data-test-id={dataTestId}
shouldShowShimmer={false}
>
Dashboard Header
</DashboardHeader>
);
Expand All @@ -21,10 +27,26 @@ describe('DashboardHeader', () => {

expect(dashboardHeader).toBeVisible();
expect(dashboardHeader).toHaveTextContent(dateTime);
expect(dashboardHeader).toHaveTextContent(dateTime);
expect(dashboardHeader).toHaveTextContent(dateTime);
expect(dashboardHeader).toHaveTextContent(daysAgo);

expect(avatar).toBeVisible();
expect(avatar).toHaveAttribute('src', profileUrl);
});

it('does NOT renders text components', () => {
const dateTime = 'Monday, JUNE 15';
const daysAgo = 'Today';
const profileUrl = 'test url';
render(
<DashboardHeader dateTime={dateTime} daysAgo={daysAgo} profileUrl={profileUrl} data-test-id={dataTestId}>
Dashboard Header
</DashboardHeader>
);

const dashboardHeader = screen.getByTestId(dataTestId);

expect(dashboardHeader).toBeVisible();
expect(dashboardHeader).not.toHaveTextContent(dateTime);
expect(dashboardHeader).not.toHaveTextContent(daysAgo);
});
});
18 changes: 18 additions & 0 deletions src/components/Shimmer/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';

import { render, screen } from '@testing-library/react';

import Shimmer from '.';

describe('Shimmer', () => {
const dataTestId = 'shimmer';
it('renders Shimmer and its components', () => {
const testClass = 'test-class';
render(<Shimmer classAttributes={testClass} data-test-id={dataTestId} />);

const shimmer = screen.getByTestId(dataTestId);

expect(shimmer).toBeVisible();
expect(shimmer).toHaveClass(testClass);
});
});
4 changes: 3 additions & 1 deletion src/components/Shimmer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import React from 'react';

interface ShimmerProps {
classAttributes: string;
'data-test-id'?: string;
}

// Ref: https://delba.dev/blog/animated-loading-skeletons-with-tailwind
const Shimmer = ({ classAttributes }: ShimmerProps): JSX.Element => {
const Shimmer = ({ classAttributes, ...attributes }: ShimmerProps): JSX.Element => {
return (
<div
className={`relative before:absolute before:inset-0 before:-translate-x-full before:animate-[shimmer_2s_infinite] before:bg-gradient-to-r before:from-transparent before:from-30% before:via-white/50 before:to-transparent before:to-70% isolate overflow-hidden shadow-xl shadow-black/5 ${classAttributes}`}
{...attributes}
>
<div className={`bg-white bg-opacity-[.12] ${classAttributes}`}></div>
</div>
Expand Down

0 comments on commit a8c3fa8

Please sign in to comment.