From bf41f338b70158f40045a0caf976589d8e5424e8 Mon Sep 17 00:00:00 2001 From: provira Date: Mon, 23 Dec 2024 12:13:58 +0100 Subject: [PATCH 1/2] feat: refs #7079 created VnLocation front test --- src/components/common/VnLocation.vue | 2 +- .../common/__tests__/VnLocation.spec.js | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/components/common/__tests__/VnLocation.spec.js diff --git a/src/components/common/VnLocation.vue b/src/components/common/VnLocation.vue index a8840f243..f58222187 100644 --- a/src/components/common/VnLocation.vue +++ b/src/components/common/VnLocation.vue @@ -26,7 +26,7 @@ const locationProperties = [ (obj) => obj.country?.name, ]; -const formatLocation = (obj, properties) => { +const formatLocation = (obj, properties = locationProperties) => { const parts = properties.map((prop) => { if (typeof prop === 'string') { return obj[prop]; diff --git a/src/components/common/__tests__/VnLocation.spec.js b/src/components/common/__tests__/VnLocation.spec.js new file mode 100644 index 000000000..920afced8 --- /dev/null +++ b/src/components/common/__tests__/VnLocation.spec.js @@ -0,0 +1,100 @@ +import { createWrapper } from 'app/test/vitest/helper'; +import VnLocation from 'components/common/VnLocation.vue'; +import { vi, afterEach, expect, it, beforeEach, describe } from 'vitest'; + +function buildComponent(data) { + return createWrapper(VnLocation, { + global: { + stubs: [''], + props: { + location: data + } + }, + }).vm; +} + +afterEach(() => { + vi.clearAllMocks(); +}); + +describe('formatLocation', () => { + let locationBase; + + beforeEach(() => { + locationBase = { + postcode: '46680', + city: 'Algemesi', + province: { name: 'Valencia' }, + country: { name: 'Spain' } + }; + }); + + it('should return the postcode, city, province and country', () => { + const location = { ...locationBase }; + const vm = buildComponent(location); + const parts = vm.formatLocation(location); + expect(parts).toEqual('46680, Algemesi(Valencia), Spain'); + }); + + it('should return the postcode and country', () => { + const location = { ...locationBase, city: undefined }; + const vm = buildComponent(location); + const parts = vm.formatLocation(location); + expect(parts).toEqual('46680, Spain'); + }); + + it('should return the city, province and country', () => { + const location = { ...locationBase, postcode: undefined }; + const vm = buildComponent(location); + const parts = vm.formatLocation(location); + expect(parts).toEqual('Algemesi(Valencia), Spain'); + }); + + it('should return the country', () => { + const location = { ...locationBase, postcode: undefined, city: undefined, province: undefined }; + const vm = buildComponent(location); + const parts = vm.formatLocation(location); + expect(parts).toEqual('Spain'); + }); +}); + +describe('showLabel', () => { + let locationBase; + + beforeEach(() => { + locationBase = { + code: '46680', + town: 'Algemesi', + province: 'Valencia', + country: 'Spain' + }; + }); + + it('should show the label with postcode, city, province and country', () => { + const location = { ...locationBase }; + const vm = buildComponent(location); + const label = vm.showLabel(location); + expect(label).toEqual('46680, Algemesi(Valencia), Spain'); + }); + + it('should show the label with postcode and country', () => { + const location = { ...locationBase, town: undefined }; + const vm = buildComponent(location); + const label = vm.showLabel(location); + expect(label).toEqual('46680, Spain'); + }); + + it('should show the label with city, province and country', () => { + const location = { ...locationBase, code: undefined }; + const vm = buildComponent(location); + const label = vm.showLabel(location); + expect(label).toEqual('Algemesi(Valencia), Spain'); + }); + + it('should show the label with country', () => { + const location = { ...locationBase, code: undefined, town: undefined, province: undefined }; + const vm = buildComponent(location); + const label = vm.showLabel(location); + expect(label).toEqual('Spain'); + }); +}); \ No newline at end of file -- 2.40.1 From 89acb338a9ebbaf521e93ca39828c65af53cba1a Mon Sep 17 00:00:00 2001 From: provira Date: Tue, 24 Dec 2024 12:18:36 +0100 Subject: [PATCH 2/2] refactor: refs #7079 removed useless code --- .../common/__tests__/VnLocation.spec.js | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/components/common/__tests__/VnLocation.spec.js b/src/components/common/__tests__/VnLocation.spec.js index 920afced8..65fdae960 100644 --- a/src/components/common/__tests__/VnLocation.spec.js +++ b/src/components/common/__tests__/VnLocation.spec.js @@ -5,7 +5,6 @@ import { vi, afterEach, expect, it, beforeEach, describe } from 'vitest'; function buildComponent(data) { return createWrapper(VnLocation, { global: { - stubs: [''], props: { location: data } @@ -32,29 +31,25 @@ describe('formatLocation', () => { it('should return the postcode, city, province and country', () => { const location = { ...locationBase }; const vm = buildComponent(location); - const parts = vm.formatLocation(location); - expect(parts).toEqual('46680, Algemesi(Valencia), Spain'); + expect(vm.formatLocation(location)).toEqual('46680, Algemesi(Valencia), Spain'); }); it('should return the postcode and country', () => { const location = { ...locationBase, city: undefined }; const vm = buildComponent(location); - const parts = vm.formatLocation(location); - expect(parts).toEqual('46680, Spain'); + expect(vm.formatLocation(location)).toEqual('46680, Spain'); }); it('should return the city, province and country', () => { const location = { ...locationBase, postcode: undefined }; const vm = buildComponent(location); - const parts = vm.formatLocation(location); - expect(parts).toEqual('Algemesi(Valencia), Spain'); + expect(vm.formatLocation(location)).toEqual('Algemesi(Valencia), Spain'); }); it('should return the country', () => { const location = { ...locationBase, postcode: undefined, city: undefined, province: undefined }; const vm = buildComponent(location); - const parts = vm.formatLocation(location); - expect(parts).toEqual('Spain'); + expect(vm.formatLocation(location)).toEqual('Spain'); }); }); @@ -73,28 +68,24 @@ describe('showLabel', () => { it('should show the label with postcode, city, province and country', () => { const location = { ...locationBase }; const vm = buildComponent(location); - const label = vm.showLabel(location); - expect(label).toEqual('46680, Algemesi(Valencia), Spain'); + expect(vm.showLabel(location)).toEqual('46680, Algemesi(Valencia), Spain'); }); it('should show the label with postcode and country', () => { const location = { ...locationBase, town: undefined }; const vm = buildComponent(location); - const label = vm.showLabel(location); - expect(label).toEqual('46680, Spain'); + expect(vm.showLabel(location)).toEqual('46680, Spain'); }); it('should show the label with city, province and country', () => { const location = { ...locationBase, code: undefined }; const vm = buildComponent(location); - const label = vm.showLabel(location); - expect(label).toEqual('Algemesi(Valencia), Spain'); + expect(vm.showLabel(location)).toEqual('Algemesi(Valencia), Spain'); }); it('should show the label with country', () => { const location = { ...locationBase, code: undefined, town: undefined, province: undefined }; const vm = buildComponent(location); - const label = vm.showLabel(location); - expect(label).toEqual('Spain'); + expect(vm.showLabel(location)).toEqual('Spain'); }); }); \ No newline at end of file -- 2.40.1