diff --git a/webapp/components/LoginForm/LoginForm.spec.js b/webapp/components/LoginForm/LoginForm.spec.js index 2004656a5..d3d4a7648 100644 --- a/webapp/components/LoginForm/LoginForm.spec.js +++ b/webapp/components/LoginForm/LoginForm.spec.js @@ -15,6 +15,11 @@ const stubs = { } 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', () => { let mocks @@ -47,6 +52,9 @@ describe('LoginForm', () => { success: jest.fn(), error: jest.fn(), }, + $apollo: { + query: apolloQueryMock, + }, } return mount(LoginForm, { mocks, localVue, propsData, store, stubs }) } @@ -74,7 +82,9 @@ describe('LoginForm', () => { describe('no categories saved', () => { 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/TOGGLE_CATEGORY']).not.toHaveBeenCalled() }) @@ -82,13 +92,28 @@ describe('LoginForm', () => { describe('categories saved', () => { it('sets the categories', async () => { - authUserMock.mockReturnValue({ activeCategories: ['cat1', 'cat9', 'cat12'] }) - await fillIn(Wrapper()) + authUserMock.mockReturnValue({ activeCategories: ['cat0', 'cat2', 'cat4'] }) + const wrapper = Wrapper() + await fillIn(wrapper) + await wrapper.vm.$nextTick() expect(storeMocks.mutations['posts/RESET_CATEGORIES']).toHaveBeenCalled() expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledTimes(3) - expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat1') - expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat9') - expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat12') + expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat0') + expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toHaveBeenCalledWith({}, 'cat2') + 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() }) }) }) diff --git a/webapp/components/LoginForm/LoginForm.vue b/webapp/components/LoginForm/LoginForm.vue index 5bdf356fe..ab64c5a3b 100644 --- a/webapp/components/LoginForm/LoginForm.vue +++ b/webapp/components/LoginForm/LoginForm.vue @@ -59,6 +59,7 @@ import LocaleSwitch from '~/components/LocaleSwitch/LocaleSwitch' import Logo from '~/components/Logo/Logo' import ShowPassword from '../ShowPassword/ShowPassword.vue' import { mapGetters, mapMutations } from 'vuex' +import CategoryQuery from '~/graphql/CategoryQuery' export default { components: { @@ -98,9 +99,13 @@ export default { const { email, password } = this.form try { 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) { this.resetCategories() - if (this.currentUser.activeCategories.length < 19) { + if (this.currentUser.activeCategories.length < categories.length) { this.currentUser.activeCategories.forEach((categoryId) => { this.toggleCategory(categoryId) })