Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristian-BFMV committed May 23, 2020
0 parents commit 2c30ddc
Show file tree
Hide file tree
Showing 8 changed files with 5,215 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
coverage
24 changes: 24 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const express = require('express');
const app = express();

const { add, subtract } = require('./calculator');

app.get('/add', (req, res) => {
let { value1, value2 } = req.query;
value1 = parseInt(value1);
value2 = parseInt(value2);
res.json({
result: add(value1, value2),
});
});

app.get('/subtract', (req, res) => {
let { value1, value2 } = req.query;
value1 = parseInt(value1);
value2 = parseInt(value2);
res.json({
result: subtract(value1, value2),
});
});

module.exports = app;
20 changes: 20 additions & 0 deletions app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const app = require('./app');
const supertest = require('supertest');
const request = supertest(app); //arrange

test('gets the add endpoint', async done => {
const response = await request.get('/add?value1=2&value2=2'); //Act
expect(response.status).toBe(200);
expect(response.type).toBe('application/json');
expect(response.body.result).toBe(4);
done();
});

test('gets the subtract endpoint', async done => {
const response = await request.get('/subtract?value1=2&value2=4'); //Act
expect(response.status).toBe(200);
expect(response.type).toBe('application/json');
expect(response.body.result).not.toBeNull();
expect(response.body.result).toBe(-2);
done();
});
9 changes: 9 additions & 0 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function add(a, b) {
return a + b;
}

function subtract(a, b) {
return a - b;
}

module.exports = { add, subtract };
25 changes: 25 additions & 0 deletions calculator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { add, subtract } = require('./calculator'); //Arrange

describe('add operation', () => {
test('adds 1 + 2 to equal 3', () => {
var value = add(1, 2); //Act
expect(value).toBe(3); //Assert
});

test('adds 5 + 5 to equal 10', () => {
var value = add(5, 5); //Act
expect(value).toBe(10); //Assert
});
});

describe('subtract operation', () => {
test('subtracts 5 - 5 to equal 0', () => {
var value = subtract(5, 5); //Act
expect(value).toBe(0); //Assert
});

test('subtracts 3 - 2 to equal 1', () => {
var value = subtract(3, 2); //Act
expect(value).toBe(1); //Assert
});
});
Loading

0 comments on commit 2c30ddc

Please sign in to comment.