Skip to content

Commit 39713e5

Browse files
committed
test: improve test coverage for FileTree
1 parent 4ce6e0e commit 39713e5

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

‎.codex‎

Whitespace-only changes.
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { fireEvent, render, screen } from '@testing-library/react';
2+
import React from 'react';
3+
import { Provider } from 'react-redux';
4+
import configureStore from 'redux-mock-store';
5+
import { thunk } from 'redux-thunk';
6+
import FileTree from '../../../../src/components/LeftPane/FileTree';
7+
import useIsMobile from '../../../../src/hooks/useIsMobile';
8+
import { fetchFileTree, setExpandedNodes } from '../../../../src/store/slices/fileTreeSlice';
9+
10+
const mockStore = configureStore([thunk]);
11+
12+
jest.mock('../../../../src/store/slices/fileTreeSlice', () => ({
13+
...jest.requireActual('../../../../src/store/slices/fileTreeSlice'),
14+
fetchFileTree: Object.assign(jest.fn(() => ({ type: 'fileTree/fetchFileTree/pending' })), {
15+
fulfilled: jest.fn((payload) => ({ type: 'fileTree/fetchFileTree/fulfilled', payload })),
16+
}),
17+
setExpandedNodes: jest.fn((payload) => ({ type: 'fileTree/setExpandedNodes', payload })),
18+
}));
19+
20+
jest.mock('../../../../src/hooks/useIsMobile', () => ({
21+
__esModule: true,
22+
default: jest.fn(),
23+
}));
24+
25+
jest.mock('../../../../src/components/LeftPane/FileTreeContent/FileTreeContent', () => {
26+
const React = jest.requireActual('react');
27+
28+
return {
29+
__esModule: true,
30+
default: ({ onExpandedItemsChange, onFileSelect }) => {
31+
const handleExpandItems = () => onExpandedItemsChange({}, ['folder1']);
32+
const handleSelectFile = () => onFileSelect('/docs/readme.md');
33+
34+
return React.createElement(
35+
'div',
36+
null,
37+
React.createElement('button', { onClick: handleExpandItems }, 'expand items'),
38+
React.createElement('button', { onClick: handleSelectFile }, 'select file')
39+
);
40+
},
41+
};
42+
});
43+
44+
describe('FileTree mobile behavior', () => {
45+
let store;
46+
47+
const renderFileTree = (props = {}) => render(
48+
<Provider store={store}>
49+
<FileTree
50+
onFileSelect={jest.fn()}
51+
isOpen={true}
52+
onToggle={jest.fn()}
53+
selectedFilePath={null}
54+
{...props}
55+
/>
56+
</Provider>
57+
);
58+
59+
beforeEach(() => {
60+
(useIsMobile as jest.Mock).mockReturnValue(true);
61+
store = mockStore({
62+
fileTree: {
63+
fileTree: [{ 'folder1': [{ path: 'folder1/file1.md', status: ' ' }] }],
64+
filteredFileTree: [{ 'folder1': [{ path: 'folder1/file1.md', status: ' ' }] }],
65+
loading: false,
66+
error: null,
67+
searchQuery: '',
68+
expandedNodes: [],
69+
mountedDirectoryPath: '',
70+
},
71+
});
72+
store.dispatch = jest.fn();
73+
});
74+
75+
test('dispatches expanded items updates from the mobile tree content', () => {
76+
renderFileTree();
77+
78+
fireEvent.click(screen.getByRole('button', { name: 'expand items' }));
79+
80+
expect(store.dispatch).toHaveBeenCalledWith(fetchFileTree());
81+
expect(store.dispatch).toHaveBeenCalledWith(setExpandedNodes(['folder1']));
82+
});
83+
84+
test('closes the mobile drawer after selecting a file', () => {
85+
const onFileSelect = jest.fn();
86+
const onToggle = jest.fn();
87+
88+
renderFileTree({ onFileSelect, onToggle });
89+
90+
fireEvent.click(screen.getByRole('button', { name: 'select file' }));
91+
92+
expect(onFileSelect).toHaveBeenCalledWith('/docs/readme.md');
93+
expect(onToggle).toHaveBeenCalledTimes(1);
94+
});
95+
});

0 commit comments

Comments
 (0)