Skip to content

Commit cefc3e7

Browse files
authored
Merge pull request #33 from leapfrogtechnology/missing-tests
Add missing tests for edge cases
2 parents 2e8af05 + 9d2d2a3 commit cefc3e7

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed

test/domain.test.ts

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'mocha';
22
import { expect } from 'chai';
33
import * as domain from 'domain';
4+
import { EventEmitter } from 'events';
45

56
import * as globalStore from '../src';
67
import { STORE_KEY } from '../src/StoreDomain';
@@ -13,6 +14,42 @@ describe('store: [adapter=DOMAIN]', () => {
1314
Object.assign(process, { domain: undefined });
1415
});
1516

17+
describe('initialize()', () => {
18+
it('should initialize the store.', done => {
19+
const callback = () => {
20+
expect(!!(process.domain as any)[STORE_KEY]).to.equal(true);
21+
expect(globalStore.isInitialized()).to.equal(true);
22+
23+
done();
24+
};
25+
26+
globalStore.initialize(adapter)(callback);
27+
});
28+
29+
it('should also bind params if params are passed through arguments.', done => {
30+
const req = new EventEmitter();
31+
const res = new EventEmitter();
32+
const emitters = [req, res];
33+
const errorCallback = (err: any) => err;
34+
35+
const callback = () => {
36+
// Postmortem domain to check bound arguments.
37+
expect((process.domain as any)._events.error).to.equal(errorCallback);
38+
expect((process.domain as any).members[0]).to.equal(req);
39+
expect((process.domain as any).members[1]).to.equal(res);
40+
41+
done();
42+
};
43+
44+
globalStore.initialize(adapter)(callback, {
45+
req,
46+
res,
47+
emitters,
48+
error: errorCallback
49+
});
50+
});
51+
});
52+
1653
describe('isInitialized()', () => {
1754
it('should return false when not initialized.', () => {
1855
expect(globalStore.isInitialized()).to.equal(false);
@@ -44,6 +81,17 @@ describe('store: [adapter=DOMAIN]', () => {
4481
expect(globalStore.get.bind(globalStore, 'foo')).to.throw('No active domain found in store.');
4582
});
4683

84+
it('should return null if invoked under active domain w/o proper store initialization.', done => {
85+
const d = domain.create();
86+
87+
d.run(() => {
88+
// Ensure data in the existing domain is available at this point.
89+
expect(globalStore.get('foo')).to.equal(null);
90+
91+
done();
92+
});
93+
});
94+
4795
it('should return `undefined` if the value was not set.', done => {
4896
const callback = () => {
4997
expect(globalStore.get('foo')).to.equal(undefined);
@@ -55,7 +103,7 @@ describe('store: [adapter=DOMAIN]', () => {
55103
});
56104

57105
describe('getAll()', () => {
58-
it('should return all value from the store', done => {
106+
it('should return all values from the store.', done => {
59107
const a = 1;
60108
const b = 2;
61109
const sum = a + b;
@@ -80,6 +128,17 @@ describe('store: [adapter=DOMAIN]', () => {
80128

81129
globalStore.initialize(adapter)(callback);
82130
});
131+
132+
it('should return null if invoked under active domain w/o proper store initialization.', done => {
133+
const d = domain.create();
134+
135+
d.run(() => {
136+
// Ensure data in the existing domain is available at this point.
137+
expect(globalStore.getAll()).to.equal(null);
138+
139+
done();
140+
});
141+
});
83142
});
84143

85144
describe('getByKeys()', () => {
@@ -90,6 +149,7 @@ describe('store: [adapter=DOMAIN]', () => {
90149
it('should return `undefined` as the value for requested keys that were not set.', done => {
91150
const callback = () => {
92151
expect(globalStore.getByKeys(['foo', 'bar'])).to.deep.equal([undefined, undefined]);
152+
93153
done();
94154
};
95155

@@ -132,7 +192,7 @@ describe('store: [adapter=DOMAIN]', () => {
132192
});
133193

134194
describe('getId()', () => {
135-
it('should return unique value if store is initialized', done => {
195+
it('should return unique value if store is initialized.', done => {
136196
const callback = () => {
137197
expect(globalStore.getId()).to.be.an('string');
138198
expect(globalStore.getId()).to.not.equal(null);
@@ -153,7 +213,7 @@ describe('store: [adapter=DOMAIN]', () => {
153213
});
154214

155215
describe('find()', () => {
156-
it('should successfully return value in synchronous callback', done => {
216+
it('should successfully return value in synchronous callback.', done => {
157217
const callback = () => {
158218
first();
159219
second();
@@ -191,7 +251,7 @@ describe('store: [adapter=DOMAIN]', () => {
191251
globalStore.initialize(adapter)(callback);
192252
});
193253

194-
it('should successfully return value in asynchronous callback', done => {
254+
it('should successfully return value in asynchronous callback.', done => {
195255
const callback = () => {
196256
globalStore.set({ foo: 'bar' });
197257

@@ -219,7 +279,7 @@ describe('store: [adapter=DOMAIN]', () => {
219279
globalStore.initialize(adapter)(callback);
220280
});
221281

222-
it('should return null if store not initialized.', () => {
282+
it('should return null even if store not initialized.', () => {
223283
expect(globalStore.find('foo')).to.equal(null);
224284
});
225285

@@ -238,7 +298,22 @@ describe('store: [adapter=DOMAIN]', () => {
238298
expect(globalStore.set.bind(globalStore, {})).to.throw('Async store not initialized.');
239299
});
240300

241-
it('should set properties in the store', done => {
301+
it('should throw an error if invalid arguments are provided.', done => {
302+
const callback = () => {
303+
expect(globalStore.set.bind(globalStore, 5)).to.throw('Invalid arguments provided for asyncStore.set()');
304+
expect(globalStore.set.bind(globalStore, 'five')).to.throw('Invalid arguments provided for asyncStore.set()');
305+
expect(globalStore.set.bind(globalStore, null)).to.throw('Invalid arguments provided for asyncStore.set()');
306+
expect(globalStore.set.bind(globalStore, undefined)).to.throw(
307+
'Invalid arguments provided for asyncStore.set()'
308+
);
309+
310+
done();
311+
};
312+
313+
globalStore.initialize(adapter)(callback);
314+
});
315+
316+
it('should set properties in the store.', done => {
242317
const callback = () => {
243318
globalStore.set({ foo: 'Hello', bar: 'World' });
244319
second();

test/general.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'mocha';
2+
import { expect } from 'chai';
3+
4+
import * as globalStore from '../src';
5+
import AsyncStoreAdapter from '../src/AsyncStoreAdapter';
6+
7+
describe('store: General', () => {
8+
beforeEach(() => {
9+
Object.assign(process, { domain: undefined });
10+
});
11+
12+
it('should throw an error while initializing if unknown adapter passed.', () => {
13+
expect(globalStore.initialize.bind(globalStore, 'UNKNOWN_ADAPTER' as AsyncStoreAdapter)).to.throw(
14+
'Invalid async store adapter provided UNKNOWN_ADAPTER.'
15+
);
16+
});
17+
18+
it('should throw an error when initialized multiple times.', () => {
19+
const callback = () => {
20+
// Try re-initializing the store and it should throw an error.
21+
expect(globalStore.initialize.bind(globalStore, AsyncStoreAdapter.DOMAIN)).to.throw(
22+
'Async store already initialized, cannot re-initialize again.'
23+
);
24+
};
25+
26+
globalStore.initialize(AsyncStoreAdapter.DOMAIN)(callback);
27+
});
28+
});

0 commit comments

Comments
 (0)