Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75ebdd4

Browse files
committed
Fix issues with reverting nested properties
Fixesjhnns#39
1 parent972972e commit75ebdd4

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

‎lib/__set__.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,36 @@ function __set__() {
1313
arguments.varName=arguments[0];
1414
arguments.varValue=arguments[1];
1515
arguments.src="";
16-
arguments.snapshot={};
16+
arguments.revertArgs=[];
1717

1818
if(typeofarguments[0]==="object"&&arguments.length===1){
1919
arguments.env=arguments.varName;
2020
if(!arguments.env||Array.isArray(arguments.env)){
2121
thrownewTypeError("__set__ expects an object as env");
2222
}
23+
arguments.revertArgs[0]={};
2324
for(arguments.varNameinarguments.env){
2425
if(arguments.env.hasOwnProperty(arguments.varName)){
2526
arguments.varValue=arguments.env[arguments.varName];
2627
arguments.src+=arguments.varName+" = arguments.env."+arguments.varName+"; ";
27-
arguments.snapshot[arguments.varName]=eval(arguments.varName);
28+
arguments.revertArgs[0][arguments.varName]=eval(arguments.varName);
2829
}
2930
}
3031
}elseif(typeofarguments.varName==="string"&&arguments.length===2){
3132
if(!arguments.varName){
3233
thrownewTypeError("__set__ expects a non-empty string as a variable name");
3334
}
3435
arguments.src=arguments.varName+" = arguments.varValue;";
35-
arguments.snapshot[arguments.varName]=eval(arguments.varName);
36+
arguments.revertArgs=[arguments.varName,eval(arguments.varName)];
3637
}else{
3738
thrownewTypeError("__set__ expects an environment object or a non-empty string as a variable name");
3839
}
3940

4041
eval(arguments.src);
4142

42-
returnfunction(snapshot){
43-
module.exports.__set__(snapshot);
44-
}.bind(null,arguments.snapshot);
43+
returnfunction(revertArgs){
44+
__set__.apply(null,revertArgs);
45+
}.bind(null,arguments.revertArgs);
4546
}
4647

4748
module.exports=__set__;

‎test/testModules/sharedTestCases.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,23 @@ describe("rewire " + (typeof testEnv === "undefined"? "(node)": "(" + testEnv +
267267
}).to.throwException(checkForTypeError);
268268
});
269269

270-
});
270+
it("should also revert nested changes (with dot notation)",function(){
271+
varrewiredModuleA=rewire("./moduleA.js"),
272+
revert;
273+
274+
revert=rewiredModuleA.__set__("myObj.test",true);
275+
expect(rewiredModuleA.getMyObj()).to.eql({
276+
test:true
277+
});
278+
revert();
279+
// This test also demonstrates a known drawback of the current implementation
280+
// If the value doesn't exist at the time it is about to be set, it will be
281+
// reverted to undefined instead deleting it from the object
282+
// However, this is probably not a real world use-case because why would you
283+
// want to mock something when it is not set.
284+
expect(rewiredModuleA.getMyObj()).to.eql({
285+
test:undefined
286+
});
287+
});
288+
289+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp