1
1
var expect = require ( "expect.js" ) ,
2
2
__with__ = require ( "../lib/__with__.js" ) ,
3
3
__set__ = require ( "../lib/__set__.js" ) ,
4
- vm = require ( "vm" ) ,
5
- expectReferenceError = expectError ( ReferenceError ) ,
6
- expectTypeError = expectError ( TypeError ) ;
4
+ vm = require ( "vm" ) ;
7
5
8
6
function expectError ( ErrConstructor ) {
9
7
return function expectReferenceError ( err ) {
@@ -12,7 +10,8 @@ function expectError(ErrConstructor) {
12
10
}
13
11
14
12
describe ( "__with__" , function ( ) {
15
- var moduleFake ;
13
+ var moduleFake ,
14
+ newObj ;
16
15
17
16
beforeEach ( function ( ) {
18
17
moduleFake = {
@@ -23,8 +22,10 @@ describe("__with__", function() {
23
22
myReference :{ } // copy by reference
24
23
} ;
25
24
26
- //__with__ requires __set__ to be in scope
25
+ newObj = { hello :"hello" } ;
26
+
27
27
vm . runInNewContext (
28
+ //__with__ requires __set__ to be present on module.exports
28
29
"module.exports.__set__ = " + __set__ . toString ( ) + "; " +
29
30
"__with__ = " + __with__ . toString ( ) + "; " +
30
31
"getValue = function () { return myValue; }; " +
@@ -33,53 +34,57 @@ describe("__with__", function() {
33
34
) ;
34
35
} ) ;
35
36
36
- it ( "should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end" , function ( ) {
37
- var newObj = { hello :"hello" } ;
37
+ it ( "should return a function" , function ( ) {
38
+ expect ( moduleFake . __with__ ( {
39
+ myValue :2 ,
40
+ myReference :newObj
41
+ } ) ) . to . be . a ( "function" ) ;
42
+ } ) ;
38
43
44
+ it ( "should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end" , function ( ) {
39
45
expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
40
46
expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
41
47
42
48
moduleFake . __with__ ( {
43
49
myValue :2 ,
44
50
myReference :newObj
45
- } ) ( function ( ) {
46
- // changes will be visible from within this callback function
47
- expect ( moduleFake . getValue ( ) ) . to . be ( 2 ) ;
48
- expect ( moduleFake . getReference ( ) ) . to . be ( newObj ) ;
51
+ } ) ( function ( ) {
52
+ // changes will be visible from within this callback function
53
+ expect ( moduleFake . getValue ( ) ) . to . be ( 2 ) ;
54
+ expect ( moduleFake . getReference ( ) ) . to . be ( newObj ) ;
49
55
} ) ;
50
56
51
- //undo will automatically get called for you after returning from your callback function
57
+ // undo will automatically get called for you after returning from your callback function
52
58
expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
53
59
expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
54
60
} ) ;
55
61
56
62
it ( "should still revert values if the callback throws an exception" , function ( ) {
57
- var newObj = { hello :"hello" } ;
58
- function withError ( ) {
59
- moduleFake . __with__ ( {
60
- myValue :2 ,
61
- myReference :newObj
62
- } ) ( function ( ) {
63
- throw new Error ( "something went wrong..." ) ;
64
- } ) ;
65
- }
66
- expect ( withError ) . to . throwError ( ) ;
63
+ expect ( function withError ( ) {
64
+ moduleFake . __with__ ( {
65
+ myValue :2 ,
66
+ myReference :newObj
67
+ } ) ( function ( ) {
68
+ throw new Error ( "something went wrong..." ) ;
69
+ } ) ;
70
+ } ) . to . throwError ( ) ;
67
71
expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
68
72
expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
69
73
} ) ;
70
74
71
75
it ( "should throw an error if something other than a function is passed as the callback" , function ( ) {
72
- var newObj = { hello :"hello" } ,
73
- withFunction = moduleFake . __with__ ( {
76
+ var withFunction = moduleFake . __with__ ( {
74
77
myValue :2 ,
75
78
myReference :newObj
76
79
} ) ;
77
- callWithFunction = function ( ) {
78
- var args = arguments ;
79
- return function ( ) {
80
+
81
+ function callWithFunction ( ) {
82
+ var args = arguments ;
83
+
84
+ return function ( ) {
80
85
withFunction . apply ( null , args ) ;
81
- } ;
82
86
} ;
87
+ }
83
88
84
89
expect ( callWithFunction ( 1 ) ) . to . throwError ( ) ;
85
90
expect ( callWithFunction ( "a string" ) ) . to . throwError ( ) ;