From a6c9bea0956075de718327467dcc023abafb434c Mon Sep 17 00:00:00 2001 From: Ait Lamine Ilias <aitlaminilias@gmail.com> Date: Wed, 26 Mar 2025 17:58:21 +0100 Subject: [PATCH] test: test unitaire. Ref #4 --- filtrage.js | 27 +++------------------------ filtrage.test.js | 21 +++++++++++++++++---- package.json | 2 +- 3 files changed, 21 insertions(+), 29 deletions(-) diff --git a/filtrage.js b/filtrage.js index 4993f10..86d57b2 100644 --- a/filtrage.js +++ b/filtrage.js @@ -1,25 +1,4 @@ -import {fetchPokemonList} from './pokemonProvider.js'; - -function getPokemons(pokemons) { - const list = pokemons; - console.log(list); +function filterPokemons(pokemons) { + return pokemons.filter(pokemon => pokemon.weight > 3 && pokemon.height < 1); } - - -/** -* Filter Pokémon with weight > 3kg and height < 1m -*/ -function filterPokemons() { - fetchPokemonList((pokemons) => { - const filteredPokemons = pokemons.filter(pokemon => pokemon.weight > 3 ); - displayPokemons(filteredPokemons); - }, 318); // Fetch 318 Pokémon -} -function filterPokemons2() { - fetchPokemonList((pokemons) => { - const filteredPokemons = pokemons.filter(pokemon => pokemon.height < 1 ); - displayPokemons(filteredPokemons); - }, 318); // Fetch 318 Pokémon -} - -fetchPokemonList(getPokemons, 20); \ No newline at end of file +module.exports = filterPokemons; \ No newline at end of file diff --git a/filtrage.test.js b/filtrage.test.js index eb3657f..b35485e 100644 --- a/filtrage.test.js +++ b/filtrage.test.js @@ -1,6 +1,19 @@ -const convert = require('./convert'); +// filterPokemons.test.js +const filterPokemons = require('./filtrage'); // Import your filter function -test('Conversion Euros to Dollars', () => { - expect(convert.convertEurToUsd(3)).toBe(3.114); -}); +test('filters Pokémon with weight > 3kg and height < 1m', () => { + const pokemons = [ + { name: 'Pikachu', weight: 6, height: 0.4 }, + { name: 'Bulbasaur', weight: 3, height: 0.7 }, + { name: 'Squirtle', weight: 4, height: 0.9 }, + { name: 'Charizard', weight: 10, height: 1.7 } + ]; + const filteredPokemons = filterPokemons(pokemons); + + // Assert that the filtered list contains only the valid Pokémon + expect(filteredPokemons).toEqual([ + { name: 'Pikachu', weight: 6, height: 0.4 }, + { name: 'Squirtle', weight: 4, height: 0.9 } + ]); +}); \ No newline at end of file diff --git a/package.json b/package.json index 7302f53..cba0b8e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "pokemonProvider.js", "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "jest" }, "keywords": [], "author": "", -- GitLab