Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
feat: allow plugins to clear all options at once (#1815)
Browse files Browse the repository at this point in the history
  • Loading branch information
dated authored Mar 14, 2020
1 parent 0b4d5ba commit 194a788
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 44 deletions.
118 changes: 74 additions & 44 deletions __tests__/unit/services/plugin-manager/sandbox/storage-sandbox.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ beforeAll(() => {
}),
'session/profileId': '1'
},
dispatch: jest.fn((_, data) => {
if (!db[data.profileId]) {
db[data.profileId] = {}
dispatch: jest.fn((action, data) => {
if (action === 'plugin/setPluginOption') {
if (!db[data.profileId]) {
db[data.profileId] = {}
}
db[data.profileId][data.key] = data.value
} else if (action === 'plugin/deletePluginOptionsForProfile') {
delete db[data.profileId]
}
db[data.profileId][data.key] = data.value
})
}
}
Expand Down Expand Up @@ -55,54 +59,80 @@ describe('Storage Sandbox', () => {
expect(walletApi.storage.getOptions).toBeTruthy()
expect(walletApi.storage.get).toBeTruthy()
expect(walletApi.storage.set).toBeTruthy()
expect(walletApi.storage.clear).toBeTruthy()
})

it('should set a value to key', () => {
walletApi.storage.set(localOptions.key, localOptions.value)
expect(app.$store.dispatch).toHaveBeenCalledWith('plugin/setPluginOption', localOptions)
})

it('should set a value to global key', () => {
walletApi.storage.set(globalOptions.key, globalOptions.value, true)
expect(app.$store.dispatch).toHaveBeenCalledWith('plugin/setPluginOption', globalOptions)
})

it('should get the value of key', () => {
const result = walletApi.storage.get(localOptions.key)
expect(app.$store.getters['plugin/pluginOptions']).toHaveBeenCalledWith(plugin.config.id, '1')
expect(result).toBe(localOptions.value)
})

it('should get the value of global key from the same profile', () => {
const result = walletApi.storage.get(globalOptions.key, true)
expect(result).toBe(globalOptions.value)
})
describe('set', () => {
it('should set a value to key', () => {
walletApi.storage.set(localOptions.key, localOptions.value)
expect(app.$store.dispatch).toHaveBeenCalledWith('plugin/setPluginOption', localOptions)
})

it('should get the value of global key from a different profile', () => {
app.$store.getters['session/profileId'] = '2'
const result = walletApi.storage.get(globalOptions.key, true)
expect(result).toBe(globalOptions.value)
it('should set a value to global key', () => {
walletApi.storage.set(globalOptions.key, globalOptions.value, true)
expect(app.$store.dispatch).toHaveBeenCalledWith('plugin/setPluginOption', globalOptions)
})
})

it('should NOT get the value of a local key from a different profile', () => {
app.$store.getters['session/profileId'] = '2'
const result = walletApi.storage.get(localOptions.key)
expect(result).toBe(undefined)
describe('get', () => {
it('should get the value of key', () => {
const result = walletApi.storage.get(localOptions.key)
expect(app.$store.getters['plugin/pluginOptions']).toHaveBeenCalledWith(plugin.config.id, '1')
expect(result).toBe(localOptions.value)
})

it('should get the value of global key from the same profile', () => {
const result = walletApi.storage.get(globalOptions.key, true)
expect(result).toBe(globalOptions.value)
})

it('should get the value of global key from a different profile', () => {
app.$store.getters['session/profileId'] = '2'
const result = walletApi.storage.get(globalOptions.key, true)
expect(result).toBe(globalOptions.value)
})

it('should NOT get the value of a local key from a different profile', () => {
app.$store.getters['session/profileId'] = '2'
const result = walletApi.storage.get(localOptions.key)
expect(result).toBe(undefined)
})
})

it('should get all local values', () => {
app.$store.getters['session/profileId'] = '1'
let result = walletApi.storage.getOptions()
expect(Object.keys(result)).toHaveLength(1)
expect(result).toHaveProperty(localOptions.key, localOptions.value)
app.$store.getters['session/profileId'] = '2'
result = walletApi.storage.getOptions()
expect(Object.keys(result)).toHaveLength(0)
describe('getOptions', () => {
it('should get all local values', () => {
app.$store.getters['session/profileId'] = '1'
let result = walletApi.storage.getOptions()
expect(Object.keys(result)).toHaveLength(1)
expect(result).toHaveProperty(localOptions.key, localOptions.value)
app.$store.getters['session/profileId'] = '2'
result = walletApi.storage.getOptions()
expect(Object.keys(result)).toHaveLength(0)
})

it('should get all global values', () => {
const result = walletApi.storage.getOptions(true)
expect(Object.keys(result)).toHaveLength(1)
expect(result).toHaveProperty(globalOptions.key, globalOptions.value)
})
})

it('should get all global values', () => {
const result = walletApi.storage.getOptions(true)
expect(Object.keys(result)).toHaveLength(1)
expect(result).toHaveProperty(globalOptions.key, globalOptions.value)
describe('clear', () => {
beforeAll(() => {
walletApi.storage.set(localOptions.key, localOptions.value)
walletApi.storage.set(globalOptions.key, globalOptions.value, true)
})

it('should clear all local values', () => {
expect(walletApi.storage.get(localOptions.key)).toBe(localOptions.value)
walletApi.storage.clear()
expect(walletApi.storage.get(localOptions.key)).toBe(undefined)
})

it('should clear all global values', () => {
expect(walletApi.storage.get(globalOptions.key, true)).toBe(globalOptions.value)
walletApi.storage.clear(true)
expect(walletApi.storage.get(globalOptions.key, true)).toBe(undefined)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export function create (walletApi, app, plugin) {
})
},

clear: (global = false) => {
app.$store.dispatch('plugin/deletePluginOptionsForProfile', {
profileId: global ? 'global' : app.$store.getters['session/profileId'],
pluginId: plugin.config.id
})
},

getOptions
}
}
Expand Down

0 comments on commit 194a788

Please sign in to comment.