Skip to content

Commit 7aee577

Browse files
feat: refactor tests
1 parent 6984b24 commit 7aee577

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

src/courseware/course/sequence/sequence-navigation/SequenceNavigationTabs.test.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('Sequence Navigation Tabs', () => {
5454
const booyah = render(<SequenceNavigationTabs {...mockData} />, { wrapWithRouter: true });
5555

5656
// wait for links to appear so we aren't testing an empty div
57-
await screen.findAllByRole('link');
57+
await screen.findAllByRole('tabpanel');
5858

5959
container = booyah.container;
6060

src/generic/hooks.test.jsx

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,15 @@ describe('Hooks', () => {
7575
expect(targetContent.focus).toHaveBeenCalled();
7676
});
7777

78-
test('should trigger on "Enter" key', async () => {
79-
render(<TestComponent />);
80-
81-
const skipLink = screen.getByRole('link', { name: /skip to content/i });
82-
const targetContent = screen.getByTestId('target-content');
83-
84-
targetContent.focus = jest.fn();
85-
86-
skipLink.focus();
87-
88-
await userEvent.keyboard('{Enter}');
89-
90-
await waitFor(() => {
91-
expect(global.scrollTo).toHaveBeenCalledWith({
92-
top: expect.any(Number), behavior: 'smooth',
93-
});
94-
});
95-
expect(targetContent.focus).toHaveBeenCalled();
96-
});
97-
9878
test('should warn if element is not focusable', async () => {
9979
render(<TestComponent />);
10080

101-
const skipLink = screen.getByRole('link', { name: /skip to content/i });
81+
const skipLink = screen.getByTestId('skip-link');
10282
const targetContent = screen.getByTestId('target-content');
10383

104-
jest.spyOn(targetContent, 'focus').mockImplementationOnce(() => {
105-
throw new Error('focus is not a function');
84+
Object.defineProperty(targetContent, 'focus', {
85+
value: undefined,
86+
configurable: true,
10687
});
10788

10889
await userEvent.click(skipLink);
@@ -119,13 +100,20 @@ describe('Hooks', () => {
119100
});
120101

121102
test('should warn if target element is not found', async () => {
122-
render(<TestComponent />);
103+
const ComponentWithoutTarget = () => {
104+
useScrollToContent();
105+
return (
106+
<>
107+
<a href="#main-content" data-testid="skip-link">Skip to content</a>
108+
{/* Нет #main-content */}
109+
</>
110+
);
111+
};
112+
113+
render(<ComponentWithoutTarget />);
123114

124115
const skipLink = screen.getByRole('link', { name: /skip to content/i });
125116

126-
const targetElement = document.getElementById('main-content');
127-
targetElement?.parentNode?.removeChild(targetElement);
128-
129117
await userEvent.click(skipLink);
130118

131119
await waitFor(() => {

0 commit comments

Comments
 (0)