Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 0 additions & 180 deletions front/src/app/politica-cookies/page.tsx

This file was deleted.

155 changes: 155 additions & 0 deletions front/src/pods/embalse-search/embalse-search.business.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { describe, it, expect } from "vitest";
import { normalizeSearchString, getFilteredEmbalses } from './embalse-search.business';

describe('normalizeSearchString', () => {
it('should return a empty string when input is empty', () => {
// Arrange
const input = '';

// Act
const result = normalizeSearchString(input);

// Assert
expect(result).toBe('');
});

it('should return a empty string when input is null', () => {
// Arrange
const input: any = null;

// Act
const result = normalizeSearchString(input);

// Assert
expect(result).toBe('');
});

it('should return a empty string when input is undefined', () => {
// Arrange
const input: any = undefined;

// Act
const result = normalizeSearchString(input);

// Assert
expect(result).toBe('');
});

it('should normalize a string by removing accents, punctuation and extra spaces', () => {
// Arrange
const input = ' Guadalquivir ';
const expectedResult = 'guadalquivir';

// Act
const result = normalizeSearchString(input);

// Assert
expect(result).toBe(expectedResult);
});

it('should normalize a string with accents', () => {
// Arrange
const input = 'Sistema Valle de Arán';
const expectedResult = 'sistema valle de aran';

// Act
const result = normalizeSearchString(input);

// Assert
expect(result).toBe(expectedResult);
});

it('should normalize a string with punctuation', () => {
// Arrange
const input = 'Embalse de la Mora, Huelva.';
const expectedResult = 'embalse de la mora huelva';

// Act
const result = normalizeSearchString(input);

// Assert
expect(result).toBe(expectedResult);
});

it('should return Avinuela, when feed Aviñuela', () => {
// Arrange
const input = 'Aviñuela';
const expectedResult = 'avinuela';

// Act
const result = normalizeSearchString(input);

// Assert
expect(result).toBe(expectedResult);
});

});

describe('getFilteredEmbalses', () => {
it('should return an empty array when embalses and inputValue is empty', () => {
// Arrange
const inputValue = '';
const embalses = [];

// Act
const result = getFilteredEmbalses(inputValue, embalses);

// Assert
expect(result).toEqual([]);
});

it('should return an empty array when embalses is empty and inputValue is null', () => {
// Arrange
const inputValue = null as any;
const embalses = [];

// Act
const result = getFilteredEmbalses(inputValue, embalses);

// Assert
expect(result).toEqual([]);
});

it('should return an empty array when embalses is empty and inputValue is undefined', () => {
// Arrange
const inputValue = undefined as any;
const embalses = [];

// Act
const result = getFilteredEmbalses(inputValue, embalses);

// Assert
expect(result).toEqual([]);
});

it('should return an empty array when embalses is empty', () => {
// Arrange
const inputValue = 'Viñuela';
const embalses = [];

// Act
const result = getFilteredEmbalses(inputValue, embalses);

// Assert
expect(result).toEqual([]);
});

it('should return filtered embalses based on inputValue', () => {
// Arrange
const inputValue = 'Viñuela';
const embalses = [
{ _id: '1', nombre: 'Embalse de la Mora', provincia: 'Huelva' },
{ _id: '2', nombre: 'Embalse de la Viñuela', provincia: 'Málaga' },
];
const expectedResult = [
{ slug: '2', name: 'Embalse de la Viñuela (Málaga)' },
];

// Act
const result = getFilteredEmbalses(inputValue, embalses);

// Assert
expect(result).toEqual(expectedResult);
});
});

35 changes: 35 additions & 0 deletions front/src/pods/embalse-search/embalse-search.business.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { mapEmbalseToSearch } from "./embalse-search.mapper";
import { Embalse } from "./api";

export const normalizeSearchString = (input: string): string => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss unit test on this business methods.

@antonio06 or @manudous could you please shed some light to @zumaichi

return input ?
input.toString()
.normalize("NFD") // Separa los acentos de las letras
.replace(/[\u0300-\u036f]/g, "") // Elimina acentos
.toLowerCase()
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "") // Elimina signos de puntuación
.replace(/\s+/g, " ").trim() : ""; // Reemplaza múltiples espacios por uno
};
// encontre todos esos filtros por internet y los aplico si sobra alguno lo quitamos, también dejé las descripciones

export const getFilteredEmbalses = (
inputValue: string,
embalses: Embalse[],
) => {
if (!embalses || embalses.length === 0) {
return [];
}

const lower = normalizeSearchString(inputValue);
const normalizedInputValue = normalizeSearchString(inputValue);

return embalses
.filter(
(e) =>
e.nombre.toLowerCase().includes(lower) ||
(e.provincia ?? "").toLowerCase().includes(lower) ||
normalizeSearchString(e.nombre ?? "").includes(normalizedInputValue) ||
normalizeSearchString(e.provincia ?? "").includes(normalizedInputValue),
)
.map(mapEmbalseToSearch);
};
Loading