storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/objects/__tests__/Path.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 { Path } from "../Path"
    20  
    21  describe("Path", () => {
    22    it("should render without crashing", () => {
    23      shallow(<Path currentBucket={"test1"} currentPrefix={"test2"} />)
    24    })
    25  
    26    it("should render only bucket if there is no prefix", () => {
    27      const wrapper = shallow(<Path currentBucket={"test1"} currentPrefix={""} />)
    28      expect(wrapper.find("span").length).toBe(1)
    29      expect(
    30        wrapper
    31          .find("span")
    32          .at(0)
    33          .text()
    34      ).toBe("test1")
    35    })
    36  
    37    it("should render bucket and prefix", () => {
    38      const wrapper = shallow(
    39        <Path currentBucket={"test1"} currentPrefix={"a/b/"} />
    40      )
    41      expect(wrapper.find("span").length).toBe(3)
    42      expect(
    43        wrapper
    44          .find("span")
    45          .at(0)
    46          .text()
    47      ).toBe("test1")
    48      expect(
    49        wrapper
    50          .find("span")
    51          .at(1)
    52          .text()
    53      ).toBe("a")
    54      expect(
    55        wrapper
    56          .find("span")
    57          .at(2)
    58          .text()
    59      ).toBe("b")
    60    })
    61  
    62    it("should call selectPrefix when a prefix part is clicked", () => {
    63      const selectPrefix = jest.fn()
    64      const wrapper = shallow(
    65        <Path
    66          currentBucket={"test1"}
    67          currentPrefix={"a/b/"}
    68          selectPrefix={selectPrefix}
    69        />
    70      )
    71      wrapper
    72        .find("a")
    73        .at(2)
    74        .simulate("click", { preventDefault: jest.fn() })
    75      expect(selectPrefix).toHaveBeenCalledWith("a/b/")
    76    })
    77  
    78    it("should switch to input mode when edit icon is clicked", () => {
    79      const wrapper = mount(<Path currentBucket={"test1"} currentPrefix={""} />)
    80      wrapper.find(".fe-edit").simulate("click", { preventDefault: jest.fn() })
    81      expect(wrapper.find(".form-control--path").exists()).toBeTruthy()
    82    })
    83  
    84    it("should navigate to prefix when user types path for existing bucket", () => {
    85      const selectBucket = jest.fn()
    86      const buckets = ["test1", "test2"]
    87      const wrapper = mount(
    88        <Path
    89          buckets={buckets}
    90          currentBucket={"test1"}
    91          currentPrefix={""}
    92          selectBucket={selectBucket}
    93        />
    94      )
    95      wrapper.setState({
    96        isEditing: true,
    97        path: "test2/dir1/"
    98      })
    99      wrapper.find("form").simulate("submit", { preventDefault: jest.fn() })
   100      expect(selectBucket).toHaveBeenCalledWith("test2", "dir1/")
   101    })
   102  
   103    it("should create a new bucket if bucket typed in path doesn't exist", () => {
   104      const makeBucket = jest.fn()
   105      const buckets = ["test1", "test2"]
   106      const wrapper = mount(
   107        <Path
   108          buckets={buckets}
   109          currentBucket={"test1"}
   110          currentPrefix={""}
   111          makeBucket={makeBucket}
   112        />
   113      )
   114      wrapper.setState({
   115        isEditing: true,
   116        path: "test3/dir1/"
   117      })
   118      wrapper.find("form").simulate("submit", { preventDefault: jest.fn() })
   119      expect(makeBucket).toHaveBeenCalledWith("test3")
   120    })
   121  
   122    it("should not make or select bucket if path doesn't point to bucket", () => {
   123      const makeBucket = jest.fn()
   124      const selectBucket = jest.fn()
   125      const buckets = ["test1", "test2"]
   126      const wrapper = mount(
   127        <Path
   128          buckets={buckets}
   129          currentBucket={"test1"}
   130          currentPrefix={""}
   131          makeBucket={makeBucket}
   132          selectBucket={selectBucket}
   133        />
   134      )
   135      wrapper.setState({
   136        isEditing: true,
   137        path: "//dir1/dir2/"
   138      })
   139      wrapper.find("form").simulate("submit", { preventDefault: jest.fn() })
   140      expect(makeBucket).not.toHaveBeenCalled()
   141      expect(selectBucket).not.toHaveBeenCalled()
   142    })
   143  })