Bladeren bron

core,plugins: Update tests for get/setPluginState

Renée Kooi 7 jaren geleden
bovenliggende
commit
7fa7f53af0
2 gewijzigde bestanden met toevoegingen van 53 en 0 verwijderingen
  1. 6 0
      src/core/Core.test.js
  2. 47 0
      src/plugins/Plugin.test.js

+ 6 - 0
src/core/Core.test.js

@@ -147,6 +147,7 @@ describe('src/Core', () => {
         foo: 'baar',
         foo: 'baar',
         info: { isHidden: true, message: '', type: 'info' },
         info: { isHidden: true, message: '', type: 'info' },
         meta: {},
         meta: {},
+        plugins: {},
         totalProgress: 0
         totalProgress: 0
       }
       }
 
 
@@ -168,6 +169,7 @@ describe('src/Core', () => {
         foo: 'bar',
         foo: 'bar',
         info: { isHidden: true, message: '', type: 'info' },
         info: { isHidden: true, message: '', type: 'info' },
         meta: {},
         meta: {},
+        plugins: {},
         totalProgress: 0
         totalProgress: 0
       })
       })
       // new state
       // new state
@@ -178,6 +180,7 @@ describe('src/Core', () => {
         foo: 'baar',
         foo: 'baar',
         info: { isHidden: true, message: '', type: 'info' },
         info: { isHidden: true, message: '', type: 'info' },
         meta: {},
         meta: {},
+        plugins: {},
         totalProgress: 0
         totalProgress: 0
       })
       })
     })
     })
@@ -193,6 +196,7 @@ describe('src/Core', () => {
         foo: 'bar',
         foo: 'bar',
         info: { isHidden: true, message: '', type: 'info' },
         info: { isHidden: true, message: '', type: 'info' },
         meta: {},
         meta: {},
+        plugins: {},
         totalProgress: 0
         totalProgress: 0
       })
       })
     })
     })
@@ -219,6 +223,7 @@ describe('src/Core', () => {
       foo: 'bar',
       foo: 'bar',
       info: { isHidden: true, message: '', type: 'info' },
       info: { isHidden: true, message: '', type: 'info' },
       meta: {},
       meta: {},
+      plugins: {},
       totalProgress: 0
       totalProgress: 0
     })
     })
   })
   })
@@ -244,6 +249,7 @@ describe('src/Core', () => {
       files: {},
       files: {},
       info: { isHidden: true, message: '', type: 'info' },
       info: { isHidden: true, message: '', type: 'info' },
       meta: {},
       meta: {},
+      plugins: {},
       totalProgress: 0
       totalProgress: 0
     })
     })
     expect(core.plugins.acquirer[0].mocks.uninstall.mock.calls.length).toEqual(
     expect(core.plugins.acquirer[0].mocks.uninstall.mock.calls.length).toEqual(

+ 47 - 0
src/plugins/Plugin.test.js

@@ -37,6 +37,53 @@ describe('Plugin', () => {
     expect(typeof plugin.opts).toBe('object')
     expect(typeof plugin.opts).toBe('object')
   })
   })
 
 
+  describe('plugin state', () => {
+    class MockPlugin extends Plugin {
+      constructor (core, opts) {
+        super(core, opts)
+        this.id = 'MockPlugin'
+      }
+    }
+    it('returns plugin state from `getPluginState()`', () => {
+      const mockState = {}
+      plugin = new MockPlugin({
+        state: {
+          plugins: {
+            MockPlugin: mockState
+          }
+        }
+      })
+
+      expect(plugin.getPluginState()).toBe(mockState)
+    })
+
+    it('merges plugin state using `setPluginState()`', () => {
+      const initialState = {
+        plugins: {
+          MockPlugin: {
+            hello: 'world',
+            asdf: 'quux'
+          }
+        }
+      }
+
+      plugin = new MockPlugin({
+        setState (patch) {
+          this.state = Object.assign({}, this.state, patch)
+        },
+        state: initialState
+      })
+
+      plugin.setPluginState({ hello: 'friends' })
+
+      expect(plugin.core.state).not.toBe(initialState)
+      expect(plugin.getPluginState()).toEqual({
+        hello: 'friends',
+        asdf: 'quux'
+      })
+    })
+  })
+
   // it('sets `replaceTargetContent` based on options argument', () => {
   // it('sets `replaceTargetContent` based on options argument', () => {
   //   plugin = new Plugin(null, { replaceTargetContent: false })
   //   plugin = new Plugin(null, { replaceTargetContent: false })
   //   expect(plugin.opts.replaceTargetContent).toBe(false)
   //   expect(plugin.opts.replaceTargetContent).toBe(false)