storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/buckets/__tests__/actions.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 configureStore from "redux-mock-store" 18 import thunk from "redux-thunk" 19 import * as actionsBuckets from "../actions" 20 import * as objectActions from "../../objects/actions" 21 import history from "../../history" 22 23 jest.mock("../../web", () => ({ 24 ListBuckets: jest.fn(() => { 25 return Promise.resolve({ buckets: [{ name: "test1" }, { name: "test2" }] }) 26 }), 27 MakeBucket: jest.fn(() => { 28 return Promise.resolve() 29 }), 30 DeleteBucket: jest.fn(() => { 31 return Promise.resolve() 32 }) 33 })) 34 35 jest.mock("../../objects/actions", () => ({ 36 selectPrefix: () => dispatch => {} 37 })) 38 39 const middlewares = [thunk] 40 const mockStore = configureStore(middlewares) 41 42 describe("Buckets actions", () => { 43 it("creates buckets/SET_LIST and buckets/SET_CURRENT_BUCKET with first bucket after fetching the buckets", () => { 44 const store = mockStore() 45 const expectedActions = [ 46 { type: "buckets/SET_LIST", buckets: ["test1", "test2"] }, 47 { type: "buckets/SET_CURRENT_BUCKET", bucket: "test1" } 48 ] 49 return store.dispatch(actionsBuckets.fetchBuckets()).then(() => { 50 const actions = store.getActions() 51 expect(actions).toEqual(expectedActions) 52 }) 53 }) 54 55 it("creates buckets/SET_CURRENT_BUCKET with bucket name in the url after fetching buckets", () => { 56 history.push("/test2") 57 const store = mockStore() 58 const expectedActions = [ 59 { type: "buckets/SET_LIST", buckets: ["test1", "test2"] }, 60 { type: "buckets/SET_CURRENT_BUCKET", bucket: "test2" } 61 ] 62 window.location 63 return store.dispatch(actionsBuckets.fetchBuckets()).then(() => { 64 const actions = store.getActions() 65 expect(actions).toEqual(expectedActions) 66 }) 67 }) 68 69 it("creates buckets/SET_CURRENT_BUCKET with first bucket when the bucket in url is not exists after fetching buckets", () => { 70 history.push("/test3") 71 const store = mockStore() 72 const expectedActions = [ 73 { type: "buckets/SET_LIST", buckets: ["test1", "test2"] }, 74 { type: "buckets/SET_CURRENT_BUCKET", bucket: "test1" } 75 ] 76 window.location 77 return store.dispatch(actionsBuckets.fetchBuckets()).then(() => { 78 const actions = store.getActions() 79 expect(actions).toEqual(expectedActions) 80 }) 81 }) 82 83 it("creates buckets/SET_CURRENT_BUCKET action when selectBucket is called", () => { 84 const store = mockStore() 85 const expectedActions = [ 86 { type: "buckets/SET_CURRENT_BUCKET", bucket: "test1" } 87 ] 88 store.dispatch(actionsBuckets.selectBucket("test1")) 89 const actions = store.getActions() 90 expect(actions).toEqual(expectedActions) 91 }) 92 93 it("creates buckets/SHOW_MAKE_BUCKET_MODAL for showMakeBucketModal", () => { 94 const store = mockStore() 95 const expectedActions = [ 96 { type: "buckets/SHOW_MAKE_BUCKET_MODAL", show: true } 97 ] 98 store.dispatch(actionsBuckets.showMakeBucketModal()) 99 const actions = store.getActions() 100 expect(actions).toEqual(expectedActions) 101 }) 102 103 it("creates buckets/SHOW_MAKE_BUCKET_MODAL for hideMakeBucketModal", () => { 104 const store = mockStore() 105 const expectedActions = [ 106 { type: "buckets/SHOW_MAKE_BUCKET_MODAL", show: false } 107 ] 108 store.dispatch(actionsBuckets.hideMakeBucketModal()) 109 const actions = store.getActions() 110 expect(actions).toEqual(expectedActions) 111 }) 112 113 it("creates buckets/SHOW_BUCKET_POLICY for showBucketPolicy", () => { 114 const store = mockStore() 115 const expectedActions = [ 116 { type: "buckets/SHOW_BUCKET_POLICY", show: true } 117 ] 118 store.dispatch(actionsBuckets.showBucketPolicy()) 119 const actions = store.getActions() 120 expect(actions).toEqual(expectedActions) 121 }) 122 123 it("creates buckets/SHOW_BUCKET_POLICY for hideBucketPolicy", () => { 124 const store = mockStore() 125 const expectedActions = [ 126 { type: "buckets/SHOW_BUCKET_POLICY", show: false } 127 ] 128 store.dispatch(actionsBuckets.hideBucketPolicy()) 129 const actions = store.getActions() 130 expect(actions).toEqual(expectedActions) 131 }) 132 133 it("creates buckets/SET_POLICIES action", () => { 134 const store = mockStore() 135 const expectedActions = [ 136 { type: "buckets/SET_POLICIES", policies: ["test1", "test2"] } 137 ] 138 store.dispatch(actionsBuckets.setPolicies(["test1", "test2"])) 139 const actions = store.getActions() 140 expect(actions).toEqual(expectedActions) 141 }) 142 143 it("creates buckets/ADD action", () => { 144 const store = mockStore() 145 const expectedActions = [{ type: "buckets/ADD", bucket: "test" }] 146 store.dispatch(actionsBuckets.addBucket("test")) 147 const actions = store.getActions() 148 expect(actions).toEqual(expectedActions) 149 }) 150 151 it("creates buckets/REMOVE action", () => { 152 const store = mockStore() 153 const expectedActions = [{ type: "buckets/REMOVE", bucket: "test" }] 154 store.dispatch(actionsBuckets.removeBucket("test")) 155 const actions = store.getActions() 156 expect(actions).toEqual(expectedActions) 157 }) 158 159 it("creates buckets/ADD and buckets/SET_CURRENT_BUCKET after creating the bucket", () => { 160 const store = mockStore() 161 const expectedActions = [ 162 { type: "buckets/ADD", bucket: "test1" }, 163 { type: "buckets/SET_CURRENT_BUCKET", bucket: "test1" } 164 ] 165 return store.dispatch(actionsBuckets.makeBucket("test1")).then(() => { 166 const actions = store.getActions() 167 expect(actions).toEqual(expectedActions) 168 }) 169 }) 170 171 it("creates alert/SET, buckets/REMOVE, buckets/SET_LIST and buckets/SET_CURRENT_BUCKET " + 172 "after deleting the bucket", () => { 173 const store = mockStore() 174 const expectedActions = [ 175 { type: "alert/SET", alert: {id: 0, message: "Bucket 'test3' has been deleted.", type: "info"} }, 176 { type: "buckets/REMOVE", bucket: "test3" }, 177 { type: "buckets/SET_LIST", buckets: ["test1", "test2"] }, 178 { type: "buckets/SET_CURRENT_BUCKET", bucket: "test1" } 179 ] 180 return store.dispatch(actionsBuckets.deleteBucket("test3")).then(() => { 181 const actions = store.getActions() 182 expect(actions).toEqual(expectedActions) 183 }) 184 }) 185 })