add filter by posts in my groups to store and test it

This commit is contained in:
Moriz Wahl 2023-03-06 16:00:50 +01:00
parent 77ca09ed04
commit d2462c34f1
2 changed files with 57 additions and 0 deletions

View File

@ -30,6 +30,19 @@ export const mutations = {
}
}
},
TOGGLE_FILTER_BY_MY_GROUPS(state) {
const filter = clone(state.filter)
const status = get(filter, 'postsInMyGroups')
if (status) {
delete filter.postsInMyGroups
state.filter = filter
} else {
state.filter = {
...filter,
postsInMyGroups: true,
}
}
},
RESET_CATEGORIES(state) {
const filter = clone(state.filter)
delete filter.categories_some
@ -84,6 +97,9 @@ export const getters = {
filteredByUsersFollowed(state) {
return !!get(state.filter, 'author.followedBy_some.id')
},
filteredByPostsInMyGroups(state) {
return !!get(state.filter, 'postsInMyGroups')
},
filteredByEmotions(state) {
return get(state.filter, 'emotions_some.emotion_in') || []
},

View File

@ -56,6 +56,18 @@ describe('getters', () => {
})
})
describe('filteredByPostsInMyGroups', () => {
it('returns true if filter is set', () => {
state = { filter: { postsInMyGroups: true } }
expect(getters.filteredByPostsInMyGroups(state)).toBe(true)
})
it('returns false if filter is not set', () => {
state = { filter: { categories_some: { id_in: [23] } } }
expect(getters.filteredByUsersFollowed(state)).toBe(false)
})
})
describe('filteredByEmotions', () => {
it('returns an emotions array if filter is set', () => {
state = { filter: { emotions_some: { emotion_in: ['sad'] } } }
@ -230,6 +242,35 @@ describe('mutations', () => {
})
})
describe('TOGGLE_FILTER_BY_MY_GROUPS', () => {
beforeEach(() => {
testMutation = () => {
mutations.TOGGLE_FILTER_BY_MY_GROUPS(state)
return getters.filter(state)
}
})
describe('given empty filter', () => {
beforeEach(() => {
state = { filter: {} }
})
it('sets postsInMyGroups filter to true', () => {
expect(testMutation()).toEqual({ postsInMyGroups: true })
})
})
describe('already filtered', () => {
beforeEach(() => {
state = { filter: { postsInMyGroups: true } }
})
it('removes postsInMyGroups filter', () => {
expect(testMutation()).toEqual({})
})
})
})
describe('TOGGLE_ORDER', () => {
beforeEach(() => {
testMutation = (key) => {