From 1b3500bbce1cdfcf84bb91e11b648beb892a80e1 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 26 Nov 2024 22:58:36 +0100 Subject: [PATCH] test: add vitest VnLinkPhone filter --- src/filters/parsePhone.js | 3 -- .../components/common/VnLinkPhone.spec.js | 29 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/vitest/__tests__/components/common/VnLinkPhone.spec.js diff --git a/src/filters/parsePhone.js b/src/filters/parsePhone.js index b2d1ed7b8..696f55007 100644 --- a/src/filters/parsePhone.js +++ b/src/filters/parsePhone.js @@ -1,14 +1,11 @@ export default function (phone, prefix = 34) { if (phone.startsWith('+')) { - console.log('a'); return `${phone.slice(1)}`; } if (phone.startsWith('00')) { - console.log('b'); return `${phone.slice(2)}`; } if (phone.startsWith(prefix) && phone.length === prefix.length + 9) { - console.log('c'); return `${prefix}${phone.slice(prefix.length)}`; } return `${prefix}${phone}`; diff --git a/test/vitest/__tests__/components/common/VnLinkPhone.spec.js b/test/vitest/__tests__/components/common/VnLinkPhone.spec.js new file mode 100644 index 000000000..1d679d979 --- /dev/null +++ b/test/vitest/__tests__/components/common/VnLinkPhone.spec.js @@ -0,0 +1,29 @@ +import { describe, it, expect } from 'vitest'; +import parsePhone from 'src/filters/parsePhone'; + +describe.only('parsePhone filter', () => { + it("adds prefix +34 if it doesn't have one", () => { + const resultado = parsePhone('123456789', '34'); + expect(resultado).toBe('34123456789'); + }); + + it('maintains prefix +34 if it is already correct', () => { + const resultado = parsePhone('+34123456789', '34'); + expect(resultado).toBe('34123456789'); + }); + + it('converts prefix 0034 to +34', () => { + const resultado = parsePhone('0034123456789', '34'); + expect(resultado).toBe('34123456789'); + }); + + it('converts prefix 34 without symbol to +34', () => { + const resultado = parsePhone('34123456789', '34'); + expect(resultado).toBe('34123456789'); + }); + + it.only('replaces incorrect prefix with the correct one', () => { + const resultado = parsePhone('+44123456789', '34'); + expect(resultado).toBe('44123456789'); + }); +});