Skip to content

Commit

Permalink
FilePickerDialog converted to redux-thunk function for calling Electr…
Browse files Browse the repository at this point in the history
…on.dialog.showOpenDialog
  • Loading branch information
nelsonni committed Mar 4, 2021
1 parent 0637af6 commit 2ce4a7e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import React from 'react';
import { v4 } from 'uuid';
import { DateTime } from 'luxon';

import { remote } from 'electron'; // imports the mocked dependency to allow access to the spies
import { wrapInReduxContext } from './__mocks__/dndReduxMock';
import { mockStore } from './__mocks__/reduxStoreMock';
import FilePickerButton from '../src/components/FilePickerDialog';
import { render } from '@testing-library/react';
import { fireEvent, screen } from '@testing-library/dom';
import { filePickerDialog } from '../src/containers/filepicker';

const store = mockStore({
canvas: {
Expand All @@ -22,21 +18,13 @@ const store = mockStore({
filetypes: {},
metafiles: {},
repos: {},
errors: {}
modals: {}
});

describe('FilePickerDialog', () => {
const FilePickerContext = wrapInReduxContext(FilePickerButton, store);

it('FilePicker does not render dialog on initial state', () => {
render(<FilePickerContext />);
expect(remote.dialog.showOpenDialog).not.toHaveBeenCalled();
});

it('FilePicker allows users to pick a file for opening', async () => {
render(<FilePickerContext />);
const button = screen.queryByRole('button');
if (button) fireEvent.click(button);
await store.dispatch(filePickerDialog('openFile'));
return expect(remote.dialog.showOpenDialog).toHaveBeenCalled();
});
});
22 changes: 0 additions & 22 deletions src/components/FilePickerDialog.tsx

This file was deleted.

17 changes: 17 additions & 0 deletions src/containers/filepicker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { remote } from 'electron';
import { Action } from 'redux';
import { ThunkAction } from 'redux-thunk';
import { RootState } from '../store/root';

import { loadCard } from './handlers';

type PickerType = 'openFile' | 'openDirectory';

export const filePickerDialog = (pickerType?: PickerType): ThunkAction<Promise<void>, RootState, undefined, Action> => {
return async (dispatch) => {
const isMac = process.platform === 'darwin';
const properties: ('openFile' | 'openDirectory')[] = pickerType ? [pickerType] : (isMac ? ['openFile', 'openDirectory'] : ['openFile']);
const paths = await remote.dialog.showOpenDialog({ properties: [...properties, 'multiSelections'] });
if (!paths.canceled && paths.filePaths) paths.filePaths.map(async filePath => dispatch(loadCard({ filepath: filePath })));
};
}

0 comments on commit 2ce4a7e

Please sign in to comment.