-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |