소스 검색

test: use DeepFrozenStore in one end to end test (#2607)

* use DeepFrozenStore in one end to end test

Follow up to #320

* add DeepFrozenStore to some Core unit tests
Artur Paikin 4 년 전
부모
커밋
95e014f572
5개의 변경된 파일73개의 추가작업 그리고 7개의 파일을 삭제
  1. 5 0
      package-lock.json
  2. 2 1
      package.json
  3. 19 5
      packages/@uppy/core/src/index.test.js
  4. 3 1
      test/endtoend/i18n-drag-drop/main.js
  5. 44 0
      test/resources/DeepFrozenStore.js

+ 5 - 0
package-lock.json

@@ -15823,6 +15823,11 @@
       "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==",
       "dev": true
     },
+    "deep-freeze": {
+      "version": "0.0.1",
+      "resolved": "https://registry.npmjs.org/deep-freeze/-/deep-freeze-0.0.1.tgz",
+      "integrity": "sha1-OgsABd4YZygZ39OM0x+RF5yJPoQ="
+    },
     "deep-is": {
       "version": "0.1.3",
       "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.3.tgz",

+ 2 - 1
package.json

@@ -52,6 +52,7 @@
     "@uppy/form": "file:packages/@uppy/form",
     "@uppy/golden-retriever": "file:packages/@uppy/golden-retriever",
     "@uppy/google-drive": "file:packages/@uppy/google-drive",
+    "@uppy/image-editor": "file:packages/@uppy/image-editor",
     "@uppy/informer": "file:packages/@uppy/informer",
     "@uppy/instagram": "file:packages/@uppy/instagram",
     "@uppy/locales": "file:packages/@uppy/locales",
@@ -74,8 +75,8 @@
     "@uppy/utils": "file:packages/@uppy/utils",
     "@uppy/webcam": "file:packages/@uppy/webcam",
     "@uppy/xhr-upload": "file:packages/@uppy/xhr-upload",
-    "@uppy/image-editor": "file:packages/@uppy/image-editor",
     "@uppy/zoom": "file:packages/@uppy/zoom",
+    "deep-freeze": "0.0.1",
     "remark-lint-uppy": "file:private/remark-lint-uppy",
     "uppy": "file:packages/uppy",
     "uppy.io": "file:website"

+ 19 - 5
packages/@uppy/core/src/index.test.js

@@ -8,6 +8,7 @@ const AcquirerPlugin2 = require('../../../../test/mocks/acquirerPlugin2')
 const InvalidPlugin = require('../../../../test/mocks/invalidPlugin')
 const InvalidPluginWithoutId = require('../../../../test/mocks/invalidPluginWithoutId')
 const InvalidPluginWithoutType = require('../../../../test/mocks/invalidPluginWithoutType')
+const DeepFrozenStore = require('../../../../test/resources/DeepFrozenStore.js')
 
 jest.mock('cuid', () => {
   return () => 'cjd09qwxb000dlql4tp4doz8h'
@@ -209,7 +210,10 @@ describe('src/Core', () => {
   })
 
   it('should reset when the reset method is called', () => {
-    const core = new Core()
+    // use DeepFrozenStore in some tests to make sure we are not mutating things
+    const core = new Core({
+      store: DeepFrozenStore()
+    })
     // const corePauseEventMock = jest.fn()
     const coreCancelEventMock = jest.fn()
     const coreStateUpdateEventMock = jest.fn()
@@ -265,7 +269,10 @@ describe('src/Core', () => {
   })
 
   it('should close, reset and uninstall when the close method is called', () => {
-    const core = new Core()
+    // use DeepFrozenStore in some tests to make sure we are not mutating things
+    const core = new Core({
+      store: DeepFrozenStore()
+    })
     core.use(AcquirerPlugin1)
 
     const coreCancelEventMock = jest.fn()
@@ -277,7 +284,6 @@ describe('src/Core', () => {
 
     core.close()
 
-    // expect(corePauseEventMock.mock.calls.length).toEqual(1)
     expect(coreCancelEventMock.mock.calls.length).toEqual(1)
     expect(coreStateUpdateEventMock.mock.calls.length).toEqual(1)
     expect(coreStateUpdateEventMock.mock.calls[0][1]).toEqual({
@@ -834,7 +840,10 @@ describe('src/Core', () => {
     })
 
     it('should return files with errors in the { failed } key', () => {
-      const core = new Core()
+      // use DeepFrozenStore in some tests to make sure we are not mutating things
+      const core = new Core({
+        store: DeepFrozenStore()
+      })
       core.addUploader((fileIDs) => {
         fileIDs.forEach((fileID) => {
           const file = core.getFile(fileID)
@@ -1171,7 +1180,9 @@ describe('src/Core', () => {
 
   describe('meta data', () => {
     it('should set meta data by calling setMeta', () => {
+      // use DeepFrozenStore in some tests to make sure we are not mutating things
       const core = new Core({
+        store: DeepFrozenStore(),
         meta: { foo2: 'bar2' }
       })
       core.setMeta({ foo: 'bar', bur: 'mur' })
@@ -1376,7 +1387,10 @@ describe('src/Core', () => {
     })
 
     it('should calculate the total progress of all file uploads', () => {
-      const core = new Core()
+      // use DeepFrozenStore in some tests to make sure we are not mutating things
+      const core = new Core({
+        store: DeepFrozenStore()
+      })
 
       core.addFile({
         source: 'jest',

+ 3 - 1
test/endtoend/i18n-drag-drop/main.js

@@ -4,11 +4,13 @@ const Uppy = require('@uppy/core')
 const DragDrop = require('@uppy/drag-drop')
 const XHRUpload = require('@uppy/xhr-upload')
 const ProgressBar = require('@uppy/progress-bar')
+const DeepFrozenStore = require('../../resources/DeepFrozenStore.js')
 
 const uppyi18n = new Uppy({
   id: 'uppyi18n',
   debug: true,
-  autoProceed: true
+  autoProceed: true,
+  store: DeepFrozenStore()
 })
 
 uppyi18n

+ 44 - 0
test/resources/DeepFrozenStore.js

@@ -0,0 +1,44 @@
+var deepFreeze = require('deep-freeze')
+
+/**
+ * Default store + deepFreeze on setState to make sure nothing is mutated accidentally
+ */
+class DeepFrozenStore {
+  constructor () {
+    this.state = {}
+    this.callbacks = []
+  }
+
+  getState () {
+    return this.state
+  }
+
+  setState (patch) {
+    const prevState = Object.assign({}, this.state)
+    const nextState = deepFreeze(Object.assign({}, this.state, patch))
+
+    this.state = nextState
+    this._publish(prevState, nextState, patch)
+  }
+
+  subscribe (listener) {
+    this.callbacks.push(listener)
+    return () => {
+      // Remove the listener.
+      this.callbacks.splice(
+        this.callbacks.indexOf(listener),
+        1
+      )
+    }
+  }
+
+  _publish (...args) {
+    this.callbacks.forEach((listener) => {
+      listener(...args)
+    })
+  }
+}
+
+module.exports = function defaultStore () {
+  return new DeepFrozenStore()
+}