Skip to content

Commit 90e803b

Browse files
committed
[js][http_api][01_base] Add base
1 parent e342c63 commit 90e803b

File tree

10 files changed

+6170
-0
lines changed

10 files changed

+6170
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.tmp/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@babel/preset-env"],
3+
"plugins": ["@babel/plugin-proposal-class-properties"]
4+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
verbose: true,
4+
cacheDirectory: './.tmp/jestCache',
5+
transform: {'^.+\\.js$': 'babel-jest'},
6+
testMatch:[ "**/tests/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)" ],
7+
moduleFileExtensions: ['js']
8+
};

examples/js/js-http_api-01_base/package-lock.json

Lines changed: 6055 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "student_grades",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "jest"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"jest": {
12+
"moduleFileExtensions": [
13+
"js",
14+
"jsx"
15+
],
16+
"moduleDirectories": [
17+
"node_modules",
18+
"src"
19+
]
20+
},
21+
"devDependencies": {
22+
"@babel/core": "^7.11.6",
23+
"@babel/plugin-proposal-class-properties": "^7.10.4",
24+
"@babel/preset-env": "^7.11.5",
25+
"@jest/globals": "^26.4.2",
26+
"jest": "^26.4.2"
27+
},
28+
"dependencies": {
29+
"sync-mysql": "^3.0.1",
30+
"sync-sql": "^1.0.2"
31+
}
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class Course {
2+
constructor(id, name, duration) {
3+
this.id = id;
4+
this.name = name;
5+
this.duration = duration;
6+
}
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {Course} from "./Course";
2+
3+
export class CourseCreator {
4+
constructor(repository) {
5+
this.repository = repository;
6+
}
7+
8+
create(request) {
9+
const course = new Course(request.body.id, request.body.name, request.body.duration);
10+
11+
this.repository.save(course);
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export class CoursesController {
2+
constructor(courseCreator) {
3+
this.courseCreator = courseCreator;
4+
}
5+
6+
create(request) {
7+
this.courseCreator.create(request);
8+
9+
return {status: 201};
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {MockCourseRepository} from "./MockCourseRepository";
2+
import {CoursesController} from "../src/CoursesController";
3+
import {CourseCreator} from "../src/CourseCreator";
4+
import {Course} from "../src/Course";
5+
6+
describe('CoursesController should', () => {
7+
it('create a valid course', () => {
8+
const request = {
9+
body: {
10+
id: "659045a4-300c-4a49-aee6-a4d880cf3079",
11+
name: "Refactoring: Bloaters",
12+
duration: 777
13+
}
14+
};
15+
16+
const expectedCourse = new Course("659045a4-300c-4a49-aee6-a4d880cf3079", "Refactoring: Bloaters", 777);
17+
18+
const repository = new MockCourseRepository(expectedCourse);
19+
const creator = new CourseCreator(repository);
20+
const controller = new CoursesController(creator);
21+
22+
expect(controller.create(request)).toStrictEqual({status: 201});
23+
});
24+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export class MockCourseRepository {
2+
constructor(expectedCourseToBeSaved) {
3+
this.expectedCourseToBeSaved = expectedCourseToBeSaved;
4+
}
5+
6+
save(course) {
7+
if (course.id !== this.expectedCourseToBeSaved.id ||
8+
course.name !== this.expectedCourseToBeSaved.name ||
9+
course.duration !== this.expectedCourseToBeSaved.duration
10+
) {
11+
throw "The expected course and the actual are not the same";
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)