1+ 'use strict' ;
2+
3+ describe ( 'Unit Test Context MiddleWare' , function ( ) {
4+ var expressContext = require ( '../../lib/index' ) ;
5+
6+ var uniqueId1 , uniqueId2 ;
7+
8+ it ( 'Should verify all API\'s' , function ( ) {
9+ expect ( typeof expressContext . sessionContext ) . toEqual ( 'function' ) ;
10+ expect ( typeof expressContext . addContextManipulator ) . toEqual ( 'function' ) ;
11+ } ) ;
12+
13+ it ( 'Should successfully add context manipulator for namespaces' , function ( ) {
14+ uniqueId1 = expressContext . addContextManipulator ( function ( contextManipulator , next ) {
15+ contextManipulator . addData ( {
16+ UNIQUE1 : 'UNIQUE1'
17+ } ) ;
18+
19+ next ( ) ;
20+ } ) ;
21+
22+ uniqueId2 = expressContext . addContextManipulator ( function ( contextManipulator , next ) {
23+ contextManipulator . addData ( {
24+ UNIQUE2 : 'UNIQUE2'
25+ } ) ;
26+
27+ next ( ) ;
28+ } ) ;
29+
30+ expressContext . addContextManipulator ( function ( contextManipulator , next ) {
31+ contextManipulator . addGlobal ( 'test' , {
32+ GLOBAL : 'GLOBAL'
33+ } ) ;
34+
35+ next ( ) ;
36+ } ) ;
37+
38+ expect ( uniqueId1 ) . toEqual ( jasmine . any ( Number ) ) ;
39+ expect ( uniqueId1 ) . toBeGreaterThan ( - 1 ) ;
40+ expect ( uniqueId2 ) . toEqual ( jasmine . any ( Number ) ) ;
41+ expect ( uniqueId2 ) . toBeGreaterThan ( - 1 ) ;
42+ } ) ;
43+
44+ it ( 'Should successfully get the context on the request' , function ( done ) {
45+ var req = { } ;
46+
47+ expressContext . sessionContext ( req , { } , function ( ) {
48+ expect ( typeof req . getContext ) . toEqual ( 'function' ) ;
49+
50+ done ( ) ;
51+ } ) ;
52+ } ) ;
53+
54+ it ( 'Should successfully get the global data from the context' , function ( done ) {
55+ var req = { } ;
56+
57+ expressContext . sessionContext ( req , { } , function ( ) {
58+ expect ( req . getContext ( ) . getData ( uniqueId1 ) ) . toEqual ( {
59+ UNIQUE1 : 'UNIQUE1'
60+ } ) ;
61+
62+ expect ( req . getContext ( ) . getData ( uniqueId2 ) ) . toEqual ( {
63+ UNIQUE2 : 'UNIQUE2'
64+ } ) ;
65+
66+ expect ( req . getContext ( ) . getGlobal ( ) . test ) . toEqual ( {
67+ GLOBAL : 'GLOBAL'
68+ } ) ;
69+
70+ done ( ) ;
71+ } ) ;
72+ } ) ;
73+
74+ it ( 'should successfully get Request from the ContextManipulator' , function ( done ) {
75+ var req = {
76+ TEST : 'TEST'
77+ } ;
78+
79+ expressContext . addContextManipulator ( function ( contextManipulator , next ) {
80+ expect ( typeof contextManipulator . getRequest ) . toEqual ( 'function' ) ;
81+ expect ( contextManipulator . getRequest ( ) . TEST ) . toEqual ( 'TEST' ) ;
82+
83+ next ( ) ;
84+ } ) ;
85+
86+ expressContext . sessionContext ( req , { } , function ( ) {
87+ done ( ) ;
88+ } ) ;
89+ } ) ;
90+ } ) ;
0 commit comments