Skip to content

Commit

Permalink
feat: adding unit tests for dashboard folder (#1735)
Browse files Browse the repository at this point in the history
  • Loading branch information
reachaadrika authored Jul 11, 2023
1 parent 7642c6a commit df1b59d
Show file tree
Hide file tree
Showing 14 changed files with 269 additions and 9 deletions.
1 change: 1 addition & 0 deletions components/buttons/GithubButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function GithubButton({
iconPosition={iconPosition}
target={target}
className={className}
data-testid="Github-button"
bgClassName="bg-gray-800 hover:bg-gray-700"
buttonSize={ inNav == "true" ? "small" : "default" }
/>
Expand Down
1 change: 1 addition & 0 deletions components/buttons/SlackButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function SlackButton({
iconPosition={iconPosition}
target={target}
className={className}
data-testid="Slack-button"
bgClassName="bg-slack hover:bg-slack-light"
/>
);
Expand Down
5 changes: 3 additions & 2 deletions components/dashboard/GoodFirstIssues.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ function GoodFirstIssues({ issues }) {
return (
<Table
title={
<div className="flex gap-3">
<div className="flex gap-3" data-testid="GoodFirstIssues-main-div">
<span>Good First Issues</span>
<GoodFirstIssuesTip />
<Filters
className="ml-auto"
data-testid="GoodFirstIssues-filter-component"
issues={filteredIssues}
allIssues={issues}
setSelectedRepo={setSelectedRepo}
setSelectedArea={setSelectedArea}
selectedArea={selectedArea}
selectedRepo={selectedRepo}
selectedRepo={selectedRepo}
/>
</div>
}
Expand Down
1 change: 1 addition & 0 deletions components/dashboard/GoodFirstIssuesTip.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function GoodFirstIssuesTip() {
onMouseEnter={() => setOpen(true)}
ref={reference}
src="/img/illustrations/icons/tip-icon.svg"
data-testid="GoodFirstIssuesTip-hover-icon"
/>

{open && (
Expand Down
4 changes: 2 additions & 2 deletions components/dashboard/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default function Header() {
<div className="sm:flex sm:justify-between" id="main-content">
<div className="lg:flex lg:justify-between">
<div className="max-w-xl">
<h2 className="text-4xl leading-10 font-extrabold text-gray-900 sm:text-4xl sm:leading-none sm:tracking-tight">
<h2 className="text-4xl leading-10 font-extrabold text-gray-900 sm:text-4xl sm:leading-none sm:tracking-tight" data-testid="Header-heading">
Dashboard
</h2>
<p className="mt-5 text-xl leading-7 text-gray-700">
<p className="mt-5 text-xl leading-7 text-gray-700" data-testid="Header-paragraph">
Visualize our progress. Get involved.{' '}
</p>
</div>
Expand Down
7 changes: 5 additions & 2 deletions components/dashboard/table/Filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default function Filters({
placement: 'left-start',
open,
});

const wrapperRef = useRef(null);
useOutsideAlerter(wrapperRef, setOpen);
const areas = allIssues.map((issue) => issue.area);
Expand All @@ -60,8 +61,10 @@ export default function Filters({
alt='filter menu'
src="/img/illustrations/icons/filters-icon.svg"
className={`cursor-pointer ${className}`}
data-testid="Filters-img-container"
/>
<div ref={wrapperRef}>

<div ref={wrapperRef} >
{open && (
<div
ref={floating}
Expand All @@ -70,7 +73,7 @@ export default function Filters({
top: y ?? '',
left: x ?? '',
}}
>
data-testid="Filter-menu">
<div className="bg-white w-96 shadow-xl rounded">
<div className="flex p-4">
<h4 className="text-base">Filter Issues</h4>
Expand Down
4 changes: 4 additions & 0 deletions components/dashboard/table/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,27 @@ const Pagination = ({ issuesPerPage, currentPage, totalIssues, paginate }) => {
<div className="inline-flex mt-2 xs:mt-0">
<Button
text="Prev"
data-testid="Pagination-prev-button"
onClick={(event) => {
event.preventDefault();
if (currentPage - 1) paginate(currentPage - 1);
}}
/>
{pageNumbers.map((number) => (
<Button
data-testid={`Pagination-page-button-${number}`}
key={number}
text={number}
onClick={(event) => {
event.preventDefault();
paginate(number);
}}

/>
))}
<Button
text="Next"
data-testid="Pagination-next-button"
onClick={(event) => {
event.preventDefault();
if (currentPage < totalIssues / issuesPerPage)
Expand Down
7 changes: 4 additions & 3 deletions components/dashboard/table/Row.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,22 @@ export default function Row({ item }) {
? '/img/illustrations/icons/pull-request.svg'
: '/img/illustrations/icons/issue.svg'
}
data-testid="Row-img-issue"
/>
<a
target='_blank' rel='noreferrer'
className="text-gray-900 text-sm lowercase font-light"
href={`https://github.com/${item.repo}`}
>
data-testid="Row-github-redirect">
{item.repo}
</a>
</div>

<span className="text-base font-medium text-gray-900 w-full leading-5 two-liner">
<span className="text-base font-medium text-gray-900 w-full leading-5 two-liner" data-testid="Row-spanText">
{item.title}
</span>

{item.labels.length > 0 && (
{item.labels && item?.labels?.length > 0 && (
<div className="flex flex-wrap items-center gap-1">
{item.labels.map((label) => (
<span
Expand Down
48 changes: 48 additions & 0 deletions cypress/test/dashboard/GoodFirstIssues.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { mount } from '@cypress/react';
import Table from '../../../components/dashboard/table/Table';
import GoodFirstIssues from '../../../components/dashboard/GoodFirstIssues';

describe('GoodFirstIssues Component', () => {
const issues = [
{ id: 1, repo: 'Repository 1', area: 'Area 1' },
{ id: 2, repo: 'Repository 2', area: 'Area 2' },
{ id: 3, repo: 'Repository 3', area: 'Area 3' },

];

it('renders the GoodFirstIssues component', () => {
mount(<GoodFirstIssues issues={issues} />);
// Assert that the component is rendered successfully
cy.get('[data-testid="GoodFirstIssues-main-div"]').should('exist');
cy.get(Table).should('exist');
});

it('filters issues based on selected repository', () => {
mount(<GoodFirstIssues issues={issues} />);
// Select a specific repository , Repo 1 for now
const selectedRepo = 'Repository 1';

mount(<GoodFirstIssues issues={issues} />);

// Select the mentioned repository
cy.get('select[name="selectedRepo"]').select(selectedRepo);

// check that the selected repository is displayed
cy.contains(selectedRepo).should('exist');

//check if Filter component renders
cy.get('[data-testid="GoodFirstIssues-filter-component"]').should('exist');

// check no other repo is displayed
cy.get('select[name="selectedRepo"] option').should('not.contain', 'Repository 2').and('not.contain', 'Repository 3')


});

it('filters issues based on selected area', () => {
mount(<GoodFirstIssues issues={issues} />);
});


});
30 changes: 30 additions & 0 deletions cypress/test/dashboard/GoodFirstIssuesTip.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { mount } from 'cypress/react';
import GoodFirstIssuesTip from '../../../components/dashboard/GoodFirstIssuesTip';

describe('GoodFirstIssuesTip', () => {
beforeEach(() => {
mount(<GoodFirstIssuesTip />);
});

it('toggles the tip when hovering over the icon', () => {
cy.get('[data-testid="GoodFirstIssuesTip-hover-icon"]').trigger('mouseenter');
cy.get('[data-testid="GoodFirstIssuesTip-hover-icon"]').trigger('mouseleave');

});

it('renders the tip content with the correct text', () => {
cy.get('[data-testid="GoodFirstIssuesTip-hover-icon"]').trigger('mouseenter');
//checking for the available class here
cy.get('.bg-white').should('have.css', 'visibility', 'visible');

cy.contains('Is this your first contribution?').should('exist');
cy.contains('The issues in this column are perfect for you!').should(
'exist'
);
cy.contains(
'These issues are of low-complexity and should be a quick commit.'
).should('exist');
cy.contains('Thanks for your help, and welcome!').should('exist');
});
});
31 changes: 31 additions & 0 deletions cypress/test/dashboard/Header.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { mount } from 'cypress/react';
import Header from '../../../components/dashboard/Header';
import Button from '../../../components/buttons/Button';
import GithubButton from '../../../components/buttons/GithubButton';
import SlackButton from '../../../components/buttons/SlackButton';

describe('Header', () => {
beforeEach(() => {
mount(
<Header
ButtonComponent={Button}
GithubButtonComponent={() => <GithubButton size="small" />}
SlackButtonComponent={SlackButton}
/>
);
});

it('renders the header correctly', () => {
cy.get('[data-testid="Header-heading"]').should('contain', 'Dashboard');
cy.get('[data-testid="Header-paragraph"]').should(
'contain',
'Visualize our progress. Get involved.'
);
cy.get('[data-testid="Github-button"]').should('exist');
cy.get('[data-testid="Slack-button"]').should('exist');
cy.contains('Contribution Guide')
.should('have.attr', 'href', 'https://github.com/asyncapi?type=source#-contribute-to-asyncapi')
.should('have.attr', 'target', '_blank');
});
});
44 changes: 44 additions & 0 deletions cypress/test/dashboard/table/Filters.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { mount } from 'cypress/react'
import Filters from '../../../../components/dashboard/table/Filters';


describe('Filters', () => {
const sampleIssues = [
{ area: 'Area 1', repo: 'Repo 1' },
{ area: 'Area 2', repo: 'Repo 1' },
{ area: 'Area 1', repo: 'Repo 2' },
{ area: 'Area 2', repo: 'Repo 2' },
];

it('displays the filter menu correctly', () => {

mount(
<Filters
className="test-class"
issues={sampleIssues}
allIssues={sampleIssues}
selectedArea="Area 1"
selectedRepo="Repo 1"
/>
);

// Click on the filter icon to open the menu
cy.get('[data-testid="Filters-img-container"]').click();

// Verify that the menu is displayed
cy.get('div[data-testid="Filter-menu"]').should('be.visible');

// Verify the heading "Filter Issues"
cy.contains('h4', 'Filter Issues').should('be.visible');

// Verify the filter options
cy.contains('BY REPOSITORY').should('be.visible');
cy.contains('BY AREA').should('be.visible');

// Verify that the menu is closed
cy.get('div[data-testid="Filter-menu"]').should('exist');

});
});

53 changes: 53 additions & 0 deletions cypress/test/dashboard/table/Row.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react';
import { mount } from 'cypress/react';
import Row from '../../../../components/dashboard/table/Row';

describe('Row component', () => {
const item = {
resourcePath: 'asyncapi',
isPR: false,
repo: 'asyncapi',
title: 'Example Issue',
labels: [
{ name: 'bug' },
{ name: 'feature' },
],
};

it('renders the component correctly', () => {
mount(<Row item={item} />);

// Assert that the component is rendered
cy.get('li').should('exist');
cy.get('.p-4').should('exist');
cy.get('a').should('have.attr', 'href', 'https://github.com/asyncapi');

// Assert the content within the component

cy.get('.text-base').should('have.text', 'Example Issue');
cy.get('.flex-wrap > :nth-child(1)').should('have.text', 'bug');
cy.get('.flex-wrap > :nth-child(2)').should('have.text', 'feature');
});

it('opens the link in a new tab', () => {
mount(<Row item={item} />);

// Assert that the link opens in a new tab
cy.get('[data-testid="Row-github-redirect"]').should('have.attr', 'target', '_blank');
cy.get('[data-testid="Row-github-redirect"]').should('have.attr', 'rel', 'noreferrer');
});

it('renders the correct icon based on isPR prop', () => {
item.isPR = true;
mount(<Row item={item} />);

// Assert that the correct icon is rendered for a PR
cy.get('[data-testid="Row-img-issue"]').should('have.attr', 'src', '/img/illustrations/icons/issue.svg');

item.isPR = false;
mount(<Row item={item} />);

// Assert that the correct icon is rendered for an issue
cy.get('[data-testid="Row-img-issue"]').should('have.attr', 'src', '/img/illustrations/icons/issue.svg');
});
});
42 changes: 42 additions & 0 deletions cypress/test/dashboard/table/Table.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { mount } from 'cypress/react';
import Table from '../../../../components/dashboard/table/Table';

describe('Table Component', () => {
const data = [
{ id: 1, title: 'Item 1' },
{ id: 2, title: 'Item 2' },
{ id: 3, title: 'Item 3' },
];

it('renders the component', () => {
mount(
<Table
title="My Table"
data={data}
className="test-class"
listClassName="test-list-class"
/>
);

cy.contains('h2', 'My Table').should('be.visible');
});

it('renders the correct item names', () => {
mount(
<Table
title="My Table"
data={data}
className="test-class"
listClassName="test-list-class"
/>
);

cy.get('.test-list-class').should('be.visible');

cy.get('.test-list-class [data-testid="Row-spanText"]').each((row, index) => {
cy.wrap(row).should('contain', data[index].title);
});
});


})

0 comments on commit df1b59d

Please sign in to comment.