From 5d42d64fec67871cb722bfcd5354529863d5df2a Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 15 Jun 2021 01:25:01 +0200 Subject: [PATCH 1/3] test forget password, remove router from test setup --- .../views/Layout/DashboardLayout_gdd.spec.js | 15 ++- .../src/views/Pages/ForgotPassword.spec.js | 114 ++++++++++++++++++ frontend/src/views/Pages/ForgotPassword.vue | 3 +- .../src/views/Pages/ResetPassword.spec.js | 13 +- frontend/test/testSetup.js | 2 - 5 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 frontend/src/views/Pages/ForgotPassword.spec.js diff --git a/frontend/src/views/Layout/DashboardLayout_gdd.spec.js b/frontend/src/views/Layout/DashboardLayout_gdd.spec.js index 62ce94322..9221ff59a 100644 --- a/frontend/src/views/Layout/DashboardLayout_gdd.spec.js +++ b/frontend/src/views/Layout/DashboardLayout_gdd.spec.js @@ -1,16 +1,12 @@ import { mount, RouterLinkStub } from '@vue/test-utils' -import VueRouter from 'vue-router' import Vuex from 'vuex' import flushPromises from 'flush-promises' -import routes from '../../routes/routes' import DashboardLayoutGdd from './DashboardLayout_gdd' jest.useFakeTimers() const localVue = global.localVue -const router = new VueRouter({ routes }) - const transitionStub = () => ({ render(h) { return this.$options._renderChildren @@ -26,6 +22,14 @@ describe('DashboardLayoutGdd', () => { }, $t: jest.fn((t) => t), $n: jest.fn(), + $route: { + meta: { + hideFooter: false, + }, + }, + $router: { + push: jest.fn(), + }, } const state = { @@ -40,6 +44,7 @@ describe('DashboardLayoutGdd', () => { const stubs = { RouterLink: RouterLinkStub, FadeTransition: transitionStub(), + RouterView: transitionStub(), } const store = new Vuex.Store({ @@ -50,7 +55,7 @@ describe('DashboardLayoutGdd', () => { }) const Wrapper = () => { - return mount(DashboardLayoutGdd, { localVue, mocks, router, store, stubs }) + return mount(DashboardLayoutGdd, { localVue, mocks, store, stubs }) } describe('mount', () => { diff --git a/frontend/src/views/Pages/ForgotPassword.spec.js b/frontend/src/views/Pages/ForgotPassword.spec.js new file mode 100644 index 000000000..9e33c785e --- /dev/null +++ b/frontend/src/views/Pages/ForgotPassword.spec.js @@ -0,0 +1,114 @@ +import { mount, RouterLinkStub } from '@vue/test-utils' +import flushPromises from 'flush-promises' +import loginAPI from '../../apis/loginAPI.js' +import ForgotPassword from './ForgotPassword' + +jest.mock('../../apis/loginAPI.js') + +const mockAPIcall = jest.fn() +loginAPI.sendEmail = mockAPIcall + +const localVue = global.localVue + +const mockRouterPush = jest.fn() + +describe('ForgotPassword', () => { + let wrapper + + const mocks = { + $t: jest.fn((t) => t), + $router: { + push: mockRouterPush, + }, + } + + const stubs = { + RouterLink: RouterLinkStub, + } + + const Wrapper = () => { + return mount(ForgotPassword, { localVue, mocks, stubs }) + } + + describe('mount', () => { + beforeEach(() => { + wrapper = Wrapper() + }) + + it('renders the component', () => { + expect(wrapper.find('div.forgot-password').exists()).toBeTruthy() + }) + + it('has a title', () => { + expect(wrapper.find('h1').text()).toEqual('site.password.title') + }) + + it('has a subtitle', () => { + expect(wrapper.find('p.text-lead').text()).toEqual('site.password.subtitle') + }) + + describe('back button', () => { + it('has a "back" button', () => { + expect(wrapper.findComponent(RouterLinkStub).text()).toEqual('back') + }) + + it('links to login', () => { + expect(wrapper.findComponent(RouterLinkStub).props().to).toEqual('/Login') + }) + }) + + describe('input form', () => { + let form + + beforeEach(() => { + form = wrapper.find('form') + }) + + it('has the label "Email"', () => { + expect(form.find('label').text()).toEqual('Email') + }) + + it('has the placeholder "Email"', () => { + expect(form.find('input').attributes('placeholder')).toEqual('Email') + }) + + it('has a submit button', () => { + expect(form.find('button[type="submit"]').exists()).toBeTruthy() + }) + + describe('invalid Email', () => { + beforeEach(async () => { + await form.find('input').setValue('no-email') + await flushPromises() + }) + + it('displays an error', () => { + expect(form.find('#reset-pwd--live-feedback').text()).toEqual( + 'The Email field must be a valid email', + ) + }) + + it('does not call the API', () => { + expect(mockAPIcall).not.toHaveBeenCalled() + }) + }) + + describe('valid Email', () => { + beforeEach(async () => { + await form.find('input').setValue('user@example.org') + form.trigger('submit') + await wrapper.vm.$nextTick() + await flushPromises() + }) + + it('calls the API', () => { + expect(mockAPIcall).toHaveBeenCalledWith('user@example.org') + }) + + it('pushes "/thx/password" to the route', () => { + expect(mockRouterPush).toHaveBeenCalledWith('/thx/password') + }) + }) + }) + }) +}) diff --git a/frontend/src/views/Pages/ForgotPassword.vue b/frontend/src/views/Pages/ForgotPassword.vue index eebba0ced..e407f3590 100644 --- a/frontend/src/views/Pages/ForgotPassword.vue +++ b/frontend/src/views/Pages/ForgotPassword.vue @@ -1,5 +1,5 @@