From 754251258dd29c0e0522f1ae13e5d14b30c99dee Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 7 Sep 2022 21:50:37 +0200 Subject: [PATCH] test setting of saved categories --- webapp/components/LoginForm/LoginForm.spec.js | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/webapp/components/LoginForm/LoginForm.spec.js b/webapp/components/LoginForm/LoginForm.spec.js index 430fdbe21..4dfdea50a 100644 --- a/webapp/components/LoginForm/LoginForm.spec.js +++ b/webapp/components/LoginForm/LoginForm.spec.js @@ -12,6 +12,8 @@ config.stubs['nuxt-link'] = '' config.stubs['locale-switch'] = '' config.stubs['client-only'] = '' +const authUserMock = jest.fn().mockReturnValue({ activeCategories: [] }) + describe('LoginForm', () => { let mocks let propsData @@ -26,10 +28,15 @@ describe('LoginForm', () => { storeMocks = { getters: { 'auth/pending': () => false, + 'auth/user': authUserMock, }, actions: { 'auth/login': jest.fn(), }, + mutations: { + 'posts/TOGGLE_CATEGORY': jest.fn(), + 'posts/RESET_CATEGORIES': jest.fn(), + }, } const store = new Vuex.Store(storeMocks) mocks = { @@ -43,20 +50,46 @@ describe('LoginForm', () => { } describe('fill in email and password and submit', () => { - const fillIn = (wrapper, opts = {}) => { + const fillIn = async (wrapper, opts = {}) => { const { email = 'email@example.org', password = '1234' } = opts wrapper.find('input[name="email"]').setValue(email) wrapper.find('input[name="password"]').setValue(password) - wrapper.find('form').trigger('submit') + await wrapper.find('form').trigger('submit') } - it('dispatches login with form data', () => { - fillIn(Wrapper()) + it('dispatches login with form data', async () => { + await fillIn(Wrapper()) expect(storeMocks.actions['auth/login']).toHaveBeenCalledWith(expect.any(Object), { email: 'email@example.org', password: '1234', }) }) + + describe('setting saved categories', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + + describe('no categories saved', () => { + it('resets the categories', async () => { + await fillIn(Wrapper()) + expect(storeMocks.actions['auth/login']).toBeCalled() + expect(storeMocks.mutations['posts/RESET_CATEGORIES']).toBeCalled() + }) + }) + + describe('categories saved', () => { + it('sets the categories', async () => { + authUserMock.mockReturnValue({ activeCategories: ['cat1', 'cat9', 'cat12'] }) + await fillIn(Wrapper()) + expect(storeMocks.actions['auth/login']).toBeCalled() + expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toBeCalledTimes(3) + expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toBeCalledWith({}, 'cat1') + expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toBeCalledWith({}, 'cat9') + expect(storeMocks.mutations['posts/TOGGLE_CATEGORY']).toBeCalledWith({}, 'cat12') + }) + }) + }) }) describe('Visibility of password', () => {