mirror of
https://github.com/minio/minio.git
synced 2025-11-24 19:46:16 -05:00
Refactor download object and bulk action components (#5546)
This commit is contained in:
committed by
Harshavardhana
parent
da4558a8f7
commit
6a42727e00
@@ -25,7 +25,25 @@ describe("ObjectContainer", () => {
|
||||
|
||||
it("should render ObjectItem with props", () => {
|
||||
const wrapper = shallow(<ObjectContainer object={{ name: "test1.jpg" }} />)
|
||||
expect(wrapper.find("ObjectItem").length).toBe(1)
|
||||
expect(wrapper.find("ObjectItem").prop("name")).toBe("test1.jpg")
|
||||
expect(wrapper.find("Connect(ObjectItem)").length).toBe(1)
|
||||
expect(wrapper.find("Connect(ObjectItem)").prop("name")).toBe("test1.jpg")
|
||||
})
|
||||
|
||||
it("should pass actions to ObjectItem", () => {
|
||||
const wrapper = shallow(
|
||||
<ObjectContainer object={{ name: "test1.jpg" }} checkedObjectsCount={0} />
|
||||
)
|
||||
expect(wrapper.find("Connect(ObjectItem)").prop("actionButtons")).not.toBe(
|
||||
undefined
|
||||
)
|
||||
})
|
||||
|
||||
it("should pass empty actions to ObjectItem when checkedObjectCount is more than 0", () => {
|
||||
const wrapper = shallow(
|
||||
<ObjectContainer object={{ name: "test1.jpg" }} checkedObjectsCount={1} />
|
||||
)
|
||||
expect(wrapper.find("Connect(ObjectItem)").prop("actionButtons")).toBe(
|
||||
undefined
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -34,4 +34,27 @@ describe("ObjectItem", () => {
|
||||
wrapper.find("a").simulate("click", { preventDefault: jest.fn() })
|
||||
expect(onClick).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should call checkObject when the object/prefix is checked", () => {
|
||||
const checkObject = jest.fn()
|
||||
const wrapper = shallow(
|
||||
<ObjectItem name={"test"} checked={false} checkObject={checkObject} />
|
||||
)
|
||||
wrapper.find("input[type='checkbox']").simulate("change")
|
||||
expect(checkObject).toHaveBeenCalledWith("test")
|
||||
})
|
||||
|
||||
it("should render checked checkbox", () => {
|
||||
const wrapper = shallow(<ObjectItem name={"test"} checked={true} />)
|
||||
expect(wrapper.find("input[type='checkbox']").prop("checked")).toBeTruthy()
|
||||
})
|
||||
|
||||
it("should call uncheckObject when the object/prefix is unchecked", () => {
|
||||
const uncheckObject = jest.fn()
|
||||
const wrapper = shallow(
|
||||
<ObjectItem name={"test"} checked={true} uncheckObject={uncheckObject} />
|
||||
)
|
||||
wrapper.find("input[type='checkbox']").simulate("change")
|
||||
expect(uncheckObject).toHaveBeenCalledWith("test")
|
||||
})
|
||||
})
|
||||
|
||||
74
browser/app/js/objects/__tests__/ObjectsBulkActions.test.js
Normal file
74
browser/app/js/objects/__tests__/ObjectsBulkActions.test.js
Normal file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Minio Cloud Storage (C) 2018 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import React from "react"
|
||||
import { shallow } from "enzyme"
|
||||
import { ObjectsBulkActions } from "../ObjectsBulkActions"
|
||||
|
||||
describe("ObjectsBulkActions", () => {
|
||||
it("should render without crashing", () => {
|
||||
shallow(<ObjectsBulkActions checkedObjectsCount={0} />)
|
||||
})
|
||||
|
||||
it("should show actions when checkObjectsCount is more than 0", () => {
|
||||
const wrapper = shallow(<ObjectsBulkActions checkedObjectsCount={1} />)
|
||||
expect(wrapper.hasClass("list-actions-toggled")).toBeTruthy()
|
||||
})
|
||||
|
||||
it("should call downloadChecked when download button is clicked", () => {
|
||||
const downloadChecked = jest.fn()
|
||||
const wrapper = shallow(
|
||||
<ObjectsBulkActions
|
||||
checkedObjectsCount={1}
|
||||
downloadChecked={downloadChecked}
|
||||
/>
|
||||
)
|
||||
wrapper.find("#download-checked").simulate("click")
|
||||
expect(downloadChecked).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should call clearChecked when close button is clicked", () => {
|
||||
const clearChecked = jest.fn()
|
||||
const wrapper = shallow(
|
||||
<ObjectsBulkActions checkedObjectsCount={1} clearChecked={clearChecked} />
|
||||
)
|
||||
wrapper.find("#close-bulk-actions").simulate("click")
|
||||
expect(clearChecked).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("shoud show DeleteObjectConfirmModal when delete-checked button is clicked", () => {
|
||||
const wrapper = shallow(<ObjectsBulkActions checkedObjectsCount={1} />)
|
||||
wrapper.find("#delete-checked").simulate("click")
|
||||
wrapper.update()
|
||||
expect(wrapper.find("DeleteObjectConfirmModal").length).toBe(1)
|
||||
})
|
||||
|
||||
it("shoud call deleteChecked when Delete is clicked on confirmation modal", () => {
|
||||
const deleteChecked = jest.fn()
|
||||
const wrapper = shallow(
|
||||
<ObjectsBulkActions
|
||||
checkedObjectsCount={1}
|
||||
deleteChecked={deleteChecked}
|
||||
/>
|
||||
)
|
||||
wrapper.find("#delete-checked").simulate("click")
|
||||
wrapper.update()
|
||||
wrapper.find("DeleteObjectConfirmModal").prop("deleteObject")()
|
||||
expect(deleteChecked).toHaveBeenCalled()
|
||||
wrapper.update()
|
||||
expect(wrapper.find("DeleteObjectConfirmModal").length).toBe(0)
|
||||
})
|
||||
})
|
||||
@@ -27,7 +27,7 @@ describe("ObjectsList", () => {
|
||||
const wrapper = shallow(
|
||||
<ObjectsList objects={[{ name: "test1.jpg" }, { name: "test2.jpg" }]} />
|
||||
)
|
||||
expect(wrapper.find("ObjectContainer").length).toBe(2)
|
||||
expect(wrapper.find("Connect(ObjectContainer)").length).toBe(2)
|
||||
})
|
||||
|
||||
it("should render PrefixContainer for every prefix", () => {
|
||||
|
||||
@@ -25,8 +25,8 @@ describe("PrefixContainer", () => {
|
||||
|
||||
it("should render ObjectItem with props", () => {
|
||||
const wrapper = shallow(<PrefixContainer object={{ name: "abc/" }} />)
|
||||
expect(wrapper.find("ObjectItem").length).toBe(1)
|
||||
expect(wrapper.find("ObjectItem").prop("name")).toBe("abc/")
|
||||
expect(wrapper.find("Connect(ObjectItem)").length).toBe(1)
|
||||
expect(wrapper.find("Connect(ObjectItem)").prop("name")).toBe("abc/")
|
||||
})
|
||||
|
||||
it("should call selectPrefix when the prefix is clicked", () => {
|
||||
@@ -38,7 +38,7 @@ describe("PrefixContainer", () => {
|
||||
selectPrefix={selectPrefix}
|
||||
/>
|
||||
)
|
||||
wrapper.find("ObjectItem").prop("onClick")()
|
||||
wrapper.find("Connect(ObjectItem)").prop("onClick")()
|
||||
expect(selectPrefix).toHaveBeenCalledWith("xyz/abc/")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -18,8 +18,10 @@ import configureStore from "redux-mock-store"
|
||||
import thunk from "redux-thunk"
|
||||
import * as actionsObjects from "../actions"
|
||||
import * as alertActions from "../../alert/actions"
|
||||
import { minioBrowserPrefix } from "../../constants"
|
||||
|
||||
jest.mock("../../web", () => ({
|
||||
LoggedIn: jest.fn(() => true).mockReturnValueOnce(false),
|
||||
ListObjects: jest.fn(() => {
|
||||
return Promise.resolve({
|
||||
objects: [{ name: "test1" }, { name: "test2" }],
|
||||
@@ -38,7 +40,18 @@ jest.mock("../../web", () => ({
|
||||
return Promise.reject({ message: "Invalid bucket" })
|
||||
}
|
||||
return Promise.resolve({ url: "https://test.com/bk1/pre1/b.txt" })
|
||||
})
|
||||
}),
|
||||
CreateURLToken: jest
|
||||
.fn()
|
||||
.mockImplementationOnce(() => {
|
||||
return Promise.resolve({ token: "test" })
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
return Promise.reject({ message: "Error in creating token" })
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
return Promise.resolve({ token: "test" })
|
||||
})
|
||||
}))
|
||||
|
||||
const middlewares = [thunk]
|
||||
@@ -169,13 +182,14 @@ describe("Objects actions", () => {
|
||||
expect(actions).toEqual(expectedActions)
|
||||
})
|
||||
|
||||
it("should update browser url and creates objects/SET_CURRENT_PREFIX action when selectPrefix is called", () => {
|
||||
it("should update browser url and creates objects/SET_CURRENT_PREFIX and objects/CHECKED_LIST_RESET actions when selectPrefix is called", () => {
|
||||
const store = mockStore({
|
||||
buckets: { currentBucket: "test" },
|
||||
objects: { currentPrefix: "" }
|
||||
})
|
||||
const expectedActions = [
|
||||
{ type: "objects/SET_CURRENT_PREFIX", prefix: "abc/" }
|
||||
{ type: "objects/SET_CURRENT_PREFIX", prefix: "abc/" },
|
||||
{ type: "objects/CHECKED_LIST_RESET" }
|
||||
]
|
||||
store.dispatch(actionsObjects.selectPrefix("abc/"))
|
||||
const actions = store.getActions()
|
||||
@@ -301,4 +315,132 @@ describe("Objects actions", () => {
|
||||
expect(actions).toEqual(expectedActions)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Download object", () => {
|
||||
it("should download the object non-LoggedIn users", () => {
|
||||
const setLocation = jest.fn()
|
||||
Object.defineProperty(window, "location", {
|
||||
set(url) {
|
||||
setLocation(url)
|
||||
}
|
||||
})
|
||||
const store = mockStore({
|
||||
buckets: { currentBucket: "bk1" },
|
||||
objects: { currentPrefix: "pre1/" }
|
||||
})
|
||||
store.dispatch(actionsObjects.downloadObject("obj1"))
|
||||
const url = `${
|
||||
window.location.origin
|
||||
}${minioBrowserPrefix}/download/bk1/${encodeURI("pre1/obj1")}?token=''`
|
||||
expect(setLocation).toHaveBeenCalledWith(url)
|
||||
})
|
||||
|
||||
it("should download the object for LoggedIn users", () => {
|
||||
const setLocation = jest.fn()
|
||||
Object.defineProperty(window, "location", {
|
||||
set(url) {
|
||||
setLocation(url)
|
||||
}
|
||||
})
|
||||
const store = mockStore({
|
||||
buckets: { currentBucket: "bk1" },
|
||||
objects: { currentPrefix: "pre1/" }
|
||||
})
|
||||
return store.dispatch(actionsObjects.downloadObject("obj1")).then(() => {
|
||||
const url = `${
|
||||
window.location.origin
|
||||
}${minioBrowserPrefix}/download/bk1/${encodeURI(
|
||||
"pre1/obj1"
|
||||
)}?token=test`
|
||||
expect(setLocation).toHaveBeenCalledWith(url)
|
||||
})
|
||||
})
|
||||
|
||||
it("create alert/SET action when CreateUrlToken fails", () => {
|
||||
const store = mockStore({
|
||||
buckets: { currentBucket: "bk1" },
|
||||
objects: { currentPrefix: "pre1/" }
|
||||
})
|
||||
const expectedActions = [
|
||||
{
|
||||
type: "alert/SET",
|
||||
alert: {
|
||||
type: "danger",
|
||||
message: "Error in creating token",
|
||||
id: alertActions.alertId
|
||||
}
|
||||
}
|
||||
]
|
||||
return store.dispatch(actionsObjects.downloadObject("obj1")).then(() => {
|
||||
const actions = store.getActions()
|
||||
expect(actions).toEqual(expectedActions)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("creates objects/CHECKED_LIST_ADD action", () => {
|
||||
const store = mockStore()
|
||||
const expectedActions = [
|
||||
{
|
||||
type: "objects/CHECKED_LIST_ADD",
|
||||
object: "obj1"
|
||||
}
|
||||
]
|
||||
store.dispatch(actionsObjects.checkObject("obj1"))
|
||||
const actions = store.getActions()
|
||||
expect(actions).toEqual(expectedActions)
|
||||
})
|
||||
|
||||
it("creates objects/CHECKED_LIST_REMOVE action", () => {
|
||||
const store = mockStore()
|
||||
const expectedActions = [
|
||||
{
|
||||
type: "objects/CHECKED_LIST_REMOVE",
|
||||
object: "obj1"
|
||||
}
|
||||
]
|
||||
store.dispatch(actionsObjects.uncheckObject("obj1"))
|
||||
const actions = store.getActions()
|
||||
expect(actions).toEqual(expectedActions)
|
||||
})
|
||||
|
||||
it("creates objects/CHECKED_LIST_RESET action", () => {
|
||||
const store = mockStore()
|
||||
const expectedActions = [
|
||||
{
|
||||
type: "objects/CHECKED_LIST_RESET"
|
||||
}
|
||||
]
|
||||
store.dispatch(actionsObjects.resetCheckedList())
|
||||
const actions = store.getActions()
|
||||
expect(actions).toEqual(expectedActions)
|
||||
})
|
||||
|
||||
it("should download checked objects", () => {
|
||||
const open = jest.fn()
|
||||
const send = jest.fn()
|
||||
const xhrMockClass = () => ({
|
||||
open: open,
|
||||
send: send
|
||||
})
|
||||
window.XMLHttpRequest = jest.fn().mockImplementation(xhrMockClass)
|
||||
|
||||
const store = mockStore({
|
||||
buckets: { currentBucket: "bk1" },
|
||||
objects: { currentPrefix: "pre1/", checkedList: ["obj1"] }
|
||||
})
|
||||
return store.dispatch(actionsObjects.downloadCheckedObjects()).then(() => {
|
||||
const requestUrl = `${
|
||||
location.origin
|
||||
}${minioBrowserPrefix}/zip?token=test`
|
||||
expect(open).toHaveBeenCalledWith("POST", requestUrl, true)
|
||||
expect(send).toHaveBeenCalledWith(
|
||||
JSON.stringify({
|
||||
bucketName: "bk1",
|
||||
prefix: "pre1/",
|
||||
objects: ["obj1"]
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -31,7 +31,8 @@ describe("objects reducer", () => {
|
||||
show: false,
|
||||
object: "",
|
||||
url: ""
|
||||
}
|
||||
},
|
||||
checkedList: []
|
||||
})
|
||||
})
|
||||
|
||||
@@ -135,4 +136,33 @@ describe("objects reducer", () => {
|
||||
url: "test"
|
||||
})
|
||||
})
|
||||
|
||||
it("should handle CHECKED_LIST_ADD", () => {
|
||||
const newState = reducer(undefined, {
|
||||
type: actions.CHECKED_LIST_ADD,
|
||||
object: "obj1"
|
||||
})
|
||||
expect(newState.checkedList).toEqual(["obj1"])
|
||||
})
|
||||
|
||||
it("should handle SELECTED_LIST_REMOVE", () => {
|
||||
const newState = reducer(
|
||||
{ checkedList: ["obj1", "obj2"] },
|
||||
{
|
||||
type: actions.CHECKED_LIST_REMOVE,
|
||||
object: "obj1"
|
||||
}
|
||||
)
|
||||
expect(newState.checkedList).toEqual(["obj2"])
|
||||
})
|
||||
|
||||
it("should handle CHECKED_LIST_RESET", () => {
|
||||
const newState = reducer(
|
||||
{ checkedList: ["obj1", "obj2"] },
|
||||
{
|
||||
type: actions.CHECKED_LIST_RESET
|
||||
}
|
||||
)
|
||||
expect(newState.checkedList).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user