Skip to content

Commit b7e597d

Browse files
committed
test: add test for renaming file
1 parent d7390c8 commit b7e597d

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

tests/renderer/components/sidebar-file-tree-spec.tsx

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import * as React from 'react';
22

33
import { shallow } from 'enzyme';
44

5-
import { EditorValues, PACKAGE_NAME } from '../../../src/interfaces';
5+
import {
6+
EditorValues,
7+
MAIN_CJS,
8+
MAIN_JS,
9+
PACKAGE_NAME,
10+
} from '../../../src/interfaces';
611
import { Editors } from '../../../src/renderer/components/editors';
712
import { SidebarFileTree } from '../../../src/renderer/components/sidebar-file-tree';
813
import {
@@ -98,16 +103,13 @@ describe('SidebarFileTree component', () => {
98103
);
99104
});
100105

101-
it('fails if trying to rename an editor to an invalid value', async () => {
106+
it('fails if trying to rename an editor to package(-lock).json', async () => {
102107
const wrapper = shallow(<SidebarFileTree appState={store} />);
103108
const instance: any = wrapper.instance();
104109

105-
const EDITOR_NAME = 'data.json';
110+
const EDITOR_NAME = 'index.html';
106111
const EDITOR_NEW_NAME = PACKAGE_NAME;
107112

108-
editorValues[EDITOR_NAME] = '{}';
109-
editorMosaic.set(editorValues);
110-
111113
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
112114
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
113115

@@ -116,6 +118,62 @@ describe('SidebarFileTree component', () => {
116118
expect(store.showErrorDialog).toHaveBeenCalledWith(
117119
`Cannot add ${PACKAGE_NAME} or package-lock.json as custom files`,
118120
);
121+
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(EditorPresence.Pending);
122+
});
123+
124+
it('fails if trying to rename an editor to an unsupported name', async () => {
125+
const wrapper = shallow(<SidebarFileTree appState={store} />);
126+
const instance: any = wrapper.instance();
127+
128+
const EDITOR_NAME = 'index.html';
129+
const EDITOR_NEW_NAME = 'data.txt';
130+
131+
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
132+
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
133+
134+
await instance.renameEditor(EDITOR_NAME);
135+
136+
expect(store.showErrorDialog).toHaveBeenCalledWith(
137+
`Invalid filename "${EDITOR_NEW_NAME}": Must be a file ending in .cjs, .js, .mjs, .html, .css, or .json`,
138+
);
139+
expect(editorMosaic.files.get(EDITOR_NAME)).toBe(EditorPresence.Pending);
140+
});
141+
142+
it('fails if trying to rename an editor to an existing name', async () => {
143+
const wrapper = shallow(<SidebarFileTree appState={store} />);
144+
const instance: any = wrapper.instance();
145+
146+
const EXISTED_NAME = 'styles.css';
147+
const TO_BE_NAMED = 'index.html';
148+
const EDITOR_NEW_NAME = EXISTED_NAME;
149+
150+
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
151+
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
152+
153+
await instance.renameEditor(TO_BE_NAMED);
154+
155+
expect(store.showErrorDialog).toHaveBeenCalledWith(
156+
`Cannot add file "${EDITOR_NEW_NAME}": File already exists`,
157+
);
158+
expect(editorMosaic.files.get(TO_BE_NAMED)).toBe(EditorPresence.Pending);
159+
});
160+
161+
it('fails if trying to rename an editor to another main entry point file', async () => {
162+
const wrapper = shallow(<SidebarFileTree appState={store} />);
163+
const instance: any = wrapper.instance();
164+
165+
const TO_BE_NAMED = 'index.html';
166+
const EDITOR_NEW_NAME = MAIN_CJS;
167+
168+
store.showInputDialog = jest.fn().mockResolvedValueOnce(EDITOR_NEW_NAME);
169+
store.showErrorDialog = jest.fn().mockResolvedValueOnce(true);
170+
171+
await instance.renameEditor(TO_BE_NAMED);
172+
173+
expect(store.showErrorDialog).toHaveBeenCalledWith(
174+
`Cannot add file "${EDITOR_NEW_NAME}": Main entry point ${MAIN_JS} exists`,
175+
);
176+
expect(editorMosaic.files.get(TO_BE_NAMED)).toBe(EditorPresence.Pending);
119177
});
120178

121179
it('can reset the editor layout', () => {

0 commit comments

Comments
 (0)