storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/browser/app/js/uploads/__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 uploadsActions from "../actions"
    20  
    21  const middlewares = [thunk]
    22  const mockStore = configureStore(middlewares)
    23  
    24  describe("Uploads actions", () => {
    25    it("creates uploads/ADD action", () => {
    26      const store = mockStore()
    27      const expectedActions = [
    28        {
    29          type: "uploads/ADD",
    30          slug: "a-b-c",
    31          size: 100,
    32          name: "test"
    33        }
    34      ]
    35      store.dispatch(uploadsActions.add("a-b-c", 100, "test"))
    36      const actions = store.getActions()
    37      expect(actions).toEqual(expectedActions)
    38    })
    39  
    40    it("creates uploads/UPDATE_PROGRESS action", () => {
    41      const store = mockStore()
    42      const expectedActions = [
    43        {
    44          type: "uploads/UPDATE_PROGRESS",
    45          slug: "a-b-c",
    46          loaded: 50
    47        }
    48      ]
    49      store.dispatch(uploadsActions.updateProgress("a-b-c", 50))
    50      const actions = store.getActions()
    51      expect(actions).toEqual(expectedActions)
    52    })
    53  
    54    it("creates uploads/STOP action", () => {
    55      const store = mockStore()
    56      const expectedActions = [
    57        {
    58          type: "uploads/STOP",
    59          slug: "a-b-c"
    60        }
    61      ]
    62      store.dispatch(uploadsActions.stop("a-b-c"))
    63      const actions = store.getActions()
    64      expect(actions).toEqual(expectedActions)
    65    })
    66  
    67    it("creates uploads/SHOW_ABORT_MODAL action", () => {
    68      const store = mockStore()
    69      const expectedActions = [
    70        {
    71          type: "uploads/SHOW_ABORT_MODAL",
    72          show: true
    73        }
    74      ]
    75      store.dispatch(uploadsActions.showAbortModal())
    76      const actions = store.getActions()
    77      expect(actions).toEqual(expectedActions)
    78    })
    79  
    80    describe("uploadFile", () => {
    81      const file = new Blob(["file content"], {
    82        type: "text/plain"
    83      })
    84      file.name = "file1"
    85  
    86      it("creates alerts/SET action when currentBucket is not present", () => {
    87        const store = mockStore({
    88          buckets: { currentBucket: "" }
    89        })
    90        const expectedActions = [
    91          {
    92            type: "alert/SET",
    93            alert: {
    94              id: 0,
    95              type: "danger",
    96              message: "Please choose a bucket before trying to upload files."
    97            }
    98          }
    99        ]
   100        const file = new Blob(["file content"], { type: "text/plain" })
   101        store.dispatch(uploadsActions.uploadFile(file))
   102        const actions = store.getActions()
   103        expect(actions).toEqual(expectedActions)
   104      })
   105  
   106      it("creates uploads/ADD action before uploading the file", () => {
   107        const store = mockStore({
   108          buckets: { currentBucket: "test1" },
   109          objects: { currentPrefix: "pre1/" }
   110        })
   111        const expectedActions = [
   112          {
   113            type: "uploads/ADD",
   114            slug: "test1-pre1/-file1",
   115            size: file.size,
   116            name: file.name
   117          }
   118        ]
   119        store.dispatch(uploadsActions.uploadFile(file))
   120        const actions = store.getActions()
   121        expect(actions).toEqual(expectedActions)
   122      })
   123  
   124      it("should open and send XMLHttpRequest", () => {
   125        const open = jest.fn()
   126        const send = jest.fn()
   127        const xhrMockClass = () => ({
   128          open: open,
   129          send: send,
   130          setRequestHeader: jest.fn(),
   131          upload: {
   132            addEventListener: jest.fn()
   133          }
   134        })
   135        window.XMLHttpRequest = jest.fn().mockImplementation(xhrMockClass)
   136        const store = mockStore({
   137          buckets: { currentBucket: "test1" },
   138          objects: { currentPrefix: "pre1/" }
   139        })
   140        store.dispatch(uploadsActions.uploadFile(file))
   141        const objectPath = encodeURIComponent("pre1/file1")
   142        expect(open).toHaveBeenCalledWith(
   143          "PUT",
   144          "https://localhost:8080/upload/test1/" + objectPath,
   145          true
   146        )
   147        expect(send).toHaveBeenCalledWith(file)
   148      })
   149    })
   150  
   151    it("creates uploads/STOP and uploads/SHOW_ABORT_MODAL after abortUpload", () => {
   152      const store = mockStore()
   153      const expectedActions = [
   154        {
   155          type: "uploads/STOP",
   156          slug: "a-b/-c"
   157        },
   158        {
   159          type: "uploads/SHOW_ABORT_MODAL",
   160          show: false
   161        }
   162      ]
   163      store.dispatch(uploadsActions.abortUpload("a-b/-c"))
   164      const actions = store.getActions()
   165      expect(actions).toEqual(expectedActions)
   166    })
   167  })