Skip to content

Commit 7d413cf

Browse files
committed
test: improve test coverage for Outline
1 parent d55153d commit 7d413cf

1 file changed

Lines changed: 40 additions & 25 deletions

File tree

‎packages/frontend/test/unit/components/RightPane/Outline.test.tsx‎

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from 'react';
33
import { Provider } from 'react-redux';
44
import configureStore from 'redux-mock-store';
55
import { thunk } from 'redux-thunk';
6+
import useIsMobile from '../../../../src/hooks/useIsMobile';
67
import Outline from '../../../../src/components/RightPane/Outline';
78
import { fetchOutline } from '../../../../src/store/slices/outlineSlice';
89

@@ -13,10 +14,22 @@ jest.mock('../../../../src/store/slices/outlineSlice', () => ({
1314
fetchOutline: jest.fn(() => ({ type: 'outline/fetchOutline/pending' })),
1415
}));
1516

17+
jest.mock('../../../../src/hooks/useIsMobile', () => ({
18+
__esModule: true,
19+
default: jest.fn(),
20+
}));
21+
1622
describe('Outline', () => {
1723
let store;
1824

25+
const renderOutline = (props = {}) => render(
26+
<Provider store={store}>
27+
<Outline filePath="/test.md" onItemClick={jest.fn()} isOpen={true} onToggle={jest.fn()} {...props} />
28+
</Provider>
29+
);
30+
1931
beforeEach(() => {
32+
(useIsMobile as jest.Mock).mockReturnValue(false);
2033
store = mockStore({
2134
outline: {
2235
outline: [{ id: 'title', content: 'Title', level: 1 }],
@@ -41,30 +54,18 @@ describe('Outline', () => {
4154
});
4255

4356
test('dispatches fetchOutline on mount with correct filePath', () => {
44-
render(
45-
<Provider store={store}>
46-
<Outline filePath="/test.md" onItemClick={jest.fn()} isOpen={true} onToggle={jest.fn()} />
47-
</Provider>
48-
);
57+
renderOutline();
4958
expect(store.dispatch).toHaveBeenCalledWith(fetchOutline('/test.md'));
5059
});
5160

5261
test('dispatches fetchOutline with null when filePath is null', () => {
53-
render(
54-
<Provider store={store}>
55-
<Outline filePath={null} onItemClick={jest.fn()} isOpen={true} onToggle={jest.fn()} />
56-
</Provider>
57-
);
62+
renderOutline({ filePath: null });
5863
expect(store.dispatch).toHaveBeenCalledWith(fetchOutline(null));
5964
});
6065

6166
test('calls onItemClick when an outline item is clicked', () => {
6267
const onItemClickMock = jest.fn();
63-
render(
64-
<Provider store={store}>
65-
<Outline filePath="/test.md" onItemClick={onItemClickMock} isOpen={true} onToggle={jest.fn()} />
66-
</Provider>
67-
);
68+
renderOutline({ onItemClick: onItemClickMock });
6869

6970
fireEvent.click(screen.getByText('Title'));
7071
expect(onItemClickMock).toHaveBeenCalledWith('title');
@@ -79,11 +80,7 @@ describe('Outline', () => {
7980
},
8081
});
8182

82-
render(
83-
<Provider store={store}>
84-
<Outline filePath="/test.md" onItemClick={jest.fn()} isOpen={true} onToggle={jest.fn()} />
85-
</Provider>
86-
);
83+
renderOutline();
8784

8885
expect(screen.getByRole('progressbar')).toBeInTheDocument();
8986
});
@@ -97,12 +94,30 @@ describe('Outline', () => {
9794
},
9895
});
9996

100-
render(
101-
<Provider store={store}>
102-
<Outline filePath="/test.md" onItemClick={jest.fn()} isOpen={true} onToggle={jest.fn()} />
103-
</Provider>
104-
);
97+
renderOutline();
10598

10699
expect(screen.getByText('Error: Failed to load outline')).toBeInTheDocument();
107100
});
101+
102+
test('closes the mobile outline after selecting an item', () => {
103+
const onItemClickMock = jest.fn();
104+
const onToggleMock = jest.fn();
105+
(useIsMobile as jest.Mock).mockReturnValue(true);
106+
107+
renderOutline({ onItemClick: onItemClickMock, onToggle: onToggleMock });
108+
109+
fireEvent.click(screen.getByText('Title'));
110+
111+
expect(onItemClickMock).toHaveBeenCalledWith('title');
112+
expect(onToggleMock).toHaveBeenCalledTimes(1);
113+
expect(screen.getByText('Outline')).toBeInTheDocument();
114+
});
115+
116+
test('hides outline content when collapsed on desktop', () => {
117+
renderOutline({ isOpen: false });
118+
119+
expect(screen.queryByText('Title')).not.toBeInTheDocument();
120+
expect(screen.queryByText('Outline')).not.toBeInTheDocument();
121+
expect(screen.getByLabelText('toggle outline')).toBeInTheDocument();
122+
});
108123
});

0 commit comments

Comments
 (0)