Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React file explorer #92

Merged
merged 17 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion __test__/CanvasComponent.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import isUUID from 'validator/lib/isUUID';
import { mount } from 'enzyme';

import { wrapInTestContext } from './__mocks__/dndReduxMock';
import { getMockStore, getCanvasProps } from './__mocks__/reduxStoreMock';
import CanvasComponent from '../src/components/CanvasComponent';
Expand Down
63 changes: 63 additions & 0 deletions __test__/FileExplorer.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React from 'react';
import { mount } from 'enzyme';
import { getMockStore } from './__mocks__/reduxStoreMock';
import FileExplorerComponent from '../src/components/FileExplorer';
import { wrapInTestContext } from './__mocks__/dndReduxMock';
import TreeView from '@material-ui/lab/TreeView';
import mock from 'mock-fs';

describe('FileExplorerComponent', () => {
beforeAll(() => {
mock({
withchildren: {
testdir: {},
test: mock.file({ content: 'file contents', ctime: new Date(1) })
}
});
});
afterAll(mock.restore);

const domElement = document.getElementById('app');
const mountOptions = {
attachTo: domElement,
};
const store = getMockStore();

it('FileExplorer should render exactly one file explorer component', () => {
const FileExplorerContext = wrapInTestContext(FileExplorerComponent, store);
const wrapper = mount(<FileExplorerContext metaDirId={'99'} />, mountOptions);
const component = wrapper.find(FileExplorerComponent).first();
expect(component).toBeDefined();
expect(component).toHaveLength(1);
});

it('FileExplorer should render exactly one tree view component', () => {
const FileExplorerContext = wrapInTestContext(FileExplorerComponent, store);
const wrapper = mount(<FileExplorerContext metaDirId={'99'} />, mountOptions);
const component = wrapper.find(TreeView).first();
expect(component).toBeDefined();
expect(component).toHaveLength(1);
});

it('FileExplorer should correctly render root with no children', () => {
const FileExplorerContext = wrapInTestContext(FileExplorerComponent, store);
const wrapper = mount(<FileExplorerContext metaDirId={'99'} />, mountOptions);
const component = wrapper.find(TreeView).first();
expect(component.html()).toContain('tree');
expect(component.html()).toContain('MuiTreeView-root');
expect(component.html()).toContain('MuiTreeItem-root');
expect(component.html()).toContain('treeitem');
expect(component.html()).toContain('testdir');
});

it('FileExplorer should correctly render root with children', () => {
const FileExplorerContext = wrapInTestContext(FileExplorerComponent, store);
const wrapper = mount(<FileExplorerContext metaDirId={'24'} />, mountOptions);
const component = wrapper.find(TreeView).first();
expect(component.html()).toContain('tree');
expect(component.html()).toContain('MuiTreeView-root');
expect(component.html()).toContain('MuiTreeItem-root');
expect(component.html()).toContain('treeitem');
expect(component.html()).toContain('withchildren');
});
});
17 changes: 17 additions & 0 deletions __test__/FolderPicker.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import FolderPicker from '../src/components/FolderPicker';
import { createStore } from 'redux';
import { rootReducer } from '../src/store/root';
import { wrapInTestContext } from './__mocks__/dndReduxMock';
import React from 'react';
import { mount } from 'enzyme';
import { Provider } from 'react-redux';

describe('FolderPicker', () => {
it('FolderPicker allows users to pick a directory for opening', () => {
const store = createStore(rootReducer);
const FolderPickerContext = wrapInTestContext(FolderPicker, store);
const ref = React.createRef();
const enzymeWrapper = mount(<Provider store={store}><FolderPickerContext ref={ref} /></Provider>);
expect(enzymeWrapper.find(FolderPicker)).toHaveLength(1);
});
});
83 changes: 83 additions & 0 deletions __test__/RenderTree.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React from 'react';
import RenderTree from '../src/components/RenderTree';
import { Metadir } from '../src/types';

