Skip to content

Commit

Permalink
test: add tests for FocusContext
Browse files Browse the repository at this point in the history
Introduce comprehensive test cases for FocusContext including focus setting, clearing, and context usage. Ensure correct behavior of focus manipulation and expected errors when used outside FocusProvider.
  • Loading branch information
mallikcheripally committed Aug 8, 2024
1 parent 20c2a2b commit 62f8983
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/context/FocusContext.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { useRef } from 'react';
import { render, fireEvent, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import { FocusProvider, useFocusContext } from '@/context/FocusContext';

const TestComponent: React.FC = () => {
const { focusedElement, setFocusedElement, focusElement } = useFocusContext();
const inputRef1 = useRef<HTMLInputElement>(null);
const inputRef2 = useRef<HTMLInputElement>(null);

return (
<div>
<input ref={inputRef1} data-testid="test-input-1" />
<input ref={inputRef2} data-testid="test-input-2" />
<button onClick={() => focusElement(inputRef1.current!)}>Focus Input 1</button>
<button onClick={() => focusElement(inputRef2.current!)}>Focus Input 2</button>
<button onClick={() => setFocusedElement(null)}>Clear Focus</button>
<span data-testid="focused-element">{focusedElement ? focusedElement.tagName : 'None'}</span>
</div>
);
};

describe('FocusContext', () => {
it('provides context values to children', () => {
render(
<FocusProvider>
<TestComponent />
</FocusProvider>,
);

const input1 = screen.getByTestId('test-input-1') as HTMLInputElement;
const input2 = screen.getByTestId('test-input-2') as HTMLInputElement;
const focusButton1 = screen.getByText('Focus Input 1');
const focusButton2 = screen.getByText('Focus Input 2');
const focusedElementDisplay = screen.getByTestId('focused-element');

expect(focusedElementDisplay).toHaveTextContent('None');

fireEvent.click(focusButton1);
expect(document.activeElement).toBe(input1);
expect(focusedElementDisplay).toHaveTextContent('INPUT');

const blurSpy = jest.spyOn(input1, 'blur');

fireEvent.click(focusButton2);
expect(document.activeElement).toBe(input2);
expect(focusedElementDisplay).toHaveTextContent('INPUT');
expect(blurSpy).toHaveBeenCalled();

fireEvent.click(screen.getByText('Clear Focus'));
expect(focusedElementDisplay).toHaveTextContent('None');
});

it('throws an error if useFocusContext is used outside of FocusProvider', () => {
const renderWithoutProvider = () =>
render(
<div>
<TestComponent />
</div>,
);

expect(renderWithoutProvider).toThrow('useFocusContext must be used within a FocusProvider');
});
});

0 comments on commit 62f8983

Please # to comment.