Skip to content

Commit

Permalink
Add tests for FileActions.
Browse files Browse the repository at this point in the history
  • Loading branch information
negative0 committed Jul 19, 2020
1 parent c346743 commit fab2f28
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 50 deletions.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"enzyme-adapter-react-16": "^1.15.2",
"enzyme-to-json": "^3.4.4",
"fetch-mock": "^9.4.0",
"react-dnd-test-backend": "^7.7.0",
"react-dnd-test-utils": "^7.4.4",
"react-dnd-test-backend": "^11.1.3",
"react-dnd-test-utils": "^11.1.3",
"react-scripts": "^3.3.1",
"redux-mock-store": "^1.5.4"
},
Expand Down
9 changes: 9 additions & 0 deletions src/utils/testData.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,13 @@ export const FILE_ITEM_DATA = {
MimeType: 'application/octet-stream',
ModTime: '2019-06-07T22:44:37.357539000+05:30',
IsDir: false
};

export const DIR_ITEM_DATA = {
Path: 'abc',
Name: 'abc',
Size: 0,
MimeType: '',
ModTime: '2019-06-07T22:44:37.357539000+05:30',
IsDir: true
};
72 changes: 24 additions & 48 deletions src/views/Explorer/FilesView/FileActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,30 @@ function FileActions({downloadHandle, deleteHandle, item, linkShareHandle}) {
// ID = Name;
// }


if (!IsDir) {

return (
<React.Fragment>
<Button color="link" onClick={() => downloadHandle(item)}>
<i className={"fa fa-cloud-download fa-lg d-inline"}/>
</Button>
<Button color="link">
<i className="fa fa-info-circle"/>
</Button>

<UncontrolledButtonDropdown>
<DropdownToggle color="link">
<i className="fa fa-ellipsis-v"/>
</DropdownToggle>
<DropdownMenu>
<DropdownItem header>Actions</DropdownItem>
<DropdownItem onClick={() => linkShareHandle(item)}><i
className="fa fa-share fa-lg d-inline"/> Share with link</DropdownItem>
<DropdownItem divider/>
<DropdownItem onClick={() => confirmDelete(deleteHandle, item)}><i
className="fa fa-remove fa-lg d-inline text-danger"/> Delete </DropdownItem>
</DropdownMenu>
</UncontrolledButtonDropdown>
</React.Fragment>

);
} else {
return (
<React.Fragment>

<UncontrolledButtonDropdown>
<DropdownToggle color="link">
<i className="fa fa-ellipsis-v"/>
</DropdownToggle>
<DropdownMenu>
<DropdownItem header>Actions</DropdownItem>
<DropdownItem onClick={() => linkShareHandle(item)}><i
className="fa fa-share fa-lg d-inline"/> Share with link</DropdownItem>
<DropdownItem divider/>
<DropdownItem onClick={() => confirmDelete(deleteHandle, item)}><i
className="fa fa-remove fa-lg d-inline text-danger"/> Delete </DropdownItem>
</DropdownMenu>
</UncontrolledButtonDropdown>
</React.Fragment>
)
}
return (
<div data-test="fileActionsComponent">
{!IsDir && <Button color="link" onClick={() => downloadHandle(item)} data-test="btn-download">
<i className={"fa fa-cloud-download fa-lg d-inline"}/>
</Button>}
<Button color="link">
<i className="fa fa-info-circle"/>
</Button>

<UncontrolledButtonDropdown>
<DropdownToggle color="link">
<i className="fa fa-ellipsis-v"/>
</DropdownToggle>
<DropdownMenu>
<DropdownItem header>Actions</DropdownItem>
<DropdownItem data-test="btn-share-with-link" onClick={() => linkShareHandle(item)}><i
className="fa fa-share fa-lg d-inline"/> Share with link</DropdownItem>
<DropdownItem divider/>
<DropdownItem data-test="btn-delete-item" onClick={() => confirmDelete(deleteHandle, item)}><i
className="fa fa-remove fa-lg d-inline text-danger"/> Delete </DropdownItem>
</DropdownMenu>
</UncontrolledButtonDropdown>
</div>
)
}

FileActions.propTypes = {
Expand Down
55 changes: 55 additions & 0 deletions src/views/Explorer/FilesView/FileActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from "react";
import {shallow} from "enzyme";
import toJson from "enzyme-to-json";
import FileActions from "./FileActions";
import {findByTestAttr, testStore} from "../../../../Utils";
import {FILE_ITEM_DATA} from "../../../utils/testData";

const setUp = (intialState = {}, props = {}) => {
const store = testStore(intialState);

const component = shallow(<FileActions {...props} store={store}/>);
return component;
};


describe('File Actions', function () {
describe('renders', function () {
let wrapper;
let downloadHandle = jest.fn();
let deleteHandle = jest.fn();
let linkShareHandle = jest.fn();
beforeEach(() => {
const initialState = {};

const props = {
item: FILE_ITEM_DATA,
downloadHandle,
deleteHandle,
linkShareHandle
};
wrapper = setUp(initialState, props)
});

it('should render without crashing', function () {
const component = findByTestAttr(wrapper, "fileActionsComponent");
expect(component).toHaveLength(1);
});
it('should contain buttons', function () {
const downloadButton = findByTestAttr(wrapper, "btn-download");
expect(downloadButton).toHaveLength(1);

const shareWithLinkButton = findByTestAttr(wrapper, "btn-share-with-link");
expect(shareWithLinkButton).toHaveLength(1);

const deleteItemButton = findByTestAttr(wrapper, "btn-delete-item");
expect(deleteItemButton).toHaveLength(1);

});

it('should match snapshot', function () {
expect(toJson(wrapper)).toMatchSnapshot();
});

});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`File Actions renders should match snapshot 1`] = `
<div
data-test="fileActionsComponent"
>
<Button
color="link"
data-test="btn-download"
onClick={[Function]}
tag="button"
>
<i
className="fa fa-cloud-download fa-lg d-inline"
/>
</Button>
<Button
color="link"
tag="button"
>
<i
className="fa fa-info-circle"
/>
</Button>
<UncontrolledButtonDropdown>
<DropdownToggle
aria-haspopup={true}
color="link"
>
<i
className="fa fa-ellipsis-v"
/>
</DropdownToggle>
<DropdownMenu
flip={true}
tag="div"
>
<DropdownItem
header={true}
tag="button"
toggle={true}
>
Actions
</DropdownItem>
<DropdownItem
data-test="btn-share-with-link"
onClick={[Function]}
tag="button"
toggle={true}
>
<i
className="fa fa-share fa-lg d-inline"
/>
Share with link
</DropdownItem>
<DropdownItem
divider={true}
tag="button"
toggle={true}
/>
<DropdownItem
data-test="btn-delete-item"
onClick={[Function]}
tag="button"
toggle={true}
>
<i
className="fa fa-remove fa-lg d-inline text-danger"
/>
Delete
</DropdownItem>
</DropdownMenu>
</UncontrolledButtonDropdown>
</div>
`;

0 comments on commit fab2f28

Please sign in to comment.