storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/objects/__tests__/ShareObjectModal.test.js (about) 1 /* 2 * MinIO Cloud Storage (C) 2018 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import React from "react" 18 import { shallow, mount } from "enzyme" 19 import { ShareObjectModal } from "../ShareObjectModal" 20 import { 21 SHARE_OBJECT_EXPIRY_DAYS, 22 SHARE_OBJECT_EXPIRY_HOURS, 23 SHARE_OBJECT_EXPIRY_MINUTES 24 } from "../../constants" 25 26 jest.mock("../../web", () => ({ 27 LoggedIn: jest.fn(() => { 28 return true 29 }) 30 })) 31 32 describe("ShareObjectModal", () => { 33 it("should render without crashing", () => { 34 shallow( 35 <ShareObjectModal 36 object={{ name: "obj1" }} 37 shareObjectDetails={{ show: true, object: "obj1", url: "test", showExpiryDate: true }} 38 /> 39 ) 40 }) 41 42 it("shoud call hideShareObject when Cancel is clicked", () => { 43 const hideShareObject = jest.fn() 44 const wrapper = shallow( 45 <ShareObjectModal 46 object={{ name: "obj1" }} 47 shareObjectDetails={{ show: true, object: "obj1", url: "test", showExpiryDate: true }} 48 hideShareObject={hideShareObject} 49 /> 50 ) 51 wrapper 52 .find("button") 53 .last() 54 .simulate("click") 55 expect(hideShareObject).toHaveBeenCalled() 56 }) 57 58 it("should show the shareable link", () => { 59 const wrapper = shallow( 60 <ShareObjectModal 61 object={{ name: "obj1" }} 62 shareObjectDetails={{ show: true, object: "obj1", url: "test", showExpiryDate: true }} 63 /> 64 ) 65 expect( 66 wrapper 67 .find("input") 68 .first() 69 .prop("value") 70 ).toBe(`${window.location.protocol}//test`) 71 }) 72 73 it("should call showCopyAlert and hideShareObject when Copy button is clicked", () => { 74 const hideShareObject = jest.fn() 75 const showCopyAlert = jest.fn() 76 const wrapper = shallow( 77 <ShareObjectModal 78 object={{ name: "obj1" }} 79 shareObjectDetails={{ show: true, object: "obj1", url: "test", showExpiryDate: true }} 80 hideShareObject={hideShareObject} 81 showCopyAlert={showCopyAlert} 82 /> 83 ) 84 wrapper.find("CopyToClipboard").prop("onCopy")() 85 expect(showCopyAlert).toHaveBeenCalledWith("Link copied to clipboard!") 86 expect(hideShareObject).toHaveBeenCalled() 87 }) 88 89 describe("Update expiry values", () => { 90 const props = { 91 object: { name: "obj1" }, 92 shareObjectDetails: { show: true, object: "obj1", url: "test", showExpiryDate: true } 93 } 94 95 it("should not show expiry values if shared with public link", () => { 96 const shareObjectDetails = { show: true, object: "obj1", url: "test", showExpiryDate: false } 97 const wrapper = shallow(<ShareObjectModal {...props} shareObjectDetails={shareObjectDetails} />) 98 expect(wrapper.find('.set-expire').exists()).toEqual(false) 99 }) 100 101 it("should have default expiry values", () => { 102 const wrapper = shallow(<ShareObjectModal {...props} />) 103 expect(wrapper.state("expiry")).toEqual({ 104 days: SHARE_OBJECT_EXPIRY_DAYS, 105 hours: SHARE_OBJECT_EXPIRY_HOURS, 106 minutes: SHARE_OBJECT_EXPIRY_MINUTES 107 }) 108 }) 109 110 it("should not allow any increments when days is already max", () => { 111 const shareObject = jest.fn() 112 const wrapper = shallow( 113 <ShareObjectModal {...props} shareObject={shareObject} /> 114 ) 115 wrapper.setState({ 116 expiry: { 117 days: 7, 118 hours: 0, 119 minutes: 0 120 } 121 }) 122 wrapper.find("#increase-hours").simulate("click") 123 expect(wrapper.state("expiry")).toEqual({ 124 days: 7, 125 hours: 0, 126 minutes: 0 127 }) 128 expect(shareObject).not.toHaveBeenCalled() 129 }) 130 131 it("should not allow expiry values less than minimum value", () => { 132 const shareObject = jest.fn() 133 const wrapper = shallow( 134 <ShareObjectModal {...props} shareObject={shareObject} /> 135 ) 136 wrapper.setState({ 137 expiry: { 138 days: 5, 139 hours: 0, 140 minutes: 0 141 } 142 }) 143 wrapper.find("#decrease-hours").simulate("click") 144 expect(wrapper.state("expiry").hours).toBe(0) 145 wrapper.find("#decrease-minutes").simulate("click") 146 expect(wrapper.state("expiry").minutes).toBe(0) 147 expect(shareObject).not.toHaveBeenCalled() 148 }) 149 150 it("should not allow expiry values more than maximum value", () => { 151 const shareObject = jest.fn() 152 const wrapper = shallow( 153 <ShareObjectModal {...props} shareObject={shareObject} /> 154 ) 155 wrapper.setState({ 156 expiry: { 157 days: 1, 158 hours: 23, 159 minutes: 59 160 } 161 }) 162 wrapper.find("#increase-hours").simulate("click") 163 expect(wrapper.state("expiry").hours).toBe(23) 164 wrapper.find("#increase-minutes").simulate("click") 165 expect(wrapper.state("expiry").minutes).toBe(59) 166 expect(shareObject).not.toHaveBeenCalled() 167 }) 168 169 it("should set hours and minutes to 0 when days reaches max", () => { 170 const shareObject = jest.fn() 171 const wrapper = shallow( 172 <ShareObjectModal {...props} shareObject={shareObject} /> 173 ) 174 wrapper.setState({ 175 expiry: { 176 days: 6, 177 hours: 5, 178 minutes: 30 179 } 180 }) 181 wrapper.find("#increase-days").simulate("click") 182 expect(wrapper.state("expiry")).toEqual({ 183 days: 7, 184 hours: 0, 185 minutes: 0 186 }) 187 expect(shareObject).toHaveBeenCalled() 188 }) 189 190 it("should set days to MAX when all of them becomes 0", () => { 191 const shareObject = jest.fn() 192 const wrapper = shallow( 193 <ShareObjectModal {...props} shareObject={shareObject} /> 194 ) 195 wrapper.setState({ 196 expiry: { 197 days: 0, 198 hours: 1, 199 minutes: 0 200 } 201 }) 202 wrapper.find("#decrease-hours").simulate("click") 203 expect(wrapper.state("expiry")).toEqual({ 204 days: 7, 205 hours: 0, 206 minutes: 0 207 }) 208 expect(shareObject).toHaveBeenCalledWith("obj1", 7, 0, 0) 209 }) 210 }) 211 })