Ocelot-Social/webapp/store/categories.spec.js
Moriz Wahl a3178a91b4
refactor(webapp): store for categories (#8551)
* after authentification, query the categories if active and store them

* get categories from store

* use category store to get categories

* get categories from store

* mock store to have access to categories

* to get rid of the active categories config variable in the frontend, the Category query returns an empty array when categories are not active

* remove CATEGORIES_ACTIVE from .env

* should return string to avoid warnings in console

* replace all env calls for categories active by getter from store

* use categoriesActive getter

* ignore order of returned categories

* mixin to get the category infos from the store, to ensure, that the quey has been called

* fix misspelling

---------

Co-authored-by: Wolfgang Huß <wolle.huss@pjannto.com>
2025-05-27 15:03:26 +02:00

106 lines
2.5 KiB
JavaScript

import { state, mutations, getters, actions } from './categories'
import CategoryQuery from '~/graphql/CategoryQuery'
describe('categories store', () => {
describe('initial state', () => {
it('sets no categories and is not inizialized', () => {
expect(state()).toEqual({
categories: [],
isInitialized: false,
})
})
})
describe('getters', () => {
describe('categoriesActive', () => {
it('returns true if there are categories', () => {
const state = { categories: ['cat1', 'cat2'] }
expect(getters.categoriesActive(state)).toBe(true)
})
it('returns false if there are no categories', () => {
const state = { categories: [] }
expect(getters.categoriesActive(state)).toBe(false)
})
})
})
describe('mutations', () => {
let testMutation
describe('SET_CATEGORIES', () => {
beforeEach(() => {
testMutation = (categories) => {
mutations.SET_CATEGORIES(state, categories)
return getters.categories(state)
}
})
it('sets categories to [] if value is undefined', () => {
expect(testMutation(undefined)).toEqual([])
})
it('sets categories correctly', () => {
expect(testMutation(['cat1', 'cat2', 'cat3'])).toEqual(['cat1', 'cat2', 'cat3'])
})
})
describe('SET_INIZIALIZED', () => {
beforeEach(() => {
testMutation = () => {
mutations.SET_INIZIALIZED(state)
return getters.isInitialized(state)
}
})
it('sets isInitialized to true', () => {
expect(testMutation()).toBe(true)
})
})
})
describe('actions', () => {
const queryMock = jest.fn().mockResolvedValue({
data: {
Category: ['cat1', 'cat2', 'cat3'],
},
})
const commit = jest.fn()
let action
beforeEach(() => {
const module = {
app: {
apolloProvider: {
defaultClient: {
query: queryMock,
},
},
},
}
action = actions.init.bind(module)
})
describe('init', () => {
beforeEach(async () => {
await action({ commit })
})
it('calls apollo', () => {
expect(queryMock).toBeCalledWith({
query: CategoryQuery(),
})
})
it('commits SET_CATEGORIES', () => {
expect(commit).toBeCalledWith('SET_CATEGORIES', ['cat1', 'cat2', 'cat3'])
})
it('commits SET_INIZIALIZED', () => {
expect(commit).toBeCalledWith('SET_INIZIALIZED')
})
})
})
})