0
0
Fork 0

test: add vitest VnLinkPhone filter

This commit is contained in:
Javier Segarra 2024-11-26 22:58:36 +01:00
parent 21eda340a8
commit 1b3500bbce
2 changed files with 29 additions and 3 deletions

View File

@ -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}`;

View File

@ -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');
});
});