Skip to content
Snippets Groups Projects
Commit 2096ba2e authored by MAHDIR Safaa's avatar MAHDIR Safaa
Browse files

test: tests unitaires des fonctions de filtrage

parent 6d3c83b3
No related branches found
No related tags found
1 merge request!4Tache4
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
gui.js 0 → 100644
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));
}));
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
<button id="weight">+ de 3kg</button> <button id="weight">+ de 3kg</button>
<button id="height">- de 1m</button> <button id="height">- de 1m</button>
<ul id="pokemon-list"></ul> <ul id="pokemon-list"></ul>
<script src="script.js"></script> <script src="gui.js"></script>
</body> </body>
</html> </html>
\ No newline at end of file
{
"devDependencies": {
"jest": "^29.7.0"
},
"scripts": {
"test": "jest"
}
}
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;
// 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) => { // Fonctions de filtrage
displayPokemons(pokemons);
});
//Fonctions de filtrage
function filterByWeight(pokemons) { function filterByWeight(pokemons) {
return pokemons.filter(pokemon => pokemon.weight > 3); return pokemons.filter(pokemon => pokemon.weight > 3);
} }
function filterByHeight(pokemons) { function filterByHeight(pokemons) {
return pokemons.filter(pokemon => pokemon.height < 1); return pokemons.filter(pokemon => pokemon.height < 1);
} }
// Filtrage exports.filterByWeight = filterByWeight;
document.getElementById("all").addEventListener("click", () => fetchPokemonList((pokemons) => { exports.filterByHeight = filterByHeight;
displayPokemons(pokemons); \ No newline at end of file
}));
document.getElementById("weight").addEventListener("click", () => {
fetchPokemonList((pokemons) => {
displayPokemons(filterByWeight(pokemons));}
);
});
document.getElementById("height").addEventListener("click", () => {
fetchPokemonList((pokemons) => {
displayPokemons(filterByHeight(pokemons));
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment