Skip to content

Commit 8f05632

Browse files
committed
test: improve test coverage for Layout
1 parent 1ba5930 commit 8f05632

2 files changed

Lines changed: 267 additions & 562 deletions

File tree

Lines changed: 222 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,262 @@
1-
import { act, render, screen, fireEvent } from '@testing-library/react';
1+
import { fireEvent, render, screen } from '@testing-library/react';
22
import React from 'react';
33
import { Provider } from 'react-redux';
4-
import { BrowserRouter } from 'react-router-dom';
4+
import { MemoryRouter } from 'react-router-dom';
55
import Layout from '../../src/Layout';
66
import { toggleFileTree, toggleOutline } from '../../src/store/slices/appSettingSlice';
77
import { createMockStore } from '../utils';
88

9-
jest.mock('@mui/x-tree-view', () => ({
10-
...jest.requireActual('@mui/x-tree-view'),
11-
SimpleTreeView: ({ children, ...props }: { children: React.ReactNode }) => (
12-
<div data-testid="mock-simple-tree-view" {...props}>
13-
{children}
14-
</div>
15-
),
16-
}));
17-
18-
// Mock react-router-dom's useNavigate
199
const mockNavigate = jest.fn();
10+
const mockUseIsMobile = jest.fn();
11+
12+
type AppHeaderMockProps = {
13+
handleFileSelect: (path: string) => void;
14+
onSettingsClick: () => void;
15+
onToggleFileTree: () => void;
16+
onToggleOutline: () => void;
17+
};
18+
19+
type ContentMockProps = {
20+
onFileSelect: (path: string) => void;
21+
onDirectorySelect: (path: string) => void;
22+
scrollToId: string | null;
23+
};
24+
25+
type FileTreeMockProps = {
26+
isOpen: boolean;
27+
onFileSelect: (path: string) => void;
28+
selectedFilePath: string | null;
29+
};
30+
31+
type OutlineMockProps = {
32+
filePath: string | null;
33+
isOpen: boolean;
34+
onItemClick: (id: string) => void;
35+
};
36+
37+
let mockAppHeaderProps: AppHeaderMockProps;
38+
let mockContentProps: ContentMockProps;
39+
let mockFileTreeProps: FileTreeMockProps;
40+
let mockOutlineProps: OutlineMockProps;
41+
42+
function mockHandleHeaderFileSelect() {
43+
mockAppHeaderProps.handleFileSelect('docs/readme.md');
44+
}
45+
46+
function mockHandleContentFileSelect() {
47+
mockContentProps.onFileSelect('content/file.md');
48+
}
49+
50+
function mockHandleContentDirectorySelect() {
51+
mockContentProps.onDirectorySelect('guides');
52+
}
53+
54+
function mockHandleTreeFileSelect() {
55+
mockFileTreeProps.onFileSelect('tree/file.md');
56+
}
57+
58+
function mockHandleOutlineItemClick() {
59+
mockOutlineProps.onItemClick('heading-1');
60+
}
61+
2062
jest.mock('react-router-dom', () => ({
2163
...jest.requireActual('react-router-dom'),
2264
useNavigate: () => mockNavigate,
2365
}));
2466

