Skip to content

Instantly share code, notes, and snippets.

@agustinzamar
Created October 31, 2023 20:19
Show Gist options
  • Save agustinzamar/c7d4f1a0f18aa59f31de7d5358915490 to your computer and use it in GitHub Desktop.
Save agustinzamar/c7d4f1a0f18aa59f31de7d5358915490 to your computer and use it in GitHub Desktop.
Prueba tectnica East Brokers

Pre-requisites

  1. Node >= 18

Instructions

  1. Run npm install to install dependencies
  2. use node prueba.js to run the code and print in the console the properties titles
  3. use npm run test to 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");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment