Skip to content

Commit

Permalink
feat: metric model
Browse files Browse the repository at this point in the history
  • Loading branch information
FutureDuck authored and lfilho committed Jun 14, 2020
1 parent f4916da commit 41cf7f7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
23 changes: 23 additions & 0 deletions __tests__/src/shared/model/metric.spec.js
Original file line number Diff line number Diff line change
@@ -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
});
});
2 changes: 1 addition & 1 deletion src/lib/model/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 29 additions & 0 deletions src/shared/model/metric.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 41cf7f7

Please sign in to comment.