From 70f8b6d18b7cc0db185e1dd03125b9b4a045f287 Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Mon, 24 Jun 2024 15:18:57 +0200 Subject: [PATCH] update tests --- .../Federation/CommunityVisualizeItem.spec.js | 127 +++++++++++++++++- .../Federation/CommunityVisualizeItem.vue | 10 +- .../src/components/input/Coordinates.spec.js | 119 ++++++++++++++++ admin/src/components/input/Coordinates.vue | 1 + .../components/input/EditableGroup.spec.js | 92 +++++++++++++ admin/src/components/input/EditableGroup.vue | 4 +- .../input/EditableGroupableLabel.spec.js | 79 +++++++++++ .../EditableGroupableLabel.vue} | 14 +- .../components/input/EditableLabel.spec.js | 83 ------------ admin/src/components/input/EditableLabel.vue | 64 --------- 10 files changed, 433 insertions(+), 160 deletions(-) create mode 100644 admin/src/components/input/Coordinates.spec.js create mode 100644 admin/src/components/input/EditableGroup.spec.js create mode 100644 admin/src/components/input/EditableGroupableLabel.spec.js rename admin/src/components/{Federation/GMSApiKey.vue => input/EditableGroupableLabel.vue} (69%) delete mode 100644 admin/src/components/input/EditableLabel.spec.js delete mode 100644 admin/src/components/input/EditableLabel.vue diff --git a/admin/src/components/Federation/CommunityVisualizeItem.spec.js b/admin/src/components/Federation/CommunityVisualizeItem.spec.js index 2c5065ba0..6ee1ba4f7 100644 --- a/admin/src/components/Federation/CommunityVisualizeItem.spec.js +++ b/admin/src/components/Federation/CommunityVisualizeItem.spec.js @@ -1,9 +1,19 @@ +import { createMockClient } from 'mock-apollo-client' import { mount } from '@vue/test-utils' +import VueApollo from 'vue-apollo' import Vuex from 'vuex' import CommunityVisualizeItem from './CommunityVisualizeItem.vue' +import { updateHomeCommunity } from '../../graphql/updateHomeCommunity' +import { toastSuccessSpy } from '../../../test/testSetup' + +const mockClient = createMockClient() +const apolloProvider = new VueApollo({ + defaultClient: mockClient, +}) const localVue = global.localVue localVue.use(Vuex) +localVue.use(VueApollo) const today = new Date() const createdDate = new Date() createdDate.setDate(createdDate.getDate() - 3) @@ -19,7 +29,7 @@ const store = new Vuex.Store({ let propsData = { item: { - id: 1, + uuid: 1, foreign: false, url: 'http://localhost/api/', publicKey: '4007170edd8d33fb009cd99ee4e87f214e7cd21b668d45540a064deb42e243c2', @@ -76,8 +86,18 @@ const mocks = { describe('CommunityVisualizeItem', () => { let wrapper + const updateHomeCommunityMock = jest.fn() + mockClient.setRequestHandler( + updateHomeCommunity, + updateHomeCommunityMock.mockResolvedValue({ + data: { + updateHomeCommunity: { id: 1 }, + }, + }), + ) + const Wrapper = () => { - return mount(CommunityVisualizeItem, { localVue, mocks, propsData, store }) + return mount(CommunityVisualizeItem, { localVue, mocks, propsData, store, apolloProvider }) } describe('mount', () => { @@ -152,7 +172,7 @@ describe('CommunityVisualizeItem', () => { beforeEach(() => { propsData = { item: { - id: 7590, + uuid: 7590, foreign: false, publicKey: 'eaf6a426b24fd54f8fbae11c17700fc595080ca25159579c63d38dbc64284ba7', url: 'http://localhost/api/2_0', @@ -195,7 +215,7 @@ describe('CommunityVisualizeItem', () => { beforeEach(() => { propsData = { item: { - id: 7590, + uuid: 7590, foreign: false, publicKey: 'eaf6a426b24fd54f8fbae11c17700fc595080ca25159579c63d38dbc64284ba7', url: 'http://localhost/api/', @@ -219,7 +239,7 @@ describe('CommunityVisualizeItem', () => { beforeEach(() => { propsData = { item: { - id: 7590, + uuid: 7590, foreign: false, publicKey: 'eaf6a426b24fd54f8fbae11c17700fc595080ca25159579c63d38dbc64284ba7', url: 'http://localhost/api/2_0', @@ -237,6 +257,103 @@ describe('CommunityVisualizeItem', () => { expect(wrapper.vm.createdAt).toBe('') }) }) + + describe('test handleUpdateHomeCommunity', () => { + describe('gms api key', () => { + beforeEach(async () => { + wrapper = Wrapper() + wrapper.vm.originalGmsApiKey = 'original' + wrapper.vm.gmsApiKey = 'changed key' + + await wrapper.vm.handleUpdateHomeCommunity() + // Wait for the next tick to allow async operations to complete + await wrapper.vm.$nextTick() + }) + + it('expect changed gms api key', () => { + expect(updateHomeCommunityMock).toBeCalledWith({ + uuid: propsData.item.uuid, + gmsApiKey: 'changed key', + location: undefined, + }) + expect(wrapper.vm.originalGmsApiKey).toBe('changed key') + expect(toastSuccessSpy).toBeCalledWith('federation.toast_gmsApiKeyUpdated') + }) + }) + + describe('location', () => { + beforeEach(async () => { + wrapper = Wrapper() + wrapper.vm.originalLocation = { coordinates: [1.212, 15.121], type: 'Point' } + wrapper.vm.location = { coordinates: [17.212, 1.121], type: 'Point' } + + await wrapper.vm.handleUpdateHomeCommunity() + // Wait for the next tick to allow async operations to complete + await wrapper.vm.$nextTick() + }) + + it('expect changed location', () => { + expect(updateHomeCommunityMock).toBeCalledWith({ + uuid: propsData.item.uuid, + location: { coordinates: [17.212, 1.121], type: 'Point' }, + gmsApiKey: undefined, + }) + expect(wrapper.vm.originalLocation).toStrictEqual({ + coordinates: [17.212, 1.121], + type: 'Point', + }) + expect(toastSuccessSpy).toBeCalledWith('federation.toast_gmsLocationUpdated') + }) + }) + + describe('gms api key and location', () => { + beforeEach(async () => { + wrapper = Wrapper() + wrapper.vm.originalGmsApiKey = 'original' + wrapper.vm.gmsApiKey = 'changed key' + wrapper.vm.originalLocation = { coordinates: [1.212, 15.121], type: 'Point' } + wrapper.vm.location = { coordinates: [17.212, 1.121], type: 'Point' } + + await wrapper.vm.handleUpdateHomeCommunity() + // Wait for the next tick to allow async operations to complete + await wrapper.vm.$nextTick() + }) + + it('expect changed gms api key and changed location', () => { + expect(updateHomeCommunityMock).toBeCalledWith({ + uuid: propsData.item.uuid, + gmsApiKey: 'changed key', + location: undefined, + }) + expect(wrapper.vm.originalGmsApiKey).toBe('changed key') + expect(wrapper.vm.originalLocation).toStrictEqual({ + coordinates: [17.212, 1.121], + type: 'Point', + }) + expect(toastSuccessSpy).toBeCalledWith('federation.toast_gmsApiKeyAndLocationUpdated') + }) + }) + }) + + describe('test resetHomeCommunityEditable', () => { + beforeEach(async () => { + wrapper = Wrapper() + }) + + it('test', () => { + wrapper.vm.originalGmsApiKey = 'original' + wrapper.vm.gmsApiKey = 'changed key' + wrapper.vm.originalLocation = { coordinates: [1.212, 15.121], type: 'Point' } + wrapper.vm.location = { coordinates: [17.212, 1.121], type: 'Point' } + wrapper.vm.resetHomeCommunityEditable() + + expect(wrapper.vm.location).toStrictEqual({ + coordinates: [1.212, 15.121], + type: 'Point', + }) + expect(wrapper.vm.gmsApiKey).toBe('original') + }) + }) }) }) }) diff --git a/admin/src/components/Federation/CommunityVisualizeItem.vue b/admin/src/components/Federation/CommunityVisualizeItem.vue index e72b2a155..c3c8e70cb 100644 --- a/admin/src/components/Federation/CommunityVisualizeItem.vue +++ b/admin/src/components/Federation/CommunityVisualizeItem.vue @@ -45,7 +45,11 @@ @@ -81,7 +85,7 @@ import EditableGroup from '@/components/input/EditableGroup' import FederationVisualizeItem from './FederationVisualizeItem.vue' import { updateHomeCommunity } from '../../graphql/updateHomeCommunity' import Coordinates from '../input/Coordinates.vue' -import GMSApiKey from './GMSApiKey.vue' +import EditableGroupableLabel from '../input/EditableGroupableLabel.vue' const locales = { en, de, es, fr, nl } @@ -91,7 +95,7 @@ export default { Coordinates, EditableGroup, FederationVisualizeItem, - GMSApiKey, + EditableGroupableLabel, }, props: { item: { type: Object }, diff --git a/admin/src/components/input/Coordinates.spec.js b/admin/src/components/input/Coordinates.spec.js new file mode 100644 index 000000000..25e73c554 --- /dev/null +++ b/admin/src/components/input/Coordinates.spec.js @@ -0,0 +1,119 @@ +import { mount } from '@vue/test-utils' +import Coordinates from './Coordinates.vue' +import Vue from 'vue' +import VueI18n from 'vue-i18n' + +Vue.use(VueI18n) + +const localVue = global.localVue +const i18n = new VueI18n({ + locale: 'en', + messages: { + en: { + 'geo-coordinates.format': '{latitude}, {longitude}', + }, + }, +}) + +describe('Coordinates', () => { + let wrapper + const value = { + type: 'Point', + coordinates: [12.34, 56.78], + } + + const createWrapper = (propsData) => { + return mount(Coordinates, { + localVue, + i18n, + propsData, + }) + } + + beforeEach(() => { + wrapper = createWrapper({ value }) + }) + + it('renders the component with initial values', () => { + expect(wrapper.find('#home-community-latitude').element.value).toBe('56.78') + expect(wrapper.find('#home-community-longitude').element.value).toBe('12.34') + expect(wrapper.find('#home-community-latitude-longitude-smart').element.value).toBe( + '56.78, 12.34', + ) + }) + + it('updates latitude and longitude when input changes', async () => { + const latitudeInput = wrapper.find('#home-community-latitude') + const longitudeInput = wrapper.find('#home-community-longitude') + + await latitudeInput.setValue('34.56') + await longitudeInput.setValue('78.90') + + expect(wrapper.vm.latitude).toBe('34.56') + expect(wrapper.vm.longitude).toBe('78.90') + }) + + it('emits input event with updated values', async () => { + const latitudeInput = wrapper.find('#home-community-latitude') + const longitudeInput = wrapper.find('#home-community-longitude') + + await latitudeInput.setValue('34.56') + expect(wrapper.emitted().input).toBeTruthy() + expect(wrapper.emitted().input[0][0]).toEqual({ + type: 'Point', + coordinates: [12.34, 34.56], + }) + + await longitudeInput.setValue('78.90') + expect(wrapper.emitted().input).toBeTruthy() + expect(wrapper.emitted().input[1][0]).toEqual({ + type: 'Point', + coordinates: [78.9, 34.56], + }) + }) + + it('updates latitudeLongitude when latitude or longitude changes', async () => { + const latitudeInput = wrapper.find('#home-community-latitude') + const longitudeInput = wrapper.find('#home-community-longitude') + + await latitudeInput.setValue('34.56') + await longitudeInput.setValue('78.90') + + expect(wrapper.vm.latitudeLongitude).toBe('34.56, 78.90') + }) + + it('splits coordinates correctly when entering in latitudeLongitude input', async () => { + const latitudeLongitudeInput = wrapper.find('#home-community-latitude-longitude-smart') + + await latitudeLongitudeInput.setValue('34.56, 78.90') + await latitudeLongitudeInput.trigger('input') + + expect(wrapper.vm.latitude).toBe(34.56) + expect(wrapper.vm.longitude).toBe(78.9) + }) + + it('sets inputValue to null if coordinates are invalid', async () => { + const latitudeInput = wrapper.find('#home-community-latitude') + const longitudeInput = wrapper.find('#home-community-longitude') + + await latitudeInput.setValue('invalid') + await longitudeInput.setValue('78.90') + + expect(wrapper.vm.inputValue).toBeNull() + }) + + it('validates coordinates correctly', async () => { + const latitudeInput = wrapper.find('#home-community-latitude') + const longitudeInput = wrapper.find('#home-community-longitude') + + await latitudeInput.setValue('invalid') + await longitudeInput.setValue('78.90') + + expect(wrapper.vm.isValid).toBe(false) + + await latitudeInput.setValue('34.56') + await longitudeInput.setValue('78.90') + + expect(wrapper.vm.isValid).toBe(true) + }) +}) diff --git a/admin/src/components/input/Coordinates.vue b/admin/src/components/input/Coordinates.vue index 72756bd5d..699ac29ea 100644 --- a/admin/src/components/input/Coordinates.vue +++ b/admin/src/components/input/Coordinates.vue @@ -39,6 +39,7 @@