import { mount } from '@vue/test-utils' import Security from './security.vue' const localVue = global.localVue describe('security.vue', () => { let mocks beforeEach(() => { mocks = { $toast: { error: jest.fn(), success: jest.fn(), }, $t: jest.fn((key) => key), $store: { commit: jest.fn(), }, $apollo: { mutate: jest .fn() .mockRejectedValue({ message: 'Ouch!' }) .mockResolvedValueOnce({ data: { changePassword: 'NEWTOKEN' } }), }, } }) describe('mount', () => { let wrapper const Wrapper = () => { return mount(Security, { mocks, localVue }) } beforeEach(() => { wrapper = Wrapper() }) it('renders', () => { expect(wrapper.classes('os-card')).toBe(true) }) it('renders three input fields (old, new, confirm)', () => { expect(wrapper.findAll('input')).toHaveLength(3) }) describe('validations', () => { describe('new password and confirmation mismatch', () => { beforeEach(async () => { await wrapper.find('input#oldPassword').setValue('oldsecret') await wrapper.find('input#password').setValue('superdupersecret') await wrapper.find('input#passwordConfirmation').setValue('different') }) it('does not submit the form', async () => { mocks.$apollo.mutate.mockReset() await wrapper.find('form').trigger('submit') await wrapper.vm.$nextTick() expect(mocks.$apollo.mutate).not.toHaveBeenCalled() }) }) }) describe('given valid input', () => { beforeEach(() => { wrapper.find('input#oldPassword').setValue('supersecret') wrapper.find('input#password').setValue('superdupersecret') wrapper.find('input#passwordConfirmation').setValue('superdupersecret') }) describe('submit form', () => { beforeEach(async () => { await wrapper.find('form').trigger('submit') }) it('calls changePassword mutation', () => { expect(mocks.$apollo.mutate).toHaveBeenCalled() }) it('passes form data as variables', () => { expect(mocks.$apollo.mutate.mock.calls[0][0]).toEqual( expect.objectContaining({ variables: { oldPassword: 'supersecret', password: 'superdupersecret', }, }), ) }) describe('mutation resolves', () => { beforeEach(() => { wrapper.find('form').trigger('submit') }) it('calls auth/SET_TOKEN with response', () => { expect(mocks.$store.commit).toHaveBeenCalledWith('auth/SET_TOKEN', 'NEWTOKEN') }) it('displays success message', () => { expect(mocks.$t).toHaveBeenCalledWith('settings.security.change-password.success') expect(mocks.$toast.success).toHaveBeenCalled() }) }) describe('mutation rejects', () => { beforeEach(async () => { await wrapper.find('input#oldPassword').setValue('supersecret') await wrapper.find('input#password').setValue('supersecret') await wrapper.find('input#passwordConfirmation').setValue('supersecret') await wrapper.find('form').trigger('submit') }) it('displays error message', async () => { expect(mocks.$toast.error).toHaveBeenCalledWith('Ouch!') }) }) }) }) }) })