From f36eb85411dfdbad86693085dbc9324fc17abc16 Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Fri, 31 May 2024 16:50:14 +0200 Subject: [PATCH] add test for circles --- frontend/src/pages/Circles.spec.js | 84 ++++++++++++++++++++++++++++++ frontend/src/pages/Circles.vue | 3 +- 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 frontend/src/pages/Circles.spec.js diff --git a/frontend/src/pages/Circles.spec.js b/frontend/src/pages/Circles.spec.js new file mode 100644 index 000000000..39a4e0f02 --- /dev/null +++ b/frontend/src/pages/Circles.spec.js @@ -0,0 +1,84 @@ +import { mount } from '@vue/test-utils' +import Circles from './Circles' +import { authenticateHumhubAutoLogin } from '@/graphql/queries' + +const localVue = global.localVue + +const TEST_URL_WITH_JWT_TOKEN = + 'https://community.gradido.net/user/auth/external?authclient=jwt&jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTY4NTI0NjQxMn0.V2h4Rf3LfdOYDsx2clVCx-jbhKoY5F4Ks5-YJGVtHRk' + +const apolloQueryMock = jest.fn().mockResolvedValue({ + data: { + authenticateHumhubAutoLogin: TEST_URL_WITH_JWT_TOKEN, + }, +}) + +describe('Circles', () => { + let wrapper + + const mocks = { + $t: jest.fn((t) => t), + $n: jest.fn(), + $i18n: { + locale: 'en', + }, + $apollo: { + query: apolloQueryMock, + }, + $store: { + state: { + humhubAllowed: true, + }, + commit: jest.fn(), + }, + } + + const Wrapper = () => { + return mount(Circles, { + localVue, + mocks, + }) + } + + describe('mount', () => { + beforeEach(() => { + wrapper = Wrapper() + }) + + it('renders the circles page', () => { + expect(wrapper.find('div.circles').exists()).toBeTruthy() + }) + + it('calls authenticateHumhubAutoLogin', () => { + expect(apolloQueryMock).toBeCalledWith( + expect.objectContaining({ + query: authenticateHumhubAutoLogin, + fetchPolicy: 'network-only', + }), + ) + }) + + it('sets humhubUri and enables button on success', async () => { + await wrapper.vm.$nextTick() + expect(wrapper.vm.humhubUri).toBe(TEST_URL_WITH_JWT_TOKEN) + expect(wrapper.vm.enableButton).toBe(true) + }) + + describe('error apolloQueryMock', () => { + beforeEach(async () => { + jest.clearAllMocks() + apolloQueryMock.mockRejectedValue({ + message: 'uups', + }) + wrapper = Wrapper() + await wrapper.vm.$nextTick() + }) + + it('toasts an error message and disables humhub', () => { + expect(wrapper.vm.enableButton).toBe(true) + expect(wrapper.vm.humhubUri).toBe('') + expect(mocks.$store.commit).toBeCalledWith('humhubAllowed', false) + }) + }) + }) +}) diff --git a/frontend/src/pages/Circles.vue b/frontend/src/pages/Circles.vue index e0502ff9a..77e556bd7 100644 --- a/frontend/src/pages/Circles.vue +++ b/frontend/src/pages/Circles.vue @@ -60,11 +60,10 @@ export default { this.enableButton = true }) .catch(() => { - // this.toastError('authenticateHumhubAutoLogin failed!') this.enableButton = true + this.humhubUri = '' // something went wrong with login link so we disable humhub this.$store.commit('humhubAllowed', false) - this.$router.push('/settings/extern') }) }, },