forked from dlealv/UnitTestingApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMockData.js
53 lines (48 loc) · 1.36 KB
/
MockData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// jshint esversion: 8
/**
* Stores mock data that's not available locally to enable offline testing.
*/
let MockData = (function() {
const _registry = new WeakMap();
class MockData {
constructor() {
if (MockData.instance) return MockData.instance;
_registry.set(this, {});
MockData.instance = this;
return MockData.instance;
}
/**
* Adds mock data
* @param {String} key the key of the data
* @param {*} value the actual data
* @returns {this}
*/
addData(key, value) {
if (_registry.get(this)[key]) throw new Error(`Key ${key} already exists`);
if (typeof key !== 'string') throw new Error('The key must be of type string');
_registry.get(this)[key] = value;
return this;
}
/**
* Retrieves mock Data
* @param {String} key
* @returns {*}
*/
getData(key) {
if (!_registry.get(this)[key]) throw new Error(`Data with key ${key} doesn't exist`);
return _registry.get(this)[key];
}
/**
* Deletes mock data
* @param {*} key
* @returns {thiss}
*/
deleteData(key) {
if (!_registry.get(this)[key]) throw new Error(`Data with key ${key} doesn't exist`);
delete _registry.get(this)[key];
return this;
}
}
return MockData;
})();
if (typeof module !== 'undefined') module.exports = MockData;