describe('RenderTree', () => {
const handleClick = async (e: React.MouseEvent) => {
e.preventDefault();
}

const oneFoneD: Metadir = {
id: "24",
name: "withchildren",
path: "withchildren",
containsDir: ["withchildren/testdir"],
containsFile: ["withchildren/test"]
};
const noChildren: Metadir = {
id: "99",
name: "testdir",
path: "withchildren/testdir",
containsDir: [],
containsFile: []
};
const oneFnoD: Metadir = {
id: "63",
name: "withOneFile",
path: "noFoneD/withOneFile",
containsDir: [],
containsFile: ["withOneFile/foo"]
}
const noFoneD: Metadir = {
id: "2",
name: "withOneDir",
path: "withOneDir",
containsDir: ["noFoneD/withOneFile"],
containsFile: []
}
const metadirs: Metadir[] = [];
metadirs.push(oneFoneD);
metadirs.push(noChildren);
metadirs.push(oneFnoD);
metadirs.push(noFoneD);

it('RenderTree should return a valid JSX element for an empty directory', () => {
const component = RenderTree(noChildren, metadirs, handleClick);
expect(component).toBeDefined();
expect(component.key).toEqual("99");
expect(component.props.children[0]).toHaveLength(0);
expect(component.props.children[1]).toHaveLength(0);
expect(component.props.label).toEqual("testdir");
expect(component.props.nodeId).toEqual("99");
});

it('RenderTree should create a valid JSX element for a directory with one folder and one file', () => {
const component = RenderTree(oneFoneD, metadirs, handleClick);
expect(component).toBeDefined();
expect(component.key).toEqual("24");
expect(component.props.children[0]).toHaveLength(1);
expect(component.props.children[1]).toHaveLength(1);
expect(component.props.label).toEqual("withchildren");
expect(component.props.nodeId).toEqual("24");
});

it('RenderTree should create a valid JSX element for a directory with one file', () => {
const component = RenderTree(oneFnoD, metadirs, handleClick);
expect(component).toBeDefined();
expect(component.key).toEqual("63");
expect(component.props.children[0]).toHaveLength(0);
expect(component.props.children[1]).toHaveLength(1);
expect(component.props.label).toEqual("withOneFile");
expect(component.props.nodeId).toEqual("63");
});

it('RenderTree should create a valid JSX element for a directory with one directory', () => {
const component = RenderTree(noFoneD, metadirs, handleClick);
expect(component).toBeDefined();
expect(component.key).toEqual("2");
expect(component.props.children[1]).toHaveLength(0);
expect(component.props.children[0]).toHaveLength(1);
expect(component.props.label).toEqual("withOneDir");
expect(component.props.nodeId).toEqual("2");
});
});
19 changes: 18 additions & 1 deletion __test__/__mocks__/reduxStoreMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DateTime } from 'luxon';
import { v4 } from 'uuid';
import parsePath from 'parse-path';

import { Canvas, Stack, Card, Filetype, Metafile, Repository } from '../../src/types';
import { Canvas, Stack, Card, Filetype, Metafile, Repository, Metadir } from '../../src/types';
import { createStore } from 'redux';
import { rootReducer } from '../../src/store/root';

Expand All @@ -13,6 +13,7 @@ type initStateT = {
filetypes: { [id: string]: Filetype };
metafiles: { [id: string]: Metafile };
repos: { [id: string]: Repository };
metadirs: { [id: string]: Metadir };
}
const validCardUUID = v4();
const initialState: initStateT = {
Expand Down Expand Up @@ -93,6 +94,22 @@ const initialState: initStateT = {
password: 'pass123',
token: '934394304234231'
}
},
metadirs: {
99: {
id: '99',
name: 'testdir',
path: 'withchildren/testdir',
containsDir: [],
containsFile: []
},
24: {
id: '24',
name: 'withchildren',
path: 'withchildren',
containsDir: ["withchildren/testdir"],
containsFile: ['withchildren/test']
}
}
};

Expand Down
47 changes: 47 additions & 0 deletions __test__/explorer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { generateFileTreeActions } from '../src/containers/explorer';
import mock from 'mock-fs';

describe('explorer.generateTreeNodeObject', () => {

beforeAll(() => {
mock({
foo: {
bar: mock.file({ content: 'file contents', ctime: new Date(1) }),
baz: mock.file({ content: 'file contents', ctime: new Date(1) }),
zap: {
zed: {
beq: mock.file({ content: 'file contents', ctime: new Date(1) }),
bup: mock.file({ content: 'file contents', ctime: new Date(1) })
},
zip: mock.file({ content: 'file contents', ctime: new Date(1) }),
}
},
zonk: {
zork: mock.file({ content: 'file contents', ctime: new Date(1) }),
},
imp: {
bamp: {},
},
empty: {},
});
});

afterAll(mock.restore);

it('generateFileTreeActions parses a directory populated with directories and files', () => {
return expect(generateFileTreeActions('foo')).resolves.toHaveLength(8);
});

it('generateFileTreeActions parses a directory with one file', () => {
return expect(generateFileTreeActions('zonk')).resolves.toHaveLength(2);
});

it('generateFileTreeActions parses a directory with one directory', () => {
return expect(generateFileTreeActions('imp')).resolves.toHaveLength(2);
});

it('generateFileTreeActions parses an empty directory', () => {
return expect(generateFileTreeActions('empty')).resolves.toHaveLength(1);
});

});
72 changes: 0 additions & 72 deletions __test__/extractFileTreeActions.spec.ts

This file was deleted.

31 changes: 31 additions & 0 deletions __test__/extractFileTreeNames.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mock from 'mock-fs';
import { extractFileTreeNames } from '../src/containers/filetree';

describe('extractFileTreeActions', () => {

beforeAll(() => {
mock({
foo: {
bar: mock.file({ content: 'file contents', ctime: new Date(1) }),
baz: mock.file({ content: 'file contents', ctime: new Date(1) }),
zap: {
zed: {
beq: mock.file({ content: 'file contents', ctime: new Date(1) }),
bup: mock.file({ content: 'file contents', ctime: new Date(1) })
},
zip: mock.file({ content: 'file contents', ctime: new Date(1) }),
}
}
});
});

afterAll(mock.restore);

it('extractFileTreeNames locates direct descendant subfiles and subdirectories', async () => {
return expect(extractFileTreeNames('foo/zap/zed/')).resolves.toHaveLength(3);
});

it('extractFileTreeNames locates all subfiles and subdirectories in a file tree', async () => {
return expect(extractFileTreeNames('foo/')).resolves.toHaveLength(8);
});
});
Loading