fix(webapp): query categories on login to get the count (#8542)

* fix(webapp): queru categories to get the count

* padd the weapper to fill in
This commit is contained in:
Moriz Wahl 2025-05-12 12:53:22 +02:00 committed by GitHub
parent 92edde02d8
commit 470ea3f23e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 7 deletions

View File

@ -15,6 +15,11 @@ const stubs = {
} }
const authUserMock = jest.fn().mockReturnValue({ activeCategories: [] }) const authUserMock = jest.fn().mockReturnValue({ activeCategories: [] })
const apolloQueryMock = jest.fn().mockResolvedValue({
data: {
Category: [{ id: 'cat0' }, { id: 'cat1' }, { id: 'cat2' }, { id: 'cat3' }, { id: 'cat4' }],
},
})
describe('LoginForm', () => { describe('LoginForm', () => {
let mocks let mocks
@ -47,6 +52,9 @@ describe('LoginForm', () => {
success: jest.fn(), success: jest.fn(),
error: jest.fn(), error: jest.fn(),
}, },
$apollo: {
query: apolloQueryMock,
},
} }
return mount(LoginForm, { mocks, localVue, propsData, store, stubs }) return mount(LoginForm, { mocks, localVue, propsData, store, stubs })
} }
@ -74,7 +82,9 @@ describe('LoginForm', () => {
describe('no categories saved', () => { describe('no categories saved', () => {
it('resets the categories', async () => { it('resets the categories', async () => {
await fillIn(Wrapper()) const wrapper = Wrapper()
await fillIn(wrapper)
await wrapper.vm.$nextTick()
expect(storeMocks.mutations['posts/RESET_CATEGORIES']).toHaveBeenCalled() expect(storeMocks.mutations['posts/RESET_CATEGORIES']).toHaveBeenCalled()
expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).not.toHaveBeenCalled() expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).not.toHaveBeenCalled()
}) })
@ -82,13 +92,28 @@ describe('LoginForm', () => {
describe('categories saved', () => { describe('categories saved', () => {
it('sets the categories', async () => { it('sets the categories', async () => {
authUserMock.mockReturnValue({ activeCategories: ['cat1', 'cat9', 'cat12'] }) authUserMock.mockReturnValue({ activeCategories: ['cat0', 'cat2', 'cat4'] })
await fillIn(Wrapper()) const wrapper = Wrapper()
await fillIn(wrapper)
await wrapper.vm.$nextTick()
expect(storeMocks.mutations['posts/RESET_CATEGORIES']).toHaveBeenCalled() expect(storeMocks.mutations['posts/RESET_CATEGORIES']).toHaveBeenCalled()
expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledTimes(3) expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledTimes(3)
expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat1') expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat0')
expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat9') expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat2')
expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat12') expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat4')
})
})
describe('all categories saved', () => {
it('resets the categories', async () => {
authUserMock.mockReturnValue({
activeCategories: ['cat0', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5'],
})
const wrapper = Wrapper()
await fillIn(wrapper)
await wrapper.vm.$nextTick()
expect(storeMocks.mutations['posts/RESET_CATEGORIES']).toHaveBeenCalled()
expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).not.toHaveBeenCalled()
}) })
}) })
}) })

View File

@ -59,6 +59,7 @@ import LocaleSwitch from '~/components/LocaleSwitch/LocaleSwitch'
import Logo from '~/components/Logo/Logo' import Logo from '~/components/Logo/Logo'
import ShowPassword from '../ShowPassword/ShowPassword.vue' import ShowPassword from '../ShowPassword/ShowPassword.vue'
import { mapGetters, mapMutations } from 'vuex' import { mapGetters, mapMutations } from 'vuex'
import CategoryQuery from '~/graphql/CategoryQuery'
export default { export default {
components: { components: {
@ -98,9 +99,13 @@ export default {
const { email, password } = this.form const { email, password } = this.form
try { try {
await this.$store.dispatch('auth/login', { email, password }) await this.$store.dispatch('auth/login', { email, password })
const result = await this.$apollo.query({
query: CategoryQuery(),
})
const categories = result.data.Category
if (this.currentUser && this.currentUser.activeCategories) { if (this.currentUser && this.currentUser.activeCategories) {
this.resetCategories() this.resetCategories()
if (this.currentUser.activeCategories.length < 19) { if (this.currentUser.activeCategories.length < categories.length) {
this.currentUser.activeCategories.forEach((categoryId) => { this.currentUser.activeCategories.forEach((categoryId) => {
this.toggleCategory(categoryId) this.toggleCategory(categoryId)
}) })