#392: update defaultState into a function

This commit is contained in:
dave 2019-02-28 14:27:47 -05:00
parent ba2e05bc63
commit f5adb62e2e
2 changed files with 19 additions and 19 deletions

View file

@ -1,4 +1,4 @@
import { remove, slice, each, find, maxBy, minBy, merge, last, isArray, cloneDeep } from 'lodash' import { remove, slice, each, find, maxBy, minBy, merge, last, isArray } from 'lodash'
import apiService from '../services/api/api.service.js' import apiService from '../services/api/api.service.js'
// import parse from '../services/status_parser/status_parser.js' // import parse from '../services/status_parser/status_parser.js'
@ -18,7 +18,7 @@ const emptyTl = (userId = 0) => ({
flushMarker: 0 flushMarker: 0
}) })
export const defaultState = { export const defaultState = () => ({
allStatuses: [], allStatuses: [],
allStatusesObject: {}, allStatusesObject: {},
maxId: 0, maxId: 0,
@ -45,7 +45,7 @@ export const defaultState = {
tag: emptyTl(), tag: emptyTl(),
dms: emptyTl() dms: emptyTl()
} }
} })
export const prepareStatus = (status) => { export const prepareStatus = (status) => {
// Set deleted flag // Set deleted flag
@ -326,8 +326,9 @@ export const mutations = {
state.notifications.fetcherId = fetcherId state.notifications.fetcherId = fetcherId
}, },
resetStatuses (state) { resetStatuses (state) {
const emptyState = defaultState()
Object.keys(state).forEach(key => { Object.keys(state).forEach(key => {
state[key] = cloneDeep(defaultState[key]) state[key] = emptyState[key]
}) })
}, },
clearTimeline (state, { timeline }) { clearTimeline (state, { timeline }) {
@ -380,7 +381,7 @@ export const mutations = {
} }
const statuses = { const statuses = {
state: cloneDeep(defaultState), state: defaultState(),
actions: { actions: {
addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline = false, noIdUpdate = false, userId }) { addNewStatuses ({ rootState, commit }, { statuses, showImmediately = false, timeline = false, noIdUpdate = false, userId }) {
commit('addNewStatuses', { statuses, showImmediately, timeline, noIdUpdate, user: rootState.users.currentUser, userId }) commit('addNewStatuses', { statuses, showImmediately, timeline, noIdUpdate, user: rootState.users.currentUser, userId })

View file

@ -1,4 +1,3 @@
import { cloneDeep } from 'lodash'
import { defaultState, mutations, prepareStatus } from '../../../../src/modules/statuses.js' import { defaultState, mutations, prepareStatus } from '../../../../src/modules/statuses.js'
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
@ -24,7 +23,7 @@ describe('Statuses.prepareStatus', () => {
describe('The Statuses module', () => { describe('The Statuses module', () => {
it('adds the status to allStatuses and to the given timeline', () => { it('adds the status to allStatuses and to the given timeline', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' })
@ -36,7 +35,7 @@ describe('The Statuses module', () => {
}) })
it('counts the status as new if it has not been seen on this timeline', () => { it('counts the status as new if it has not been seen on this timeline', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' }) mutations.addNewStatuses(state, { statuses: [status], timeline: 'public' })
@ -54,7 +53,7 @@ describe('The Statuses module', () => {
}) })
it('add the statuses to allStatuses if no timeline is given', () => { it('add the statuses to allStatuses if no timeline is given', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
mutations.addNewStatuses(state, { statuses: [status] }) mutations.addNewStatuses(state, { statuses: [status] })
@ -66,7 +65,7 @@ describe('The Statuses module', () => {
}) })
it('adds the status to allStatuses and to the given timeline, directly visible', () => { it('adds the status to allStatuses and to the given timeline, directly visible', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' }) mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
@ -78,7 +77,7 @@ describe('The Statuses module', () => {
}) })
it('removes statuses by tag on deletion', () => { it('removes statuses by tag on deletion', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
const otherStatus = makeMockStatus({id: '3'}) const otherStatus = makeMockStatus({id: '3'})
status.uri = 'xxx' status.uri = 'xxx'
@ -96,7 +95,7 @@ describe('The Statuses module', () => {
}) })
it('does not update the maxId when the noIdUpdate flag is set', () => { it('does not update the maxId when the noIdUpdate flag is set', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
const secondStatus = makeMockStatus({id: '2'}) const secondStatus = makeMockStatus({id: '2'})
@ -110,7 +109,7 @@ describe('The Statuses module', () => {
}) })
it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => { it('keeps a descending by id order in timeline.visibleStatuses and timeline.statuses', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const nonVisibleStatus = makeMockStatus({id: '1'}) const nonVisibleStatus = makeMockStatus({id: '1'})
const status = makeMockStatus({id: '3'}) const status = makeMockStatus({id: '3'})
const statusTwo = makeMockStatus({id: '2'}) const statusTwo = makeMockStatus({id: '2'})
@ -130,7 +129,7 @@ describe('The Statuses module', () => {
}) })
it('splits retweets from their status and links them', () => { it('splits retweets from their status and links them', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
const retweet = makeMockStatus({id: '2', type: 'retweet'}) const retweet = makeMockStatus({id: '2', type: 'retweet'})
const modStatus = makeMockStatus({id: '1', text: 'something else'}) const modStatus = makeMockStatus({id: '1', text: 'something else'})
@ -155,7 +154,7 @@ describe('The Statuses module', () => {
}) })
it('replaces existing statuses with the same id', () => { it('replaces existing statuses with the same id', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
const modStatus = makeMockStatus({id: '1', text: 'something else'}) const modStatus = makeMockStatus({id: '1', text: 'something else'})
@ -172,7 +171,7 @@ describe('The Statuses module', () => {
}) })
it('replaces existing statuses with the same id, coming from a retweet', () => { it('replaces existing statuses with the same id, coming from a retweet', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
const modStatus = makeMockStatus({id: '1', text: 'something else'}) const modStatus = makeMockStatus({id: '1', text: 'something else'})
const retweet = makeMockStatus({id: '2', type: 'retweet'}) const retweet = makeMockStatus({id: '2', type: 'retweet'})
@ -193,7 +192,7 @@ describe('The Statuses module', () => {
}) })
it('handles favorite actions', () => { it('handles favorite actions', () => {
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
const favorite = { const favorite = {
@ -241,7 +240,7 @@ describe('The Statuses module', () => {
}) })
it('keeps userId when clearing user timeline', () => { it('keeps userId when clearing user timeline', () => {
const state = cloneDeep(defaultState) const state = defaultState()
state.timelines.user.userId = 123 state.timelines.user.userId = 123
mutations.clearTimeline(state, { timeline: 'user' }) mutations.clearTimeline(state, { timeline: 'user' })
@ -252,7 +251,7 @@ describe('The Statuses module', () => {
describe('notifications', () => { describe('notifications', () => {
it('removes a notification when the notice gets removed', () => { it('removes a notification when the notice gets removed', () => {
const user = { id: '1' } const user = { id: '1' }
const state = cloneDeep(defaultState) const state = defaultState()
const status = makeMockStatus({id: '1'}) const status = makeMockStatus({id: '1'})
const otherStatus = makeMockStatus({id: '3'}) const otherStatus = makeMockStatus({id: '3'})
const mentionedStatus = makeMockStatus({id: '2'}) const mentionedStatus = makeMockStatus({id: '2'})