Fixed: Issue where no parameters would fail
Fixed: Issue with end date earlier than start date being allowed Added: Jest tests Updated: tests
This commit is contained in:
parent
c94eedfcd3
commit
4a3d75d2cc
@ -142,9 +142,15 @@ class Carpark {
|
||||
*/
|
||||
calculate(start, end, mode = 'longterm') {
|
||||
let workVal = 0.0;
|
||||
// Ensure there are 3 arguments
|
||||
if (arguments.length < 3) return workVal;
|
||||
|
||||
this.#_startDT = fecha.parse(start, 'DD/MM/YYYY HH:mm:ssZ');
|
||||
this.#_endDT = fecha.parse(end, 'DD/MM/YYYY HH:mm:ssZ');
|
||||
|
||||
// Ensure that endDT is after startDT;
|
||||
if (this.#_endDT.getTime() <= this.#_startDT.getTime()) return workVal;
|
||||
|
||||
switch (mode.toLowerCase()) {
|
||||
case 'short':
|
||||
case 'shortterm':
|
||||
|
3786
package-lock.json
generated
3786
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -6,6 +6,7 @@
|
||||
"scripts": {
|
||||
"test:js": "mocha test/**/*.js",
|
||||
"test:ts": "mocha -r ts-node/register test/**/*.ts",
|
||||
"test:jest": "jest",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"coverage": "nyc npm run test:js",
|
||||
"build:js": "rollup -c rollup.config.js"
|
||||
@ -15,6 +16,7 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.2.14",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@types/mocha": "^8.0.3",
|
||||
"@types/node": "^14.14.6",
|
||||
"@typescript-eslint/eslint-plugin": "^4.6.1",
|
||||
@ -22,6 +24,7 @@
|
||||
"chai": "^4.2.0",
|
||||
"eslint": "^7.12.1",
|
||||
"expect.js": "^0.3.1",
|
||||
"jest": "^26.6.3",
|
||||
"mocha": "^8.2.1",
|
||||
"nyc": "^15.1.0",
|
||||
"rollup": "^2.33.1",
|
||||
|
@ -3,66 +3,63 @@ import Carpark from '../ts-lib/carpark-calc';
|
||||
import { expect } from 'chai';
|
||||
// const expect = require('expect.js');
|
||||
|
||||
|
||||
describe('Carpark Calculator', () => {
|
||||
let carPark: Carpark;
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
carPark = new Carpark();
|
||||
});
|
||||
|
||||
it('Should return 0.00 outside of a chargeable period', () => {
|
||||
|
||||
const start = '09/09/2017 05:20:00';
|
||||
const end = '09/09/2017 19:15:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'short')).to.equal(0);
|
||||
});
|
||||
|
||||
|
||||
it('A specific short stay should cost £12.28', () => {
|
||||
|
||||
const start = '07/09/2017 16:50:00';
|
||||
const end = '09/09/2017 19:15:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'short')).to.equal(12.28);
|
||||
|
||||
});
|
||||
|
||||
it('A specific long stay should cost £22.50', () => {
|
||||
|
||||
const start = '07/09/2017 07:50:00';
|
||||
const end = '09/09/2017 05:20:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(22.50);
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(22.5);
|
||||
});
|
||||
|
||||
it('A very specific long stay should cost £15.00', () => {
|
||||
|
||||
const start = '24/10/2020 10:00:00';
|
||||
const end = '25/10/2020 10:00:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(15.00);
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(15.0);
|
||||
});
|
||||
|
||||
it('A single day of long stay should cost £7.50', () => {
|
||||
|
||||
const start = '24/10/2020 10:00:00';
|
||||
const end = '24/10/2020 23:59:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(7.50);
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(7.5);
|
||||
});
|
||||
|
||||
it('A single week of long stay should cost £52.50', () => {
|
||||
|
||||
const start = '24/10/2020 10:00:00';
|
||||
const end = '30/10/2020 10:00:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(52.50);
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(52.5);
|
||||
});
|
||||
|
||||
describe('Error Handling', () => {
|
||||
// Returns 0.0 for less than 3 arguments test not required.
|
||||
|
||||
it('Returns 0.0 for end time before start time', () => {
|
||||
const end = '24/10/2020 10:00:00';
|
||||
const start = '30/10/2020 10:00:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.equal(0.0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -50,4 +50,17 @@ describe('Carpark Calculator', () => {
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.be(52.5);
|
||||
});
|
||||
|
||||
describe('Error Handling', () => {
|
||||
it('Returns 0.0 for less than 3 arguments', () => {
|
||||
expect(carPark.calculate()).to.be(0.0);
|
||||
});
|
||||
|
||||
it('Returns 0.0 for end time before start time', () => {
|
||||
const end = '24/10/2020 10:00:00';
|
||||
const start = '30/10/2020 10:00:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).to.be(0.0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
70
test/carpark.test.js
Normal file
70
test/carpark.test.js
Normal file
@ -0,0 +1,70 @@
|
||||
const Carpark = require('../lib/carpark');
|
||||
|
||||
describe('Carpark Calculator', () => {
|
||||
let carPark;
|
||||
|
||||
beforeEach(() => {
|
||||
carPark = new Carpark();
|
||||
});
|
||||
|
||||
it('Should return 0.00 outside of a chargeable period', (done) => {
|
||||
const start = '09/09/2017 05:20:00';
|
||||
const end = '09/09/2017 19:15:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'short')).toBe(0);
|
||||
done();
|
||||
});
|
||||
|
||||
it('A specific short stay should cost £12.28', (done) => {
|
||||
const start = '07/09/2017 16:50:00';
|
||||
const end = '09/09/2017 19:15:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'short')).toBe(12.28);
|
||||
done();
|
||||
});
|
||||
|
||||
it('A specific long stay should cost £22.50', (done) => {
|
||||
const start = '07/09/2017 07:50:00';
|
||||
const end = '09/09/2017 05:20:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).toBe(22.5);
|
||||
done();
|
||||
});
|
||||
|
||||
it('A very specific long stay should cost £15.00', (done) => {
|
||||
const start = '24/10/2020 10:00:00';
|
||||
const end = '25/10/2020 10:00:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).toBe(15.0);
|
||||
done();
|
||||
});
|
||||
|
||||
it('A single day of long stay should cost £7.50', (done) => {
|
||||
const start = '24/10/2020 10:00:00';
|
||||
const end = '24/10/2020 23:59:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).toBe(7.5);
|
||||
done();
|
||||
});
|
||||
|
||||
it('A single week of long stay should cost £52.50', (done) => {
|
||||
const start = '24/10/2020 10:00:00';
|
||||
const end = '30/10/2020 10:00:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).toBe(52.5);
|
||||
done();
|
||||
});
|
||||
|
||||
describe('Error Handling', () => {
|
||||
it('Returns 0.0 for less than 3 arguments', () => {
|
||||
expect(carPark.calculate()).toBe(0.0);
|
||||
});
|
||||
|
||||
it('Returns 0.0 for end time before start time', () => {
|
||||
const end = '24/10/2020 10:00:00';
|
||||
const start = '30/10/2020 10:00:00';
|
||||
|
||||
expect(carPark.calculate(start, end, 'long')).toBe(0.0);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,8 +1,8 @@
|
||||
import { parse } from 'fecha';
|
||||
|
||||
class Carpark {
|
||||
private _startDT: Date;
|
||||
private _endDT: Date;
|
||||
private _startDT: Date = new Date(0);
|
||||
private _endDT: Date = new Date(0);
|
||||
private _dayLength: number = 60 * 60 * 24;
|
||||
private _hourLength: number = 60 * 60;
|
||||
private _validDays: number[] = [1, 2, 3, 4, 5]; // Monday, Tuesday, Wednesday, Thursday, Friday
|
||||
@ -99,9 +99,10 @@ class Carpark {
|
||||
*/
|
||||
calculate(start: string, end: string, mode: string = 'longterm'): number {
|
||||
let workVal = 0.0;
|
||||
this._startDT = parse(start, 'DD/MM/YYYY HH:mm:ssZ');
|
||||
this._endDT = parse(end, 'DD/MM/YYYY HH:mm:ssZ');
|
||||
this._startDT = <Date>parse(start, 'DD/MM/YYYY HH:mm:ssZ');
|
||||
this._endDT = <Date>parse(end, 'DD/MM/YYYY HH:mm:ssZ');
|
||||
|
||||
if (this._endDT.getTime() <= this._startDT.getTime()) return workVal;
|
||||
switch (mode.toLowerCase()) {
|
||||
case 'short':
|
||||
case 'shortterm':
|
||||
|
@ -1,10 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"declaration": true,
|
||||
"target": "ES2018",
|
||||
"module": "commonjs",
|
||||
"noImplicitAny": true,
|
||||
"removeComments": true,
|
||||
"preserveConstEnums": true,
|
||||
"sourceMap": true
|
||||
"emitDecoratorMetadata": true,
|
||||
"experimentalDecorators": true,
|
||||
"sourceMap": true,
|
||||
"esModuleInterop": true,
|
||||
"outDir": "./dist"
|
||||
},
|
||||
"include": ["ts-lib/**/*"],
|
||||
"exclude": ["node_modules", "test/**/*.ts"]
|
||||
|
Loading…
Reference in New Issue
Block a user