storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/buckets/__tests__/PolicyInput.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 { PolicyInput } from "../PolicyInput"
    20  import { READ_ONLY, WRITE_ONLY, READ_WRITE } from "../../constants"
    21  import web from "../../web"
    22  
    23  jest.mock("../../web", () => ({
    24    SetBucketPolicy: jest.fn(() => {
    25      return Promise.resolve()
    26    })
    27  }))
    28  
    29  describe("PolicyInput", () => {
    30    it("should render without crashing", () => {
    31      const fetchPolicies = jest.fn()
    32      shallow(<PolicyInput currentBucket={"bucket"} fetchPolicies={fetchPolicies}/>)
    33    })
    34  
    35    it("should call fetchPolicies after the component has mounted", () => {
    36      const fetchPolicies = jest.fn()
    37      const wrapper = shallow(
    38        <PolicyInput currentBucket={"bucket"} fetchPolicies={fetchPolicies} />
    39      )
    40      setImmediate(() => {
    41        expect(fetchPolicies).toHaveBeenCalled()
    42      })
    43    })
    44  
    45    it("should call web.setBucketPolicy and fetchPolicies on submit", () => {
    46      const fetchPolicies = jest.fn()
    47      const wrapper = shallow(
    48        <PolicyInput currentBucket={"bucket"} policies={[]} fetchPolicies={fetchPolicies}/>
    49      )
    50      wrapper.instance().prefix = { value: "baz" }
    51      wrapper.instance().policy = { value: READ_ONLY }
    52      wrapper.find("button").simulate("click", { preventDefault: jest.fn() })
    53  
    54      expect(web.SetBucketPolicy).toHaveBeenCalledWith({
    55        bucketName: "bucket",
    56        prefix: "baz",
    57        policy: READ_ONLY
    58      })
    59  
    60      setImmediate(() => {
    61        expect(fetchPolicies).toHaveBeenCalledWith("bucket")
    62      })
    63    })
    64  
    65    it("should change the prefix '*' to an empty string", () => {
    66      const fetchPolicies = jest.fn()
    67      const wrapper = shallow(
    68        <PolicyInput currentBucket={"bucket"} policies={[]} fetchPolicies={fetchPolicies}/>
    69      )
    70      wrapper.instance().prefix = { value: "*" }
    71      wrapper.instance().policy = { value: READ_ONLY }
    72  
    73      wrapper.find("button").simulate("click", { preventDefault: jest.fn() })
    74  
    75      expect(wrapper.instance().prefix).toEqual({ value: "" })
    76    })
    77  })