test setting of saved categories

This commit is contained in:
Moriz Wahl 2022-09-07 21:50:37 +02:00
parent b323d0f580
commit 754251258d

View File

@ -12,6 +12,8 @@ config.stubs['nuxt-link'] = '<span><slot /></span>'
config.stubs['locale-switch'] = '<span><slot /></span>'
config.stubs['client-only'] = '<span><slot /></span>'
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', () => {