Skip to content

Commit 9c27dd7

Browse files
committed
fixup: CR suggestion
1 parent 724724e commit 9c27dd7

File tree

5 files changed

+100
-50
lines changed

5 files changed

+100
-50
lines changed

packages/compass-components/src/components/file-input.spec.tsx

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,25 @@ describe('FileInput', function () {
256256
describe('when a file is chosen', function () {
257257
beforeEach(async function () {
258258
render(
259-
<FileInput
260-
id="file-input"
261-
label="Select something"
262-
dataTestId="test-file-input"
263-
onChange={spy}
264-
error={true}
265-
errorMessage={'Error'}
266-
mode="open"
267-
/>
259+
<FileInputBackendProvider
260+
createFileInputBackend={() =>
261+
({
262+
getPathForFile() {
263+
return 'a/b/c';
264+
},
265+
} as any)
266+
}
267+
>
268+
<FileInput
269+
id="file-input"
270+
label="Select something"
271+
dataTestId="test-file-input"
272+
onChange={spy}
273+
error={true}
274+
errorMessage={'Error'}
275+
mode="open"
276+
/>
277+
</FileInputBackendProvider>
268278
);
269279

270280
const fileInput = screen.getByTestId('test-file-input');
@@ -284,7 +294,7 @@ describe('FileInput', function () {
284294

285295
it('calls onChange with the chosen file', function () {
286296
expect(spy.callCount).to.equal(1);
287-
expect(spy.firstCall.args[0]).to.deep.equal([undefined]); // cannot get the actual path without electron
297+
expect(spy.firstCall.args[0]).to.deep.equal(['a/b/c']);
288298
});
289299
});
290300

@@ -343,13 +353,19 @@ describe('FileInput', function () {
343353
showOpenDialog: sinon.stub(),
344354
},
345355
};
346-
return { fakeElectron, fakeWindow };
356+
const fakeWebUtils = {
357+
getPathForFile: sinon.stub().returns('a/b/c'),
358+
};
359+
return { fakeElectron, fakeWindow, fakeWebUtils };
347360
}
348361

349362
it('allows using electron-style APIs for file updates', async function () {
350-
const { fakeElectron, fakeWindow } = createFakeElectron();
363+
const { fakeElectron, fakeWindow, fakeWebUtils } = createFakeElectron();
351364

352-
const backend = createElectronFileInputBackend(fakeElectron)();
365+
const backend = createElectronFileInputBackend(
366+
fakeElectron,
367+
fakeWebUtils
368+
)();
353369
const listener = sinon.stub();
354370
const unsubscribe = backend.onFilesChosen(listener);
355371

@@ -378,10 +394,24 @@ describe('FileInput', function () {
378394
expect(fakeElectron.dialog.showSaveDialog).to.have.been.calledTwice;
379395
});
380396

397+
it('passes through getPathForFile calls', function () {
398+
const { fakeElectron, fakeWebUtils } = createFakeElectron();
399+
400+
const backend = createElectronFileInputBackend(
401+
fakeElectron,
402+
fakeWebUtils
403+
)();
404+
405+
expect(backend.getPathForFile({} as any)).to.equal('a/b/c');
406+
});
407+
381408
it('can partially handle browser-compatible accept values', function () {
382-
const { fakeElectron, fakeWindow } = createFakeElectron();
409+
const { fakeElectron, fakeWindow, fakeWebUtils } = createFakeElectron();
383410

384-
const backend = createElectronFileInputBackend(fakeElectron)();
411+
const backend = createElectronFileInputBackend(
412+
fakeElectron,
413+
fakeWebUtils
414+
)();
385415

386416
fakeElectron.dialog.showSaveDialog.resolves({
387417
canceled: true,
@@ -404,9 +434,12 @@ describe('FileInput', function () {
404434
});
405435

406436
it('does not override existing file filters', function () {
407-
const { fakeElectron, fakeWindow } = createFakeElectron();
437+
const { fakeElectron, fakeWindow, fakeWebUtils } = createFakeElectron();
408438

409-
const backend = createElectronFileInputBackend(fakeElectron)();
439+
const backend = createElectronFileInputBackend(
440+
fakeElectron,
441+
fakeWebUtils
442+
)();
410443

411444
fakeElectron.dialog.showSaveDialog.resolves({
412445
canceled: true,
@@ -430,9 +463,12 @@ describe('FileInput', function () {
430463
});
431464

432465
it('handles multi:false', function () {
433-
const { fakeElectron, fakeWindow } = createFakeElectron();
466+
const { fakeElectron, fakeWindow, fakeWebUtils } = createFakeElectron();
434467

435-
const backend = createElectronFileInputBackend(fakeElectron)();
468+
const backend = createElectronFileInputBackend(
469+
fakeElectron,
470+
fakeWebUtils
471+
)();
436472

437473
fakeElectron.dialog.showSaveDialog.resolves({
438474
canceled: true,
@@ -452,9 +488,12 @@ describe('FileInput', function () {
452488
});
453489

454490
it('handles multi:true', function () {
455-
const { fakeElectron, fakeWindow } = createFakeElectron();
491+
const { fakeElectron, fakeWindow, fakeWebUtils } = createFakeElectron();
456492

457-
const backend = createElectronFileInputBackend(fakeElectron)();
493+
const backend = createElectronFileInputBackend(
494+
fakeElectron,
495+
fakeWebUtils
496+
)();
458497

459498
fakeElectron.dialog.showSaveDialog.resolves({
460499
canceled: true,
@@ -473,9 +512,12 @@ describe('FileInput', function () {
473512
});
474513

475514
it('can call showOpenDialog if requested', async function () {
476-
const { fakeElectron, fakeWindow } = createFakeElectron();
515+
const { fakeElectron, fakeWindow, fakeWebUtils } = createFakeElectron();
477516

478-
const backend = createElectronFileInputBackend(fakeElectron)();
517+
const backend = createElectronFileInputBackend(
518+
fakeElectron,
519+
fakeWebUtils
520+
)();
479521
const listener = sinon.stub();
480522
backend.onFilesChosen(listener);
481523

@@ -501,9 +543,12 @@ describe('FileInput', function () {
501543
});
502544

503545
it('calls the listener with an empty array if the user canceled the request', async function () {
504-
const { fakeElectron } = createFakeElectron();
546+
const { fakeElectron, fakeWebUtils } = createFakeElectron();
505547

506-
const backend = createElectronFileInputBackend(fakeElectron)();
548+
const backend = createElectronFileInputBackend(
549+
fakeElectron,
550+
fakeWebUtils
551+
)();
507552
const listener = sinon.stub();
508553
backend.onFilesChosen(listener);
509554

@@ -521,8 +566,11 @@ describe('FileInput', function () {
521566
});
522567

523568
it('handles autoOpen:true', async function () {
524-
const { fakeElectron } = createFakeElectron();
525-
const backend = createElectronFileInputBackend(fakeElectron)();
569+
const { fakeElectron, fakeWebUtils } = createFakeElectron();
570+
const backend = createElectronFileInputBackend(
571+
fakeElectron,
572+
fakeWebUtils
573+
)();
526574

527575
const listener = sinon.stub();
528576
backend.onFilesChosen(listener);
@@ -577,8 +625,11 @@ describe('FileInput', function () {
577625
let openFileChooserSpy: sinon.SinonSpy;
578626

579627
beforeEach(async function () {
580-
const { fakeElectron } = createFakeElectron();
581-
const backend = createElectronFileInputBackend(fakeElectron)();
628+
const { fakeElectron, fakeWebUtils } = createFakeElectron();
629+
const backend = createElectronFileInputBackend(
630+
fakeElectron,
631+
fakeWebUtils
632+
)();
582633

583634
listener = sinon.stub();
584635
backend.onFilesChosen(listener);

packages/compass-components/src/components/file-input.tsx

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ export type FileInputBackend = {
158158
// Should install a listener that is called when files have been selected.
159159
// Should return an unsubscribe function.
160160
onFilesChosen: (listener: (files: string[]) => void) => () => void;
161+
// Gets the real on-disk path for a `File`, if the current environment supports it.
162+
getPathForFile: (file: File) => string | undefined;
161163
};
162164

163165
export const FileInputBackendContext = createContext<
@@ -191,6 +193,11 @@ export type ElectronShowFileDialogProvider<ElectronWindow> = {
191193
};
192194
};
193195

196+
export type ElectronWebUtilsProvider = {
197+
// https://github.com/electron/electron/blob/83d704009687956fb4b69cb13ab03664d7950118/docs/breaking-changes.md#removed-filepath
198+
getPathForFile(file: File): string;
199+
};
200+
194201
export const FileInputBackendProvider: React.FunctionComponent<{
195202
createFileInputBackend: (() => FileInputBackend) | null;
196203
}> = ({ children, createFileInputBackend }) => {
@@ -211,7 +218,8 @@ export const FileInputBackendProvider: React.FunctionComponent<{
211218
// <FileInput ... />
212219
// <FileInputBackendProvider/>
213220
export function createElectronFileInputBackend<ElectronWindow>(
214-
electron: ElectronShowFileDialogProvider<ElectronWindow>
221+
electron: ElectronShowFileDialogProvider<ElectronWindow>,
222+
webUtils: ElectronWebUtilsProvider | null
215223
): () => FileInputBackend {
216224
return () => {
217225
const listeners: ((files: string[]) => void)[] = [];
@@ -282,24 +290,13 @@ export function createElectronFileInputBackend<ElectronWindow>(
282290
if (index !== -1) listeners.splice(index, 1);
283291
};
284292
},
293+
getPathForFile(file: File): string | undefined {
294+
return webUtils?.getPathForFile(file);
295+
},
285296
};
286297
};
287298
}
288299

289-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
290-
let _electron: typeof import('electron') | undefined | null;
291-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
292-
function getElectronWebUtils(): typeof import('electron').webUtils | undefined {
293-
if (_electron === undefined) {
294-
try {
295-
_electron = require('electron');
296-
} catch {
297-
_electron = null;
298-
}
299-
}
300-
return _electron?.webUtils;
301-
}
302-
303300
function FileInput({
304301
autoOpen = false,
305302
id,
@@ -365,9 +362,7 @@ function FileInput({
365362
(evt: React.ChangeEvent<HTMLInputElement>) => {
366363
const fileList = Array.from(evt.currentTarget.files ?? []);
367364
const files = fileList.map((file) => {
368-
// https://github.com/electron/electron/blob/83d704009687956fb4b69cb13ab03664d7950118/docs/breaking-changes.md#removed-filepath
369-
// eslint-disable-next-line @typescript-eslint/no-var-requires
370-
return getElectronWebUtils()?.getPathForFile(file);
365+
return backend?.getPathForFile(file) ?? '';
371366
});
372367
onChange(files);
373368
},

packages/compass-import-export/src/components/export-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function ExportModal({
193193
const onClickExport = useCallback(() => {
194194
// eslint-disable-next-line @typescript-eslint/consistent-type-imports, @typescript-eslint/no-var-requires
195195
const electron: typeof import('@electron/remote') = require('@electron/remote');
196-
const fileBackend = createElectronFileInputBackend(electron)();
196+
const fileBackend = createElectronFileInputBackend(electron, null)();
197197

198198
fileBackend.onFilesChosen((files: string[]) => {
199199
if (files.length > 0) {

packages/compass-workspaces/src/stores/workspaces.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('tabs behavior', function () {
2222

2323
function configureStore() {
2424
const result = activatePluginWithConnections();
25-
return result.plugin.store as WorkspacesStore;
25+
return result.plugin.store;
2626
}
2727

2828
function openTabs(

packages/compass/src/app/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dns from 'dns';
55
import ensureError from 'ensure-error';
66
import { ipcRenderer } from 'hadron-ipc';
77
import * as remote from '@electron/remote';
8+
import { webUtils } from 'electron';
89
import { globalAppRegistry } from 'hadron-app-registry';
910
import { defaultPreferencesInstance } from 'compass-preferences-model';
1011
import semver from 'semver';
@@ -242,7 +243,10 @@ const Application = View.extend({
242243
<CompassElectron
243244
appName={remote.app.getName()}
244245
showWelcomeModal={!wasNetworkOptInShown}
245-
createFileInputBackend={createElectronFileInputBackend(remote)}
246+
createFileInputBackend={createElectronFileInputBackend(
247+
remote,
248+
webUtils
249+
)}
246250
onDisconnect={notifyMainProcessOfDisconnect}
247251
showCollectionSubMenu={showCollectionSubMenu}
248252
hideCollectionSubMenu={hideCollectionSubMenu}

0 commit comments

Comments
 (0)