11import 'mocha' ;
22import { expect } from 'chai' ;
33import * as domain from 'domain' ;
4+ import { EventEmitter } from 'events' ;
45
56import * as globalStore from '../src' ;
67import { 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 ( ) ;
0 commit comments