Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add draft messages store (#7476)
- Loading branch information
Showing
8 changed files
with
161 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Vue from 'vue'; | ||
import types from '../mutation-types'; | ||
|
||
import { LocalStorage } from 'shared/helpers/localStorage'; | ||
import { LOCAL_STORAGE_KEYS } from 'dashboard/constants/localStorage'; | ||
|
||
const state = { | ||
records: LocalStorage.get(LOCAL_STORAGE_KEYS.DRAFT_MESSAGES) || {}, | ||
}; | ||
|
||
export const getters = { | ||
get: _state => key => { | ||
return _state.records[key] || ''; | ||
}, | ||
}; | ||
|
||
export const actions = { | ||
set: async ({ commit }, { key, message }) => { | ||
commit(types.SET_DRAFT_MESSAGES, { key, message }); | ||
}, | ||
delete: ({ commit }, { key }) => { | ||
commit(types.SET_DRAFT_MESSAGES, { key }); | ||
}, | ||
}; | ||
|
||
export const mutations = { | ||
[types.SET_DRAFT_MESSAGES]($state, { key, message }) { | ||
Vue.set($state.records, key, message); | ||
LocalStorage.set(LOCAL_STORAGE_KEYS.DRAFT_MESSAGES, $state.records); | ||
}, | ||
[types.REMOVE_DRAFT_MESSAGES]($state, { key }) { | ||
const { [key]: draftToBeRemoved, ...updatedRecords } = $state.records; | ||
Vue.set($state, 'records', updatedRecords); | ||
LocalStorage.set(LOCAL_STORAGE_KEYS.DRAFT_MESSAGES, $state.records); | ||
}, | ||
}; | ||
|
||
export default { | ||
namespaced: true, | ||
state, | ||
getters, | ||
actions, | ||
mutations, | ||
}; |
54 changes: 54 additions & 0 deletions
54
app/javascript/dashboard/store/modules/specs/draftMessages/actions.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import axios from 'axios'; | ||
import { actions } from '../../draftMessages'; | ||
import types from '../../../mutation-types'; | ||
|
||
const commit = jest.fn(); | ||
global.axios = axios; | ||
jest.mock('axios'); | ||
|
||
describe('#actions', () => { | ||
describe('#set', () => { | ||
it('sends correct actions', async () => { | ||
await actions.set( | ||
{ | ||
commit, | ||
state: { | ||
draftMessages: {}, | ||
}, | ||
}, | ||
{ key: 'draft-32-REPLY', message: 'Hey how ' } | ||
); | ||
expect(commit.mock.calls).toEqual([ | ||
[ | ||
types.SET_DRAFT_MESSAGES, | ||
{ | ||
key: 'draft-32-REPLY', | ||
message: 'Hey how ', | ||
}, | ||
], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('#delete', () => { | ||
it('sends correct actions', async () => { | ||
await actions.delete( | ||
{ | ||
commit, | ||
state: { | ||
draftMessages: {}, | ||
}, | ||
}, | ||
{ key: 'draft-32-REPLY' } | ||
); | ||
expect(commit.mock.calls).toEqual([ | ||
[ | ||
types.SET_DRAFT_MESSAGES, | ||
{ | ||
key: 'draft-32-REPLY', | ||
}, | ||
], | ||
]); | ||
}); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
app/javascript/dashboard/store/modules/specs/draftMessages/fixtures.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const data = { 'draft-32-REPLY': 'Hey how ', 'draft-31-REPLY': 'Nice' }; |
18 changes: 18 additions & 0 deletions
18
app/javascript/dashboard/store/modules/specs/draftMessages/getters.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getters } from '../../draftMessages'; | ||
import { data } from './fixtures'; | ||
|
||
describe('#getters', () => { | ||
it('return the payload if key is present', () => { | ||
const state = { | ||
records: data, | ||
}; | ||
expect(getters.get(state)('draft-32-REPLY')).toEqual('Hey how '); | ||
}); | ||
|
||
it('return empty string if key is not present', () => { | ||
const state = { | ||
records: data, | ||
}; | ||
expect(getters.get(state)('draft-22-REPLY')).toEqual(''); | ||
}); | ||
}); |
33 changes: 33 additions & 0 deletions
33
app/javascript/dashboard/store/modules/specs/draftMessages/mutations.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import types from '../../../mutation-types'; | ||
import { mutations } from '../../draftMessages'; | ||
|
||
describe('#mutations', () => { | ||
describe('#SET_DRAFT_MESSAGES', () => { | ||
it('sets the draft messages', () => { | ||
const state = { | ||
records: {}, | ||
}; | ||
mutations[types.SET_DRAFT_MESSAGES](state, { | ||
key: 'draft-32-REPLY', | ||
message: 'Hey how ', | ||
}); | ||
expect(state.records).toEqual({ | ||
'draft-32-REPLY': 'Hey how ', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#REMOVE_DRAFT_MESSAGES', () => { | ||
it('removes the draft messages', () => { | ||
const state = { | ||
records: { | ||
'draft-32-REPLY': 'Hey how ', | ||
}, | ||
}; | ||
mutations[types.REMOVE_DRAFT_MESSAGES](state, { | ||
key: 'draft-32-REPLY', | ||
}); | ||
expect(state.records).toEqual({}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters