11var expect = require ( "expect.js" ) ,
22 __with__ = require ( "../lib/__with__.js" ) ,
33 __set__ = require ( "../lib/__set__.js" ) ,
4- vm = require ( "vm" ) ,
5- expectReferenceError = expectError ( ReferenceError ) ,
6- expectTypeError = expectError ( TypeError ) ;
4+ vm = require ( "vm" ) ;
75
86function expectError ( ErrConstructor ) {
97 return function expectReferenceError ( err ) {
@@ -12,7 +10,8 @@ function expectError(ErrConstructor) {
1210}
1311
1412describe ( "__with__" , function ( ) {
15- var moduleFake ;
13+ var moduleFake ,
14+ newObj ;
1615
1716 beforeEach ( function ( ) {
1817 moduleFake = {
@@ -23,8 +22,10 @@ describe("__with__", function() {
2322 myReference : { } // copy by reference
2423 } ;
2524
26- //__with__ requires __set__ to be in scope
25+ newObj = { hello : "hello" } ;
26+
2727 vm . runInNewContext (
28+ //__with__ requires __set__ to be present on module.exports
2829 "module.exports.__set__ = " + __set__ . toString ( ) + "; " +
2930 "__with__ = " + __with__ . toString ( ) + "; " +
3031 "getValue = function () { return myValue; }; " +
@@ -33,53 +34,57 @@ describe("__with__", function() {
3334 ) ;
3435 } ) ;
3536
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+ } ) ;
3843
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 ( ) {
3945 expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
4046 expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
4147
4248 moduleFake . __with__ ( {
4349 myValue : 2 ,
4450 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 ) ;
4955 } ) ;
5056
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
5258 expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
5359 expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
5460 } ) ;
5561
5662 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 ( ) ;
6771 expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
6872 expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
6973 } ) ;
7074
7175 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__ ( {
7477 myValue : 2 ,
7578 myReference : newObj
7679 } ) ;
77- callWithFunction = function ( ) {
78- var args = arguments ;
79- return function ( ) {
80+
81+ function callWithFunction ( ) {
82+ var args = arguments ;
83+
84+ return function ( ) {
8085 withFunction . apply ( null , args ) ;
81- } ;
8286 } ;
87+ }
8388
8489 expect ( callWithFunction ( 1 ) ) . to . throwError ( ) ;
8590 expect ( callWithFunction ( "a string" ) ) . to . throwError ( ) ;
0 commit comments