Skip to content

Commit

Permalink
Merge pull request #1126 from Amsterdam-Music-Lab/ts/convert-button-ts
Browse files Browse the repository at this point in the history
Refactor: Convert Button component & audio utility to TypeScript
  • Loading branch information
drikusroor authored Jun 18, 2024
2 parents 280afae + 504450c commit 3453cbc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 51 deletions.
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.

0 comments on commit 3453cbc

Please sign in to comment.