-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSpaceBaseDirsModal.test.tsx
More file actions
91 lines (75 loc) · 2.79 KB
/
Copy pathSpaceBaseDirsModal.test.tsx
File metadata and controls
91 lines (75 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Spaces — "Base directories" modal.
*
* The modal must show the space's existing base dirs, let you remove one
* easily, add a folder as a clearly-optional action, and close via "Done"
* without being forced to add anything.
*
* `@mcpmux/ui` is aliased to the real source in vitest.config, so the real
* Button / toast render.
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
const { listMock, addMock, removeMock } = vi.hoisted(() => ({
listMock: vi.fn(),
addMock: vi.fn(),
removeMock: vi.fn(),
}));
vi.mock('@/lib/api/spaces', () => ({
listSpaceBaseDirs: listMock,
addSpaceBaseDir: addMock,
removeSpaceBaseDir: removeMock,
}));
import { SpaceBaseDirsModal } from '@/features/spaces/SpaceBaseDirsModal';
const SPACE = {
id: 's1',
name: 'Work',
icon: '💼',
description: null,
is_default: false,
sort_order: 0,
created_at: '',
updated_at: '',
};
function dir(id: string, path: string) {
return { id, space_id: 's1', path, created_at: '' };
}
describe('SpaceBaseDirsModal', () => {
beforeEach(() => {
listMock.mockReset();
addMock.mockReset();
removeMock.mockReset();
});
it('lists the space’s existing base directories', async () => {
listMock.mockResolvedValue([dir('d1', '/work/a'), dir('d2', '/work/b')]);
render(<SpaceBaseDirsModal space={SPACE} onClose={() => {}} />);
expect(await screen.findByText('/work/a')).toBeTruthy();
expect(screen.getByText('/work/b')).toBeTruthy();
});
it('removes a directory with one click', async () => {
const user = userEvent.setup();
removeMock.mockResolvedValue(undefined);
listMock.mockResolvedValue([dir('d1', '/work/a')]);
render(<SpaceBaseDirsModal space={SPACE} onClose={() => {}} />);
await screen.findByText('/work/a');
await user.click(screen.getByTestId('remove-base-dir-d1'));
await waitFor(() => expect(removeMock).toHaveBeenCalledWith('d1'));
await waitFor(() => expect(screen.queryByText('/work/a')).toBeNull());
});
it('closes via "Done" without adding anything', async () => {
const user = userEvent.setup();
const onClose = vi.fn();
listMock.mockResolvedValue([]);
render(<SpaceBaseDirsModal space={SPACE} onClose={onClose} />);
await screen.findByTestId('add-base-dir-btn'); // loaded (empty)
await user.click(screen.getByTestId('space-base-dirs-done'));
expect(onClose).toHaveBeenCalledTimes(1);
expect(addMock).not.toHaveBeenCalled();
});
it('renders nothing when no space is selected', () => {
const { container } = render(<SpaceBaseDirsModal space={null} onClose={() => {}} />);
expect(container).toBeEmptyDOMElement();
expect(listMock).not.toHaveBeenCalled();
});
});