Skip to content

Commit

Permalink
Component tests for DetailElement
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswilty committed Mar 20, 2024
1 parent 9917115 commit e1eb6c3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
4 changes: 0 additions & 4 deletions frontend/src/components/ThemedButtons/DetailElement.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
background-color: var(--main-scrollbar-hover-colour);
}

.details-button[aria-expanded='false'] ~ .details-content {
display: none;
}

.details-button .button-arrow-icon {
display: inline-block;
width: 1.5rem;
Expand Down
49 changes: 49 additions & 0 deletions frontend/src/components/ThemedButtons/DetailElement.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { ReactNode } from 'react';
import { describe, expect, test } from 'vitest';

import DetailElement from './DetailElement';

const renderComponent = (content: ReactNode, buttonText?: string) => {

Check failure on line 8 in frontend/src/components/ThemedButtons/DetailElement.test.tsx

View workflow job for this annotation

GitHub Actions / lint-format (18.x)

Expected a function declaration
const user = userEvent.setup();
render(
<DetailElement useIcon={true} buttonText={buttonText}>
{content}
</DetailElement>
);

return { user };
};

const detailButton = (name: string = 'Details') => screen.getByRole('button', { name });

Check failure on line 19 in frontend/src/components/ThemedButtons/DetailElement.test.tsx

View workflow job for this annotation

GitHub Actions / lint-format (18.x)

Expected a function declaration

Check failure on line 19 in frontend/src/components/ThemedButtons/DetailElement.test.tsx

View workflow job for this annotation

GitHub Actions / lint-format (18.x)

Type string trivially inferred from a string literal, remove type annotation

describe('DetailElement component tests', () => {
test('Renders collapsed initially with Details button by default', () => {
const textContent = 'Well hello there';
renderComponent(<>{textContent}</>);

const button = detailButton();
expect(button).toBeInTheDocument();
expect(button).toHaveAttribute('aria-expanded', 'false');
expect(screen.getByText(textContent)).not.toBeVisible();
});

test('Expands on clicking Details button', async () => {
const textContent = 'Well hello there';
const { user } = renderComponent(<>{textContent}</>);

const button = detailButton();
await user.click(button);

expect(button).toHaveAttribute('aria-expanded', 'true');
expect(screen.getByText(textContent)).toBeVisible();
});

test('Button can have a custom name', () => {
const buttonText = 'This is my button, there are many others like it but this one is mine';
renderComponent(<>without my button i am nothing</>, buttonText);

expect(detailButton(buttonText)).toBeInTheDocument();
});
});
4 changes: 3 additions & 1 deletion frontend/src/components/ThemedButtons/DetailElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function DetailElement({
)}
{buttonText}
</button>
<div className="details-content">{children}</div>
<div className="details-content" style={{ display: isExpanded ? 'block' : 'none'}}>
{children}
</div>
</>
);
}
Expand Down

0 comments on commit e1eb6c3

Please sign in to comment.