Skip to content

Commit

Permalink
Extract check item to individual component
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Nov 8, 2022
1 parent 0e62677 commit 470b64c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 94 deletions.
66 changes: 9 additions & 57 deletions assets/js/components/ChecksCatalog/CatalogContainer.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import React from 'react';

import { Disclosure, Transition } from '@headlessui/react';

import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';

import NotificationBox from '@components/NotificationBox';
import LoadingBox from '@components/LoadingBox';

import { groupBy } from '@lib/lists';

import { EOS_ERROR } from 'eos-icons-react';

import CheckItem from './CheckItem';

const CatalogContainer = ({
onRefresh = () => {},
catalogData = [],
Expand Down Expand Up @@ -59,58 +56,13 @@ const CatalogContainer = ({
</div>
<ul role="list" className="divide-y divide-gray-200">
{checks.map((check) => (
<li key={check.id}>
<Disclosure>
<Disclosure.Button
as="div"
className="flex justify-between w-full cursor-pointer hover:bg-gray-100"
>
<div className="check-row px-4 py-4 sm:px-6">
<div className="flex items-center">
<p className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
{check.id}
</p>
{check.premium > 0 && (
<p className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
Premium
</p>
)}
</div>
<div className="mt-2 sm:flex sm:justify-between">
<div className="sm:flex">
<ReactMarkdown
className="markdown text-sm"
remarkPlugins={[remarkGfm]}
>
{check.description}
</ReactMarkdown>
</div>
</div>
</div>
</Disclosure.Button>
<Transition
enter="transition duration-100 ease-out"
enterFrom="transform opacity-0"
enterTo="transform opacity-100"
leave="transition duration-100 ease-out"
leaveFrom="transform opacity-100"
leaveTo="transform opacity-0"
>
<Disclosure.Panel className="check-panel border-none">
<div className="px-8 py-4 sm:px-8">
<div className="px-4 py-4 sm:px-4 bg-slate-100 rounded">
<ReactMarkdown
className="markdown"
remarkPlugins={[remarkGfm]}
>
{check.remediation}
</ReactMarkdown>
</div>
</div>
</Disclosure.Panel>
</Transition>
</Disclosure>
</li>
<CheckItem
key={check.id}
checkID={check.id}
premium={check.premium}
description={check.description}
remediation={check.remediation}
/>
))}
</ul>
</div>
Expand Down
35 changes: 0 additions & 35 deletions assets/js/components/ChecksCatalog/CatalogContainer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';

import { screen, within } from '@testing-library/react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';

import { faker } from '@faker-js/faker';
import { renderWithRouter } from '@lib/test-utils';
Expand Down Expand Up @@ -55,38 +54,4 @@ describe('ChecksCatalog CatalogContainer component', () => {
expect(checks.length).toBe(5);
}
});

it('should show check remediation when the row is clicked', () => {
const groupName = faker.animal.cat();
const catalogChecks = catalogCheckFactory.buildList(2, {
group: groupName,
});
const checkRemediation1 = catalogChecks[0].remediation;
const checkRemediation2 = catalogChecks[1].remediation;
const catalog = catalogChecks;

renderWithRouter(
<CatalogContainer
loading={false}
catalogError={null}
catalogData={catalog}
/>
);

const groups = screen.getAllByRole('list');
const { getAllByRole } = within(groups[0]);
let checks = getAllByRole('listitem');
const check1 = checks[0].querySelector('div');
const check2 = checks[1].querySelector('div');

expect(screen.queryByText(checkRemediation1)).not.toBeInTheDocument();
userEvent.click(check1);
expect(screen.getByText(checkRemediation1)).toBeVisible();
userEvent.click(check1);
expect(screen.queryByText(checkRemediation1)).not.toBeInTheDocument();

expect(screen.queryByText(checkRemediation2)).not.toBeInTheDocument();
userEvent.click(check2);
expect(screen.getByText(checkRemediation2)).toBeVisible();
});
});
62 changes: 62 additions & 0 deletions assets/js/components/ChecksCatalog/CheckItem.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';

import { Disclosure, Transition } from '@headlessui/react';

import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';

const CheckItem = ({ checkID, premium = false, description, remediation }) => {
return (
<li>
<Disclosure>
<Disclosure.Button
as="div"
className="flex justify-between w-full cursor-pointer hover:bg-gray-100"
>
<div className="check-row px-4 py-4 sm:px-6">
<div className="flex items-center">
<p className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
{checkID}
</p>
{premium > 0 && (
<p className="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
Premium
</p>
)}
</div>
<div className="mt-2 sm:flex sm:justify-between">
<div className="sm:flex">
<ReactMarkdown
className="markdown text-sm"
remarkPlugins={[remarkGfm]}
>
{description}
</ReactMarkdown>
</div>
</div>
</div>
</Disclosure.Button>
<Transition
enter="transition duration-100 ease-out"
enterFrom="transform opacity-0"
enterTo="transform opacity-100"
leave="transition duration-100 ease-out"
leaveFrom="transform opacity-100"
leaveTo="transform opacity-0"
>
<Disclosure.Panel className="check-panel border-none">
<div className="px-8 py-4 sm:px-8">
<div className="px-4 py-4 sm:px-4 bg-slate-100 rounded">
<ReactMarkdown className="markdown" remarkPlugins={[remarkGfm]}>
{remediation}
</ReactMarkdown>
</div>
</div>
</Disclosure.Panel>
</Transition>
</Disclosure>
</li>
);
};

export default CheckItem;
66 changes: 66 additions & 0 deletions assets/js/components/ChecksCatalog/CheckItem.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';

import { screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';

import { renderWithRouter } from '@lib/test-utils';
import { catalogCheckFactory } from '@lib/test-utils/factories';

import CheckItem from './CheckItem';

describe('ChecksCatalog CheckItem component', () => {
it('should show check information', () => {
const check = catalogCheckFactory.build();

renderWithRouter(
<CheckItem
key={check.id}
checkID={check.id}
description={check.description}
remediation={check.remediation}
/>
);

expect(screen.getByText(check.id)).toBeVisible();
expect(screen.getByText(check.description)).toBeVisible();
});

it('should show premium badge if the check is premium', () => {
const check = catalogCheckFactory.build();

renderWithRouter(
<CheckItem
key={check.id}
checkID={check.id}
premium={true}
description={check.description}
remediation={check.remediation}
/>
);

expect(screen.getByText('Premium')).toBeVisible();
});

it('should show check remediation when the row is clicked', () => {
const check = catalogCheckFactory.build();

renderWithRouter(
<CheckItem
key={check.id}
checkID={check.id}
description={check.description}
remediation={check.remediation}
/>
);

const checks = screen.getAllByRole('listitem');
const checkDiv = checks[0].querySelector('div');

expect(screen.queryByText(check.remediation)).not.toBeInTheDocument();
userEvent.click(checkDiv);
expect(screen.getByText(check.remediation)).toBeVisible();
userEvent.click(checkDiv);
expect(screen.queryByText(check.remediation)).not.toBeInTheDocument();
});
});
3 changes: 1 addition & 2 deletions assets/js/trento.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import ClusterDetails from '@components/ClusterDetails';
import DatabasesOverview from '@components/DatabasesOverview';
import SapSystemDetails from './components/SapSystemDetails/SapSystemDetails';
import DatabaseDetails from './components/DatabaseDetails';
import ChecksCatalog from '@components/ChecksCatalog';
import { ChecksCatalogNew } from '@components/ChecksCatalog';
import ChecksCatalog, { ChecksCatalogNew } from '@components/ChecksCatalog';
import NotFound from '@components/NotFound';
import SomethingWentWrong from '@components/SomethingWentWrong';
import Settings from '@components/Settings';
Expand Down

0 comments on commit 470b64c

Please sign in to comment.