From 41cf7f7ce41123a6a560b034cf0be0cff5e3c791 Mon Sep 17 00:00:00 2001 From: FutureDuck Date: Sat, 13 Jun 2020 19:28:37 -0700 Subject: [PATCH] feat: metric model --- __tests__/src/shared/model/metric.spec.js | 23 ++++++++++++++++++ src/lib/model/list.js | 2 +- src/shared/model/metric.js | 29 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 __tests__/src/shared/model/metric.spec.js create mode 100644 src/shared/model/metric.js diff --git a/__tests__/src/shared/model/metric.spec.js b/__tests__/src/shared/model/metric.spec.js new file mode 100644 index 0000000..9fc0b93 --- /dev/null +++ b/__tests__/src/shared/model/metric.spec.js @@ -0,0 +1,23 @@ +import Metric from '../../../../src/shared/model/metric.js'; + +describe('Metric', () => { + it('should throw with an inexisting dimension', () => { + expect(() => { + new Metric('my-made-up-dimension', 1); + }).toThrow(/Invalid dimension/); + //TODO https://github.com/lfilho/ddg-test-project/issues/46 + }); + + it('accepts a known dimension', () => { + expect(() => { + new Metric(Metric.dimensions.REQUEST_BLOCKED, 1); + }).not.toThrow(); + }); + + it('requires both dimension and value args', () => { + expect(() => { + new Metric('one-arg'); + }).toThrow(/Metric constructor needs both a dimension and value for it/); + //TODO https://github.com/lfilho/ddg-test-project/issues/46 + }); +}); diff --git a/src/lib/model/list.js b/src/lib/model/list.js index 7fa8114..f5f6e6c 100644 --- a/src/lib/model/list.js +++ b/src/lib/model/list.js @@ -8,7 +8,7 @@ export default class List { constructor(type) { if (!type) { throw new Error('List constructor needs a type'); - //TODO Refactor Error strategy later on to contain an unique error code and both technical and user friendly messages + //TODO https://github.com/lfilho/ddg-test-project/issues/46 } this.list = new Set(); this.type = type; diff --git a/src/shared/model/metric.js b/src/shared/model/metric.js new file mode 100644 index 0000000..5315a39 --- /dev/null +++ b/src/shared/model/metric.js @@ -0,0 +1,29 @@ +const DIMENSIONS = Object.freeze({ + REQUEST_BLOCKED: 'REQUEST_BLOCKED', +}); + +export default class Metric { + /** + * A simple dimension-value tuple + */ + constructor(dimension, value) { + if (!dimension || !value) { + //TODO https://github.com/lfilho/ddg-test-project/issues/46 + throw new Error( + 'Metric constructor needs both a dimension and value for it' + ); + } + const possibleDimensions = Object.keys(DIMENSIONS); + if (!possibleDimensions.includes(dimension)) { + //TODO https://github.com/lfilho/ddg-test-project/issues/46 + throw new Error(`Invalid dimension. Valid ones: ${possibleDimensions}`); + } + + this.dimension = dimension; + this.value = value; + } + + static get dimensions() { + return DIMENSIONS; + } +}