From 2096ba2ef3f6ed3928dcc1ceb76f4b9cefb818ec Mon Sep 17 00:00:00 2001 From: Safaa Mahdir <safaa.mahdir@imt-atlantique.net> Date: Wed, 26 Mar 2025 17:39:53 +0100 Subject: [PATCH] test: tests unitaires des fonctions de filtrage --- filter.test.js | 32 +++++++++++++++++++++++++ gui.js | 58 +++++++++++++++++++++++++++++++++++++++++++++ index.html | 2 +- package.json | 12 ++++++++++ pokemonprovider.js | 38 +++++++++++++++++++++++++++++ script.js | 59 ++++------------------------------------------ 6 files changed, 145 insertions(+), 56 deletions(-) create mode 100644 gui.js create mode 100644 package.json create mode 100644 pokemonprovider.js diff --git a/filter.test.js b/filter.test.js index e69de29..e37a5b1 100644 --- a/filter.test.js +++ b/filter.test.js @@ -0,0 +1,32 @@ +const { filterByWeight, filterByHeight } = require('./script.js') + +test('filtre les pokémons dont le poids est supérieur à 3kg', () => { + const pokemons = [ + { name: 'Bulbasaur', weight: 6.9, height: 0.7 }, + { name: 'ivysaur', weight: 13, height: 1 }, + { name: 'venusaur', weight: 100, height: 2 }, + { name: 'Charmander', weight: 2, height: 0.6 }, + ]; + + const result = filterByWeight(pokemons); + expect(result).toEqual([ + { name: 'Bulbasaur', weight: 6.9, height: 0.7 }, + { name: 'ivysaur', weight: 13, height: 1 }, + { name: 'venusaur', weight: 100, height: 2 }, + ]); +}); + +test('filtre les pokémons dont la taille est inférieure à 1m', () => { + const pokemons = [ + { name: 'Bulbasaur', weight: 6.9, height: 0.7 }, + { name: 'ivysaur', weight: 13, height: 1 }, + { name: 'venusaur', weight: 100, height: 2 }, + { name: 'Charmander', weight: 2, height: 0.6 }, + ]; + + const result = filterByHeight(pokemons); + expect(result).toEqual([ + { name: 'Bulbasaur', weight: 6.9, height: 0.7 }, + { name: 'Charmander', weight: 2, height: 0.6 }, + ]); +}); \ No newline at end of file diff --git a/gui.js b/gui.js new file mode 100644 index 0000000..1a39b27 --- /dev/null +++ b/gui.js @@ -0,0 +1,58 @@ + +function fetchPokemonList(callback, limit = 20) { + const baseApiUrl = "https://pokeapi.co/api/v2/pokemon"; + const url = baseApiUrl + "?limit=" + limit; + const request = async () => { + const response = await fetch(url); + const json = await response.json(); + let promisesArray = json["results"].map(result => { + return fetch(result.url).then(response => response.json()); + }); + return Promise.all(promisesArray); + }; + request().then((data) => { + let pokemons = []; + for (let i = 0; i < data.length; ++i) { + pokemons.push({ + "name": data[i].name, + "weight": data[i].weight / 10, // Convert to kg + "height": data[i].height / 10 // Convert to m + }); + } + callback(pokemons); + }); +} + + +function displayPokemons(list) { + const container = document.getElementById("pokemon-list"); + container.innerHTML = ""; + list.forEach(pokemon => { + const div = document.createElement("li"); + div.textContent = `${pokemon.name} - ${pokemon.weight}kg - ${pokemon.height}m`; + container.appendChild(div); + }); +} + + +fetchPokemonList((pokemons) => { + displayPokemons(pokemons); +}); + +function filterByWeight(pokemons) { + return pokemons.filter(pokemon => pokemon.weight > 3); +} + +function filterByHeight(pokemons) { + return pokemons.filter(pokemon => pokemon.height < 1); +} + +document.getElementById("all").addEventListener("click", () => fetchPokemonList(displayPokemons)); + +document.getElementById("weight").addEventListener("click", () => fetchPokemonList((pokemons) => { + displayPokemons(filterByWeight(pokemons)); +})); + +document.getElementById("height").addEventListener("click", () => fetchPokemonList((pokemons) => { + displayPokemons(filterByHeight(pokemons)); +})); diff --git a/index.html b/index.html index 8b7a0f4..220ac35 100644 --- a/index.html +++ b/index.html @@ -12,6 +12,6 @@ <button id="weight">+ de 3kg</button> <button id="height">- de 1m</button> <ul id="pokemon-list"></ul> - <script src="script.js"></script> + <script src="gui.js"></script> </body> </html> \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..90021b6 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "devDependencies": { + "jest": "^29.7.0" + }, + "scripts": { + "test": "jest" + } +} + + + + diff --git a/pokemonprovider.js b/pokemonprovider.js new file mode 100644 index 0000000..32eeb7a --- /dev/null +++ b/pokemonprovider.js @@ -0,0 +1,38 @@ +function fetchPokemonList(callback, limit = 20) { + const baseApiUrl = "https://pokeapi.co/api/v2/pokemon"; + const url = baseApiUrl + "?limit=" + limit; + const request = async () => { + const response = await fetch(url); + const json = await response.json(); + let promisesArray = json["results"].map(result => { + return fetch(result.url).then(response => response.json()); + }); + return Promise.all(promisesArray); + }; + request().then((data) => { + let pokemons = []; + for (let i = 0; i < data.length; ++i) { + pokemons.push({ + "name": data[i].name, + "weight": data[i].weight / 10, // Convert to kg + "height": data[i].height / 10 // Convert to m + }); + } + callback(pokemons); + }); +} + + +function displayPokemons(list) { + const container = document.getElementById("pokemon-list"); + container.innerHTML = ""; + list.forEach(pokemon => { + const div = document.createElement("li"); + div.textContent = `${pokemon.name} - ${pokemon.weight}kg - ${pokemon.height}m`; + container.appendChild(div); + }); +} + + +exports.fetchPokemonList = fetchPokemonList; +exports.displayPokemons = displayPokemons; diff --git a/script.js b/script.js index fb924b9..1d4cffd 100644 --- a/script.js +++ b/script.js @@ -1,64 +1,13 @@ -// Récupérer les Pokémon via l'API -function fetchPokemonList(callback, limit = 20) { - const baseApiUrl = "https://pokeapi.co/api/v2/pokemon"; - const url = baseApiUrl + "?limit=" + limit; - const request = async () => { - const response = await fetch(url); - const json = await response.json(); - let promisesArray = json["results"].map(result => { - return fetch(result.url).then(response => response.json()); - }); - return Promise.all(promisesArray); - }; - request().then((data) => { - let pokemons = []; - for (let i = 0; i < data.length; ++i) { - pokemons.push({ - "name": data[i].name, - "weight": data[i].weight / 10, // Convert to kg - "height": data[i].height / 10 // Convert to m - }); - } - callback(pokemons); - }); -} - -function displayPokemons(list) { - const container = document.getElementById("pokemon-list"); - container.innerHTML = ""; - list.forEach(pokemon => { - const div = document.createElement("li"); - div.textContent = `${pokemon.name} - ${pokemon.weight}kg - ${pokemon.height}m`; - container.appendChild(div); - }); -} -fetchPokemonList((pokemons) => { - displayPokemons(pokemons); -}); +// Fonctions de filtrage -//Fonctions de filtrage function filterByWeight(pokemons) { return pokemons.filter(pokemon => pokemon.weight > 3); } + function filterByHeight(pokemons) { return pokemons.filter(pokemon => pokemon.height < 1); } -// Filtrage -document.getElementById("all").addEventListener("click", () => fetchPokemonList((pokemons) => { - displayPokemons(pokemons); -})); - -document.getElementById("weight").addEventListener("click", () => { - fetchPokemonList((pokemons) => { - displayPokemons(filterByWeight(pokemons));} - ); -}); - -document.getElementById("height").addEventListener("click", () => { - fetchPokemonList((pokemons) => { - displayPokemons(filterByHeight(pokemons)); - }); -}); - +exports.filterByWeight = filterByWeight; +exports.filterByHeight = filterByHeight; \ No newline at end of file -- GitLab