- Node >= 18
- Run
npm installto install dependencies - use
node prueba.jsto run the code and print in the console the properties titles - use
npm run testto run the tests
| { | |
| "dependencies": { | |
| "jest": "^29.7.0" | |
| }, | |
| "scripts": { | |
| "test": "jest" | |
| } | |
| } |
| const API_KEY = "l7u502p8v46ba3ppgvj5y2aad50lb9"; // This should be in a .env file, it's here only for the example | |
| const BASE_URL = "https://api.stagingeb.com/v1/properties"; | |
| async function getAllProperties(params) { | |
| const urlParams = new URLSearchParams(params); | |
| try { | |
| const response = await fetch(`${BASE_URL}?${urlParams}`, { | |
| headers: { | |
| Accept: "application/json", | |
| "X-Authorization": API_KEY, | |
| }, | |
| }); | |
| const data = await response.json(); | |
| return data.content; | |
| } catch (error) { | |
| console.error(error); | |
| } | |
| } | |
| function formatProperties(properties, lang = "es") { | |
| const propertiesTitles = properties.map((property) => property.title); | |
| const formatter = new Intl.ListFormat(lang, { | |
| style: "long", | |
| type: "conjunction", | |
| }); | |
| return formatter.format(propertiesTitles); | |
| } | |
| getAllProperties().then((res) => { | |
| console.log(formatProperties(res)); | |
| }); | |
| module.exports = { getAllProperties, formatProperties }; | |
| const { getAllProperties, formatProperties } = require("./Prueba.js"); | |
| // Test the service | |
| describe("Test getAllProperties", () => { | |
| test("Can paginate", async () => { | |
| const properties = await getAllProperties({ limit: 2 }); | |
| expect(properties).toHaveLength(2); | |
| }); | |
| }); | |
| describe("Test formatProperties", () => { | |
| const properties = [ | |
| { title: "Title 1" }, | |
| { title: "Title 2" }, | |
| { title: "Title 3" }, | |
| ]; | |
| test("Spanish is the default lang", async () => { | |
| const result = formatProperties(properties); | |
| expect(result).toBe("Title 1, Title 2 y Title 3"); | |
| }); | |
| test("Concats correctly in Spanish", async () => { | |
| const result = formatProperties(properties, "es"); | |
| expect(result).toBe("Title 1, Title 2 y Title 3"); | |
| }); | |
| test("Concats correctly in English", async () => { | |
| const result = formatProperties(properties, "en"); | |
| expect(result).toBe("Title 1, Title 2, and Title 3"); | |
| }); | |
| }); |