mock path to have differnet include results

This commit is contained in:
Moriz Wahl 2022-01-24 16:42:09 +01:00
parent 65d2354817
commit 9df3ca7c01
2 changed files with 81 additions and 47 deletions

View File

@ -15,10 +15,7 @@ const stubs = {
RouterLink: RouterLinkStub, RouterLink: RouterLinkStub,
} }
const createMockObject = (comingFrom) => { const mocks = {
return {
localVue,
mocks: {
$i18n: { $i18n: {
locale: 'en', locale: 'en',
}, },
@ -26,10 +23,10 @@ const createMockObject = (comingFrom) => {
$route: { $route: {
params: { params: {
optin: '123', optin: '123',
comingFrom,
}, },
path: { path: {
includes: (t) => t, mock: 'checkEmail',
includes: jest.fn((t) => t === mocks.$route.path.mock),
}, },
}, },
$toasted: { $toasted: {
@ -48,21 +45,18 @@ const createMockObject = (comingFrom) => {
$apollo: { $apollo: {
mutate: apolloMutationMock, mutate: apolloMutationMock,
}, },
},
stubs,
}
} }
describe('ResetPassword', () => { describe('ResetPassword', () => {
let wrapper let wrapper
const Wrapper = (functionName) => { const Wrapper = () => {
return mount(ResetPassword, functionName) return mount(ResetPassword, { localVue, mocks, stubs })
} }
describe('mount', () => { describe('mount', () => {
beforeEach(() => { beforeEach(() => {
wrapper = Wrapper(createMockObject()) wrapper = Wrapper()
}) })
describe('No valid optin', () => { describe('No valid optin', () => {
@ -86,6 +80,12 @@ describe('ResetPassword', () => {
}) })
describe('Register header', () => { describe('Register header', () => {
describe('from reset', () => {
beforeEach(() => {
mocks.$route.path.mock = 'reset'
wrapper = Wrapper()
})
it('has a welcome message', async () => { it('has a welcome message', async () => {
expect(wrapper.find('div.header').text()).toContain('settings.password.reset') expect(wrapper.find('div.header').text()).toContain('settings.password.reset')
expect(wrapper.find('div.header').text()).toContain( expect(wrapper.find('div.header').text()).toContain(
@ -94,6 +94,21 @@ describe('ResetPassword', () => {
}) })
}) })
describe('from checkEmail', () => {
beforeEach(() => {
mocks.$route.path.mock = 'checkEmail'
wrapper = Wrapper()
})
it('has a welcome message', async () => {
expect(wrapper.find('div.header').text()).toContain('settings.password.set')
expect(wrapper.find('div.header').text()).toContain(
'settings.password.set-password.text',
)
})
})
})
describe('links', () => { describe('links', () => {
it('has a link "Back"', async () => { it('has a link "Back"', async () => {
expect(wrapper.findAllComponents(RouterLinkStub).at(0).text()).toEqual('back') expect(wrapper.findAllComponents(RouterLinkStub).at(0).text()).toEqual('back')
@ -128,7 +143,6 @@ describe('ResetPassword', () => {
describe('submit form', () => { describe('submit form', () => {
beforeEach(async () => { beforeEach(async () => {
// wrapper = Wrapper(createMockObject())
await wrapper.findAll('input').at(0).setValue('Aa123456_') await wrapper.findAll('input').at(0).setValue('Aa123456_')
await wrapper.findAll('input').at(1).setValue('Aa123456_') await wrapper.findAll('input').at(1).setValue('Aa123456_')
await flushPromises() await flushPromises()
@ -164,14 +178,14 @@ describe('ResetPassword', () => {
}) })
}) })
describe('server response with success', () => { describe('server response with success on /checkEmail', () => {
beforeEach(async () => { beforeEach(async () => {
mocks.$route.path.mock = 'checkEmail'
apolloMutationMock.mockResolvedValue({ apolloMutationMock.mockResolvedValue({
data: { data: {
resetPassword: 'success', resetPassword: 'success',
}, },
}) })
wrapper = Wrapper(createMockObject('checkEmail'))
await wrapper.findAll('input').at(0).setValue('Aa123456_') await wrapper.findAll('input').at(0).setValue('Aa123456_')
await wrapper.findAll('input').at(1).setValue('Aa123456_') await wrapper.findAll('input').at(1).setValue('Aa123456_')
await wrapper.find('form').trigger('submit') await wrapper.find('form').trigger('submit')
@ -193,6 +207,26 @@ describe('ResetPassword', () => {
expect(routerPushMock).toHaveBeenCalledWith('/thx/checkEmail') expect(routerPushMock).toHaveBeenCalledWith('/thx/checkEmail')
}) })
}) })
describe('server response with success on /reset', () => {
beforeEach(async () => {
mocks.$route.path.mock = 'reset'
wrapper = Wrapper()
apolloMutationMock.mockResolvedValue({
data: {
resetPassword: 'success',
},
})
await wrapper.findAll('input').at(0).setValue('Aa123456_')
await wrapper.findAll('input').at(1).setValue('Aa123456_')
await wrapper.find('form').trigger('submit')
await flushPromises()
})
it('redirects to "/thx/reset"', () => {
expect(routerPushMock).toHaveBeenCalledWith('/thx/reset')
})
})
}) })
}) })
}) })

View File

@ -94,7 +94,7 @@ export default {
this.form.password = '' this.form.password = ''
if (this.$route.path.includes('checkEmail')) { if (this.$route.path.includes('checkEmail')) {
this.$router.push('/thx/checkEmail') this.$router.push('/thx/checkEmail')
} else if (this.$route.path.includes('reset')) { } else {
this.$router.push('/thx/reset') this.$router.push('/thx/reset')
} }
}) })