fix(curriculum): update tests for pokemon search app (#55221)

This commit is contained in:
Lasse Jørgensen
2024-06-24 13:06:14 +02:00
committed by GitHub
parent 7f381b3a30
commit 3a078957ca

View File

@@ -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) {