Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Convert Button component & audio utility to TypeScript #1126

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions frontend/src/components/Button/Button.test.jsx

This file was deleted.

48 changes: 48 additions & 0 deletions frontend/src/components/Button/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import Button from './Button';
import { describe, expect, it, vi } from 'vitest';

describe('Button component', () => {

it('renders correctly', () => {
const mockOnClick = vi.fn();
const { getByText } = render(<Button title="Test Button" onClick={mockOnClick} />);
const buttonElement = getByText('Test Button');

expect(document.body.contains(buttonElement)).toBe(true);
});

it('calls onClick handler only once', () => {
const mockOnClick = vi.fn();
const { getByText } = render(<Button title="Test Button" onClick={mockOnClick} />);

const button = getByText('Test Button');
fireEvent.click(button);
fireEvent.click(button); // second click

expect(mockOnClick).toHaveBeenCalledTimes(1);
});

it('does not call onClick when disabled is true', () => {
const mockOnClick = vi.fn();
const { getByText } = render(<Button title="Test Button" onClick={mockOnClick} disabled={true} />);

const button = getByText('Test Button');
fireEvent.click(button);

expect(mockOnClick).not.toHaveBeenCalled();
});

it('applies custom class and style', () => {
const mockOnClick = vi.fn();
const style = { backgroundColor: 'blue' };
const { getByText } = render(<Button title="Test Button" className="custom-class" style={style} onClick={mockOnClick} />);

const button = getByText('Test Button');

expect(button.classList.contains('custom-class')).toBe(true);
expect(button.style.backgroundColor).toBe('blue');
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import React, { useRef } from "react";
import classNames from "classnames";
import { audioInitialized } from "../../util/audio";

interface ButtonProps {
title: string;
onClick: (value: string) => void;
className?: string;
padding?: string;
style?: React.CSSProperties;
disabled?: boolean;
value?: string;
}

// Button is a button that can only be clicked one time
const Button = ({
title,
Expand All @@ -11,7 +21,7 @@ const Button = ({
style = {},
disabled = false,
value = "",
}) => {
}: ButtonProps) => {
const clicked = useRef(false);

// Only handle the first click
Expand All @@ -28,12 +38,12 @@ const Button = ({
// Without the browser having registered any user interaction (e.g. click)
const touchEvent = audioInitialized
? {
onTouchStart: (e) => {
e.stopPropagation();
clickOnce();
return false;
},
}
onTouchStart: (e) => {
e.stopPropagation();
clickOnce();
return false;
},
}
: {};

return (
Expand Down
File renamed without changes.
Loading