From eed9c7a26f91c06d1bb99fa225c33eb8f0fc866a Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 27 Sep 2021 20:55:40 +0200 Subject: [PATCH 1/5] feat: Unit Tests for Vue Router --- frontend/src/routes/router.test.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 frontend/src/routes/router.test.js diff --git a/frontend/src/routes/router.test.js b/frontend/src/routes/router.test.js new file mode 100644 index 000000000..6c182425e --- /dev/null +++ b/frontend/src/routes/router.test.js @@ -0,0 +1,28 @@ +import router from './router' + +describe('router', () => { + describe('options', () => { + const options = router.options + + it('has "/vue" as base', () => { + console.log(router) + expect(options).toEqual(expect.objectContaining({ + base: '/vue', + })) + }) + + it('has "active" as linkActiveClass', () => { + expect(options).toEqual(expect.objectContaining({ + linkActiveClass: 'active', + })) + }) + + it('has "history" as mode', () => { + expect(options).toEqual(expect.objectContaining({ + mode: 'history', + })) + }) + + + }) +}) From b5bc04e98cbaf9277cee64e4e10bf01d8b7e9df6 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 4 Oct 2021 17:24:33 +0200 Subject: [PATCH 2/5] test all the routes functions --- frontend/src/routes/router.test.js | 147 ++++++++++++++++-- .../src/views/Pages/UserProfileOverview.vue | 1 + 2 files changed, 136 insertions(+), 12 deletions(-) diff --git a/frontend/src/routes/router.test.js b/frontend/src/routes/router.test.js index 6c182425e..5a6367676 100644 --- a/frontend/src/routes/router.test.js +++ b/frontend/src/routes/router.test.js @@ -1,28 +1,151 @@ import router from './router' +import NotFound from '@/views/NotFoundPage.vue' describe('router', () => { describe('options', () => { - const options = router.options + const { options } = router + const { scrollBehavior, routes } = options it('has "/vue" as base', () => { - console.log(router) - expect(options).toEqual(expect.objectContaining({ - base: '/vue', - })) + expect(options).toEqual( + expect.objectContaining({ + base: '/vue', + }), + ) }) it('has "active" as linkActiveClass', () => { - expect(options).toEqual(expect.objectContaining({ - linkActiveClass: 'active', - })) + expect(options).toEqual( + expect.objectContaining({ + linkActiveClass: 'active', + }), + ) }) it('has "history" as mode', () => { - expect(options).toEqual(expect.objectContaining({ - mode: 'history', - })) + expect(options).toEqual( + expect.objectContaining({ + mode: 'history', + }), + ) }) - + describe('scroll behavior', () => { + it('returns save position when given', () => { + expect(scrollBehavior({}, {}, 'given')).toBe('given') + }) + + it('returns selector when hash is given', () => { + expect(scrollBehavior({ hash: '#to' }, {})).toEqual({ selector: '#to' }) + }) + + it('returns top left coordinates as default', () => { + expect(scrollBehavior({}, {})).toEqual({ x: 0, y: 0 }) + }) + }) + + describe('register page', () => { + it('is not present', () => { + expect(routes.find((r) => r.path === '/register')).toBe(undefined) + }) + }) + + describe('routes', () => { + it('has "/login" as default', () => { + expect(routes.find((r) => r.path === '/').redirect()).toEqual({ path: '/login' }) + }) + + describe('overview', () => { + it('requires authorization', () => { + expect(routes.find((r) => r.path === '/overview').meta.requiresAuth).toBeTruthy() + }) + + it('loads the "Overview" component', async () => { + const component = await routes.find((r) => r.path === '/overview').component() + expect(component.default.name).toBe('Overview') + }) + }) + + describe('profile', () => { + it('requires authorization', () => { + expect(routes.find((r) => r.path === '/profile').meta.requiresAuth).toBeTruthy() + }) + + it('loads the "UserProfile" component', async () => { + const component = await routes.find((r) => r.path === '/profile').component() + expect(component.default.name).toBe('UserProfile') + }) + }) + + describe('transactions', () => { + it('requires authorization', () => { + expect(routes.find((r) => r.path === '/transactions').meta.requiresAuth).toBeTruthy() + }) + + it('loads the "UserProfileTransactionList" component', async () => { + const component = await routes.find((r) => r.path === '/transactions').component() + expect(component.default.name).toBe('UserProfileTransactionList') + }) + }) + + describe('login', () => { + it('loads the "Login" component', async () => { + const component = await routes.find((r) => r.path === '/login').component() + expect(component.default.name).toBe('login') + }) + }) + + describe('thx', () => { + const thx = routes.find((r) => r.path === '/thx/:comingFrom') + + it('loads the "Thx" component', async () => { + const component = await thx.component() + expect(component.default.name).toBe('Thx') + }) + + describe('beforeEnter', () => { + const beforeEnter = thx.beforeEnter + const next = jest.fn() + + it('redirects to login when not coming from a valid page', () => { + beforeEnter({}, { path: '' }, next) + expect(next).toBeCalledWith({ path: '/login' }) + }) + + it('enters the page when coming from a valid page', () => { + jest.resetAllMocks() + beforeEnter({}, { path: '/password' }, next) + expect(next).toBeCalledWith() + }) + }) + }) + + describe('password', () => { + it('loads the "Password" component', async () => { + const component = await routes.find((r) => r.path === '/password').component() + expect(component.default.name).toBe('password') + }) + }) + + describe('reset', () => { + it('loads the "ResetPassword" component', async () => { + const component = await routes.find((r) => r.path === '/reset/:optin').component() + expect(component.default.name).toBe('ResetPassword') + }) + }) + + describe('checkEmail', () => { + it('loads the "CheckEmail" component', async () => { + const component = await routes.find((r) => r.path === '/checkEmail/:optin').component() + expect(component.default.name).toBe('CheckEmail') + }) + }) + + describe('not found page', () => { + it('renders the "NotFound" component', async () => { + expect(routes.find((r) => r.path === '*').component).toEqual(NotFound) + }) + }) + }) }) }) diff --git a/frontend/src/views/Pages/UserProfileOverview.vue b/frontend/src/views/Pages/UserProfileOverview.vue index c5be76145..83e2dbf7e 100644 --- a/frontend/src/views/Pages/UserProfileOverview.vue +++ b/frontend/src/views/Pages/UserProfileOverview.vue @@ -18,6 +18,7 @@ import FormUserLanguage from './UserProfile/UserCard_Language.vue' import FormUserNewsletter from './UserProfile/UserCard_Newsletter.vue' export default { + name: 'UserProfile', components: { UserCard, FormUserData, From 6b879b26afff61767dc7948634a768a317bd810a Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 4 Oct 2021 17:32:35 +0200 Subject: [PATCH 3/5] frontend unit test coverage to 71% --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dbf7e807e..e59fb5100 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -344,7 +344,7 @@ jobs: report_name: Coverage Frontend type: lcov result_path: ./coverage/lcov.info - min_coverage: 67 + min_coverage: 71 token: ${{ github.token }} ############################################################################## From e41d3e5db206e1cc81de9960352ae7d2bca81373 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 4 Oct 2021 17:42:52 +0200 Subject: [PATCH 4/5] frontend unit test coverage to 73% --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 77243c913..c3687cc5a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -344,7 +344,7 @@ jobs: report_name: Coverage Frontend type: lcov result_path: ./coverage/lcov.info - min_coverage: 71 + min_coverage: 73 token: ${{ github.token }} ############################################################################## From a84bf7b7122494a28007ed103ca1ab093d8213ef Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 4 Oct 2021 18:15:41 +0200 Subject: [PATCH 5/5] count the routes --- frontend/src/routes/router.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/frontend/src/routes/router.test.js b/frontend/src/routes/router.test.js index 5a6367676..57fca142b 100644 --- a/frontend/src/routes/router.test.js +++ b/frontend/src/routes/router.test.js @@ -55,6 +55,10 @@ describe('router', () => { expect(routes.find((r) => r.path === '/').redirect()).toEqual({ path: '/login' }) }) + it('has ten routes defined', () => { + expect(routes).toHaveLength(10) + }) + describe('overview', () => { it('requires authorization', () => { expect(routes.find((r) => r.path === '/overview').meta.requiresAuth).toBeTruthy()