Ocelot-Social/webapp/store/pinnedPosts.spec.js
Moriz Wahl b736a2a2e3
feat(backend): pin more than one post (#8598)
* feat(backend): pin more than one post

* add postPinnedCount query, better names for env variable

* add store and mixin for pinned posts counts

* test pinned post store

* context menu for pin posts

* fix typos

* unpin posts is always possible

---------

Co-authored-by: Wolfgang Huß <wolle.huss@pjannto.com>
2025-05-28 19:12:27 +02:00

112 lines
2.7 KiB
JavaScript

import { state as initialState, mutations, getters, actions } from './pinnedPosts'
import { postsPinnedCountsQuery } from '~/graphql/PostQuery'
describe('pinned post store', () => {
describe('initial state', () => {
it('sets all values to 0', () => {
expect(initialState()).toEqual({
maxPinnedPosts: 0,
currentlyPinnedPosts: 0,
})
})
})
describe('mutations', () => {
let testMutation
const state = {
maxPinnedPosts: 0,
currentlyPinnedPosts: 0,
}
describe('pinPost', () => {
it('increments currentlyPinnedPosts', () => {
testMutation = () => {
mutations.pinPost(state)
return getters.currentlyPinnedPosts(state)
}
expect(testMutation()).toBe(1)
})
})
describe('unpinPost', () => {
it('decrements currentlyPinnedPosts', () => {
state.currentlyPinnedPosts = 2
testMutation = () => {
mutations.unpinPost(state)
return getters.currentlyPinnedPosts(state)
}
expect(testMutation()).toBe(1)
})
})
describe('setMaxPinnedPosts', () => {
it('sets maxPinnedPosts correctly', () => {
state.maxPinnedPosts = 3
testMutation = () => {
mutations.setMaxPinnedPosts(state, 1)
return getters.maxPinnedPosts(state)
}
expect(testMutation()).toBe(1)
})
})
describe('setCurrentlyPinnedPosts', () => {
it('sets currentlyPinnedPosts', () => {
state.currentlyPinnedPosts = 3
testMutation = () => {
mutations.setCurrentlyPinnedPosts(state, 1)
return getters.currentlyPinnedPosts(state)
}
expect(testMutation()).toBe(1)
})
})
})
describe('actions', () => {
const queryMock = jest.fn().mockResolvedValue({
data: {
PostsPinnedCounts: {
maxPinnedPosts: 3,
currentlyPinnedPosts: 2,
},
},
})
const commit = jest.fn()
let action
beforeEach(() => {
const module = {
app: {
apolloProvider: {
defaultClient: {
query: queryMock,
},
},
},
}
action = actions.fetch.bind(module)
})
describe('fetch', () => {
beforeEach(async () => {
await action({ commit })
})
it('calls apollo', () => {
expect(queryMock).toBeCalledWith({
query: postsPinnedCountsQuery(),
})
})
it('commits setMaxPinnedPosts', () => {
expect(commit).toBeCalledWith('setMaxPinnedPosts', 3)
})
it('commits setCurrentlyPinnedPosts', () => {
expect(commit).toBeCalledWith('setCurrentlyPinnedPosts', 2)
})
})
})
})