Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions history/source/rates/RatesService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* This service returns historical exchange rates
*/
module.exports = class RatesService {

constructor(repository) {
Expand All @@ -7,17 +10,14 @@ module.exports = class RatesService {
loadRatesHistory(src, target) {
const data = this.repository.load(src + "_TO_" + target);

// Each data point in "data" follows this format:
// { "value": exchangeRateValue }

if (data === null) {
throw new Error("Unknown currency conversion");
}

return data.map((element, i) => {
const elementWithDate = {...element};
var date = new Date();
date.setDate(date.getDate() - i);
elementWithDate["date"] = date;
return elementWithDate;
});
return data.map(rate => ({ date: new Date() , ...rate }));
}

};
35 changes: 22 additions & 13 deletions history/tests/unit/rates/RatesService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ const RatesService = require("../../../source/rates/RatesService");

describe("RatesService", () => {

// test("returns rates retrieved from a rates repository", () => {
// const repository = {
// load: () => [{ value: 1 }, { value: 2 }, { value: 3 } ]
// };

// const service = new RatesService(repository);
// const rates = service.loadRatesHistory("EUR", "USD");
// expect(rates).toEqual([{ value: 1 }, { value: 2 }, { value: 3 } ]);
// });

test("throws an exception if the repository returns null", () => {
function invalidScenario() {
const repository = {
Expand All @@ -24,14 +14,33 @@ describe("RatesService", () => {
expect(invalidScenario).toThrow(/Unknown currency conversion/i);
});

test("returns a list of currency exchange rates with dates", () => {
test("returns exchange rates", () => {
// Given
const repository = {
load: () => [{ value: 1 }, { value: 2 }, { value: 3 } ]
load: () => [{ value: 0.5}]
};
const service = new RatesService(repository);

// When
const rates = service.loadRatesHistory("EUR", "USD");

// Then
expect(rates[0].value).toBe(0.5);
});


test("returns exchange rates including dates", () => {
// Given
const repository = {
load: () => [{ value: 0.5}, { value: 0.3 }]
};
const service = new RatesService(repository);

// When
const rates = service.loadRatesHistory("EUR", "USD");
for (const rate of rates) {

// Then
for(const rate of rates) {
expect(rate.date).toBeInstanceOf(Date);
}
});
Expand Down