From 3a078957ca5bf692cb5056f3c48d7e4e6beeb88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lasse=20J=C3=B8rgensen?= <28780271+lasjorg@users.noreply.github.com> Date: Mon, 24 Jun 2024 13:06:14 +0200 Subject: [PATCH] fix(curriculum): update tests for pokemon search app (#55221) --- .../build-a-pokemon-search-app.md | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-pokemon-search-app-project/build-a-pokemon-search-app.md b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-pokemon-search-app-project/build-a-pokemon-search-app.md index 6b629b66b48..fe53b0285f3 100644 --- a/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-pokemon-search-app-project/build-a-pokemon-search-app.md +++ b/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/build-a-pokemon-search-app-project/build-a-pokemon-search-app.md @@ -361,30 +361,17 @@ async () => { const searchButton = document.getElementById('search-button'); let alertMessage; window.alert = (message) => alertMessage = message; // Override alert and store message - const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' - const numbers = '0123456789'; - const charactersLength = letters.length; - const numbersLength = numbers.length; + const randomInvalidPokeId = crypto.randomUUID().substring(0, 6); - const firstLetter = letters.charAt(Math.floor(Math.random() * charactersLength)); - const secondLetter = letters.charAt(Math.floor(Math.random() * charactersLength)); - const thirdLetter = letters.charAt(Math.floor(Math.random() * charactersLength)); - const fourthLetter = letters.charAt(Math.floor(Math.random() * charactersLength)); - const randomNumber1 = numbers.charAt(Math.floor(Math.random() * numbersLength)); - const randomNumber2 = numbers.charAt(Math.floor(Math.random() * numbersLength)); - - const badName = firstLetter + secondLetter + thirdLetter + fourthLetter + randomNumber1 + randomNumber2; - - const randomInvalidPokeId = badName; searchInput.value = randomInvalidPokeId; searchInput.dispatchEvent(new Event('change')); searchButton.click(); - const res = await fetch('https://pokeapi-proxy.freecodecamp.rocks/api/pokemon/' + randomInvalidPokeId.toString()); // Fetch from proxy to simulate network delay + const res = await fetch('https://pokeapi-proxy.freecodecamp.rocks/api/pokemon/' + randomInvalidPokeId); // Fetch from proxy to simulate network delay if (!res.ok) { - await new Promise(resolve => setTimeout(resolve, 1000)); // Additional delay to allow the alert to trigger + await new Promise(resolve => setTimeout(resolve, 2000)); // Additional delay to allow the alert to trigger assert.include(['pokémon not found', 'pokemon not found'], alertMessage.trim().replace(/[.,?!]+$/g, '').toLowerCase()); } @@ -405,24 +392,22 @@ async () => { let alertMessage; window.alert = (message) => alertMessage = message; // Override alert and store message - const randomValidPokeId = Math.floor(Math.random() * 1025) + 1; + const randomValidPokeId = String(Math.floor(Math.random() * 1025) + 1); + searchInput.value = randomValidPokeId; searchInput.dispatchEvent(new Event('change')); searchButton.click(); - const res = await fetch('https://pokeapi-proxy.freecodecamp.rocks/api/pokemon/' + randomValidPokeId.toString()); // Fetch from proxy to simulate network delay + const res = await fetch('https://pokeapi-proxy.freecodecamp.rocks/api/pokemon/' + randomValidPokeId); // Fetch from proxy to simulate network delay if (res.ok) { - await new Promise(resolve => setTimeout(resolve, 1000)); // Additional delay to allow UI to update + await new Promise(resolve => setTimeout(resolve, 2000)); // Additional delay to allow UI to update - const data = await res.json(); - - const typesEl = document.getElementById('types'); - - const actualTypes = data.types.map(typeSlot => typeSlot.type.name); + const data = await res.json(); + const typesEl = document.getElementById('types'); + const actualTypes = data.types.map(typeSlot => typeSlot.type.name); assert.lengthOf(typesEl.children, actualTypes.length); - assert.sameMembers(actualTypes, [...typesEl.children].map(el => el.innerText.trim().toLowerCase())); } } catch (err) {