25-
// Mock appSettingSlice actions
26-
jest.mock('../../src/store/slices/appSettingSlice', () => ({
27-
...jest.requireActual('../../src/store/slices/appSettingSlice'),
28-
toggleFileTree: jest.fn(() => ({ type: 'appSetting/toggleFileTree' })),
29-
toggleOutline: jest.fn(() => ({ type: 'appSetting/toggleOutline' })),
67+
jest.mock('../../src/hooks/useIsMobile', () => ({
68+
__esModule: true,
69+
default: () => mockUseIsMobile(),
3070
}));
3171

32-
describe('Layout', () => {
33-
let store;
72+
jest.mock('../../src/components/AppHeader', () => ({
73+
__esModule: true,
74+
default: (props: AppHeaderMockProps) => {
75+
mockAppHeaderProps = props;
3476

35-
beforeEach(() => {
36-
store = createMockStore();
77+
return (
78+
<div>
79+
<button onClick={mockHandleHeaderFileSelect}>header-file-select</button>
80+
<button onClick={props.onSettingsClick}>settings</button>
81+
<button aria-label="toggle file tree" onClick={props.onToggleFileTree}>toggle file tree</button>
82+
<button aria-label="toggle outline" onClick={props.onToggleOutline}>toggle outline</button>
83+
</div>
84+
);
85+
},
86+
}));
87+
88+
jest.mock('../../src/components/Content/Content', () => ({
89+
__esModule: true,
90+
default: (props: ContentMockProps) => {
91+
mockContentProps = props;
92+
93+
return (
94+
<div>
95+
<div data-testid="content-scroll-id">{props.scrollToId ?? 'null'}</div>
96+
<button onClick={mockHandleContentFileSelect}>content-file-select</button>
97+
<button onClick={mockHandleContentDirectorySelect}>content-directory-select</button>
98+
</div>
99+
);
100+
},
101+
}));
102+
103+
jest.mock('../../src/components/LeftPane/FileTree', () => ({
104+
__esModule: true,
105+
default: (props: FileTreeMockProps) => {
106+
mockFileTreeProps = props;
107+
108+
return (
109+
<div>
110+
<div data-testid="file-tree-open">{String(props.isOpen)}</div>
111+
<div data-testid="selected-file-path">{props.selectedFilePath ?? 'null'}</div>
112+
<button onClick={mockHandleTreeFileSelect}>tree-file-select</button>
113+
</div>
114+
);
115+
},
116+
}));
117+
118+
jest.mock('../../src/components/RightPane/Outline', () => ({
119+
__esModule: true,
120+
default: (props: OutlineMockProps) => {
121+
mockOutlineProps = props;
122+
123+
return (
124+
<div>
125+
<div data-testid="outline-file-path">{props.filePath ?? 'null'}</div>
126+
<div data-testid="outline-open">{String(props.isOpen)}</div>
127+
<button onClick={mockHandleOutlineItemClick}>outline-item-click</button>
128+
</div>
129+
);
130+
},
131+
}));
132+
133+
describe('Layout', () => {
134+
const renderLayout = (
135+
initialState = {},
136+
onSettingsClick = jest.fn(),
137+
) => {
138+
const store = createMockStore(initialState);
37139
store.dispatch = jest.fn();
38-
});
39140

40-
test('renders correctly', async () => {
41-
let asFragment;
42-
await act(async () => {
43-
const { asFragment: f } = render(
141+
return {
142+
...render(
44143
<Provider store={store}>
45-
<BrowserRouter>
46-
<Layout />
47-
</BrowserRouter>
144+
<MemoryRouter>
145+
<Layout onSettingsClick={onSettingsClick} />
146+
</MemoryRouter>
48147
</Provider>
49-
);
50-
asFragment = f;
51-
});
52-
expect(asFragment()).toMatchSnapshot();
148+
),
149+
onSettingsClick,
150+
store,
151+
};
152+
};
153+
154+
beforeEach(() => {
155+
mockNavigate.mockClear();
156+
mockUseIsMobile.mockReturnValue(false);
157+
document.documentElement.style.removeProperty('--markdown-font-family');
158+
document.documentElement.style.removeProperty('--markdown-monospace-font-family');
53159
});
54160

55-
test('handleFileSelect navigates to the correct path', async () => {
56-
await act(async () => {
57-
render(
58-
<Provider store={store}>
59-
<BrowserRouter>
60-
<Layout />
61-
</BrowserRouter>
62-
</Provider>
63-
);
161+
test('renders correctly', () => {
162+
const { asFragment } = renderLayout({
163+
history: { currentPath: 'docs/readme.md', isDirectory: false },
64164
});
65165

66-
// Simulate a file selection from AppHeader (logo click)
67-
fireEvent.click(screen.getByText('mdts'));
68-
expect(mockNavigate).toHaveBeenCalledWith('/');
166+
expect(asFragment()).toMatchSnapshot();
69167
});
70168

71-
test('handleToggleFileTree dispatches toggleFileTree', async () => {
72-
await act(async () => {
73-
render(
74-
<Provider store={store}>
75-
<BrowserRouter>
76-
<Layout />
77-
</BrowserRouter>
78-
</Provider>
79-
);
80-
});
169+
test('navigates from child callbacks and applies font variables', () => {
170+
const onSettingsClick = jest.fn();
81171

82-
fireEvent.click(screen.getByLabelText('toggle file tree'));
83-
expect(store.dispatch).toHaveBeenCalledWith(toggleFileTree());
172+
renderLayout({
173+
config: {
174+
fontFamily: 'Fira Sans',
175+
fontFamilyMonospace: 'Fira Code',
176+
},
177+
history: { currentPath: 'docs/readme.md', isDirectory: false },
178+
}, onSettingsClick);
179+
180+
fireEvent.click(screen.getByText('header-file-select'));
181+
fireEvent.click(screen.getByText('tree-file-select'));
182+
fireEvent.click(screen.getByText('content-directory-select'));
183+
fireEvent.click(screen.getByText('settings'));
184+
185+
expect(mockNavigate).toHaveBeenNthCalledWith(1, '/docs/readme.md');
186+
expect(mockNavigate).toHaveBeenNthCalledWith(2, '/tree/file.md');
187+
expect(mockNavigate).toHaveBeenNthCalledWith(3, '/guides');
188+
expect(onSettingsClick).toHaveBeenCalledTimes(1);
189+
expect(screen.getByTestId('selected-file-path')).toHaveTextContent('docs/readme.md');
190+
expect(screen.getByTestId('outline-file-path')).toHaveTextContent('docs/readme.md');
191+
expect(document.documentElement.style.getPropertyValue('--markdown-font-family')).toBe('Fira Sans');
192+
expect(document.documentElement.style.getPropertyValue('--markdown-monospace-font-family')).toBe('Fira Code');
84193
});
85194

86-
test('handleDirectorySelect navigates to the correct path', async () => {
87-
await act(async () => {
88-
render(
89-
<Provider store={store}>
90-
<BrowserRouter>
91-
<Layout />
92-
</BrowserRouter>
93-
</Provider>
94-
);
195+
test('passes null outline file path for directories and updates scroll target', () => {
196+
renderLayout({
197+
history: { currentPath: 'docs', isDirectory: true },
95198
});
96199

97-
// This requires simulating a directory selection from the Content component.
98-
// Since Content is mocked, we'll simulate the prop being called.
99-
// For a more robust test, we'd need to render Content and simulate interaction there.
100-
// For now, we'll rely on the fact that handleDirectorySelect is passed down.
200+
expect(screen.getByTestId('outline-file-path')).toHaveTextContent('null');
201+
expect(screen.getByTestId('content-scroll-id')).toHaveTextContent('null');
202+
203+
fireEvent.click(screen.getByText('outline-item-click'));
101204

102-
// This part is tricky without directly interacting with the Content component.
103-
// We'll assume the prop is correctly passed and focus on the navigation.
104-
// The actual interaction would happen in DirectoryContent.test.tsx
205+
expect(screen.getByTestId('content-scroll-id')).toHaveTextContent('heading-1');
105206
});
106207

107-
test('handleOutlineItemClick sets scrollToId', async () => {
108-
// This is an internal state change, difficult to test directly without exposing state.
109-
// We'll rely on the Outline component's tests to ensure onItemClick is called correctly.
208+
test('dispatches desktop toggle actions', () => {
209+
const { store } = renderLayout();
210+
211+
fireEvent.click(screen.getByLabelText('toggle file tree'));
212+
fireEvent.click(screen.getByLabelText('toggle outline'));
213+
214+
expect(store.dispatch).toHaveBeenNthCalledWith(1, toggleFileTree());
215+
expect(store.dispatch).toHaveBeenNthCalledWith(2, toggleOutline());
216+
expect(screen.getByTestId('file-tree-open')).toHaveTextContent('true');
217+
expect(screen.getByTestId('outline-open')).toHaveTextContent('true');
110218
});
111219

112-
test('handleToggleOutline dispatches toggleOutline', async () => {
113-
await act(async () => {
114-
render(
115-
<Provider store={store}>
116-
<BrowserRouter>
117-
<Layout />
118-
</BrowserRouter>
119-
</Provider>
120-
);
220+
test('uses local mobile panel state and resets it after returning from desktop', () => {
221+
mockUseIsMobile.mockReturnValue(true);
222+
223+
const { rerender, store } = renderLayout({
224+
appSetting: {
225+
fileTreeOpen: false,
226+
outlineOpen: false,
227+
},
121228
});
122229

230+
fireEvent.click(screen.getByLabelText('toggle file tree'));
231+
232+
expect(screen.getByTestId('file-tree-open')).toHaveTextContent('true');
233+
expect(screen.getByTestId('outline-open')).toHaveTextContent('false');
234+
123235
fireEvent.click(screen.getByLabelText('toggle outline'));
124-
expect(store.dispatch).toHaveBeenCalledWith(toggleOutline());
236+
237+
expect(screen.getByTestId('file-tree-open')).toHaveTextContent('false');
238+
expect(screen.getByTestId('outline-open')).toHaveTextContent('true');
239+
expect(store.dispatch).not.toHaveBeenCalled();
240+
241+
mockUseIsMobile.mockReturnValue(false);
242+
rerender(
243+
<Provider store={store}>
244+
<MemoryRouter>
245+
<Layout onSettingsClick={jest.fn()} />
246+
</MemoryRouter>
247+
</Provider>
248+
);
249+
250+
mockUseIsMobile.mockReturnValue(true);
251+
rerender(
252+
<Provider store={store}>
253+
<MemoryRouter>
254+
<Layout onSettingsClick={jest.fn()} />
255+
</MemoryRouter>
256+
</Provider>
257+
);
258+
259+
expect(screen.getByTestId('file-tree-open')).toHaveTextContent('false');
260+
expect(screen.getByTestId('outline-open')).toHaveTextContent('false');
125261
});
126262
});

0 commit comments

Comments
 (0)