...
 
Commits (2)
const mapToByYear = require("./mapToByYear");
const sortByKey = require("./sortByKey");
const movies = require("../data.json")
module.exports = () => {
return sortByKey(mapToByYear(movies));
}
const byYear = require("./byYear")
module.exports = (req, res) => {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(byYear()))
}
module.exports = (movies = []) => {
return movies.reduce((acc, value) => {
acc[value.year] = acc[value.year] ? acc[value.year].concat(value) : acc[value.year] = [value];
return acc;
}, {});
}
\ No newline at end of file
const mapToByYear = require("./mapToByYear")
describe("mapToByYear", () => {
it('should be defined', () => {
expect(mapToByYear).toBeDefined();
});
it('should return an empty object if given nothing', () => {
expect(mapToByYear()).toEqual(expect.any(Object));
});
it('should return an empty object if given empty array', () => {
expect(mapToByYear([])).toEqual(expect.any(Object));
});
const mockMovieOneFrom2003 = {
title: "Matrix",
year: "2003"
};
const mockMovieTwoFrom2003 = {
title: "Johnny English",
year: "2003"
};
it('should map two movies to their respective year', () => {
expect(mapToByYear([mockMovieOneFrom2003, mockMovieTwoFrom2003])).toEqual({
2003: [mockMovieOneFrom2003, mockMovieTwoFrom2003]
});
});
const mockMovieOneFrom2001 = {
title: "Herr der Ringe",
year: "2001"
};
it('should map two movies from different years to their respective year', () => {
expect(mapToByYear([mockMovieOneFrom2003, mockMovieOneFrom2001])).toEqual({
2003: [mockMovieOneFrom2003],
2001: [mockMovieOneFrom2001]
});
});
});
module.exports = (movies = {}) => {
return Object.entries(movies)
.sort(([firstKey], [secondKey]) => secondKey - firstKey)
.map(([year, movies]) => {
return {year, movies};
});
}
const sortByKey = require("./sortByKey")
describe("sortByKey", () => {
it('should be defined', () => {
expect(sortByKey).toBeDefined();
});
it('should return an empty array if given nothing', () => {
expect(sortByKey()).toEqual(expect.any(Array));
});
it('should return an empty array if given empty array', () => {
expect(sortByKey([])).toEqual(expect.any(Array));
});
it('should not change an already sorted object', () => {
const mockMoviesByYear = {
2018: []
};
expect(sortByKey(mockMoviesByYear)).toEqual([{year: "2018", movies: []}]);
});
it('should not change an already sorted object - with movie', () => {
const mockMoviesByYearWithMovie = {
2003: [{
title: "Matrix",
year: "2003"
}]
};
expect(sortByKey(mockMoviesByYearWithMovie)).toEqual([{year: "2003", movies: [{
title: "Matrix",
year: "2003"
}]}]);
});
it('should sort by year', () => {
const mockMoviesByYearWithMovie = {
1992: [],
2003: []
};
expect(sortByKey(mockMoviesByYearWithMovie)).toEqual([{
year: "2003", movies: []
}, {
year: "1992", movies: []
}]);
});
});
......@@ -12,7 +12,7 @@
"test:api": "npm run test:api:unit",
"test:api:unit": "jest --coverage api/.*/.*\\.test.js",
"test:api:unit:watch": "npm run test:api:unit -- --watch",
"test:webapp": "npm run test:webapp:unit && npm run test:webapp:build",
"test:webapp": "npm run test:webapp:unit && npm run test:webapp:build && npm run test:webapp:visualregression",
"test:webapp:unit": "jest --coverage ./components/*.test.js ./lib/*.test.js",
"test:webapp:unit:watch": "npm run test:webapp:unit -- --watch",
"test:webapp:build": "npm run now-build",
......