Skip to content

Commit

Permalink
test: Add unit tests for Circle component
Browse files Browse the repository at this point in the history
  • Loading branch information
drikusroor committed Dec 8, 2023
1 parent e1ce6e7 commit 1a72343
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion frontend/src/components/Circle/Circle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from "react";
import Timer from "../../util/timer";
import Timer from "util/timer";

// Circle shows a counterclockwise circular animation
const Circle = ({
Expand Down
86 changes: 86 additions & 0 deletions frontend/src/components/Circle/Circle.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import Circle from './Circle';
import Timer from 'util/timer';

global.requestAnimationFrame = (callback) => {
setTimeout(callback, 0);
};
global.performance = {
now: () => Date.now()
};

jest.mock('util/timer', () => ({
__esModule: true,
default: jest.fn(),
}));

describe('Circle', () => {

afterEach(() => {
jest.clearAllMocks();
});

it('renders correctly with default props', () => {
const { container } = render(<Circle />);
expect(container.querySelector('.aha__circle')).toBeInTheDocument();
expect(container.querySelectorAll('circle').length).toBe(2);
});

it('calls onTick and onFinish callbacks', async () => {

Timer.mockImplementation(({ onTick, onFinish, duration }) => {
let time = 0;
const interval = 10; // Simulate a timer interval
const timerId = setInterval(() => {
time += interval;
if (onTick) {
onTick(time);
}
if (time >= duration) {
if (onFinish) {
onFinish();
}
clearInterval(timerId);
}
}, interval);
return () => clearInterval(timerId);
});

const onTick = jest.fn();
const onFinish = jest.fn();
render(<Circle onTick={onTick} onFinish={onFinish} running={true} duration={100} />);

await waitFor(() => expect(onTick).toHaveBeenCalled());
await waitFor(() => expect(onFinish).toHaveBeenCalled());
});

it('starts timer when running is true', () => {
const startTime = 0;
const duration = 0;
render(<Circle startTime={startTime} duration={duration} running={true} />);

expect(Timer).toHaveBeenCalled();
});

it('calculates style for circle animation correctly', () => {
const time = 50;
const duration = 100;
const { container } = render(<Circle time={time} duration={duration} running={true} animateCircle={true} />);

const percentageCircle = container.querySelector('.circle-percentage');

expect(percentageCircle).toHaveStyle('stroke-dashoffset: 0.5340707511102648;');
});

it('does not start timer when running is false', () => {
const onTick = jest.fn();
const onFinish = jest.fn();
render(<Circle running={false} onTick={onTick} onFinish={onFinish} duration={100} />);

expect(Timer).not.toHaveBeenCalled();
expect(onTick).not.toHaveBeenCalled();
expect(onFinish).not.toHaveBeenCalled();
});

});

0 comments on commit 1a72343

Please sign in to comment.