forked from jsdom/jsdom
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide methods to define ES5 getters and setters
To allow jsdom to run in environments that don't support `__defineGetter__` and `__defineSetter__`, implement equivalents in terms of ES5's `Object.defineProperty`.
- Loading branch information
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
"use strict"; | ||
|
||
var utils = require("../../lib/jsdom/utils"); | ||
|
||
exports["defineSetter defines a setter"] = function (t) { | ||
var o = {}; | ||
var called = false; | ||
var expected = 'bar'; | ||
var actual; | ||
|
||
utils.defineSetter(o, 'foo', function (val) { | ||
called = true; | ||
actual = val; | ||
}); | ||
|
||
o.foo = expected; | ||
t.equal(called, true); | ||
t.equal(actual, expected); | ||
|
||
t.done(); | ||
}; | ||
|
||
exports["defineSetter replaces existing setters"] = function (t) { | ||
var o = {}; | ||
var originalCalled = false; | ||
var newCalled = false; | ||
|
||
utils.defineSetter(o, 'foo', function (val) { | ||
originalCalled = true; | ||
}); | ||
|
||
utils.defineSetter(o, 'foo', function (val) { | ||
newCalled = true; | ||
}); | ||
|
||
o.foo = true; | ||
t.equal(originalCalled, false); | ||
t.equal(newCalled, true); | ||
|
||
t.done(); | ||
}; | ||
|
||
exports["defineSetter does not remove existing getters"] = function (t) { | ||
var o = {}; | ||
var called = false; | ||
var expected = 'bar'; | ||
var actual; | ||
|
||
utils.defineGetter(o, 'foo', function () { | ||
called = true; | ||
return expected; | ||
}); | ||
|
||
utils.defineSetter(o, 'foo', function (val) { /* NOP */ }); | ||
|
||
actual = o.foo; | ||
t.equal(called, true); | ||
t.equal(actual, expected); | ||
|
||
t.done(); | ||
}; | ||
|
||
exports["defineGetter defines a getter"] = function (t) { | ||
var o = {}; | ||
var called = false; | ||
var expected = 'bar'; | ||
var actual; | ||
|
||
utils.defineGetter(o, 'foo', function () { | ||
called = true; | ||
return expected | ||
}); | ||
|
||
actual = o.foo; | ||
t.equal(called, true); | ||
t.equal(actual, expected); | ||
|
||
t.done(); | ||
}; | ||
|
||
exports["defineGetter replaces existing getters"] = function (t) { | ||
var o = {}; | ||
var originalCalled = false; | ||
var newCalled = false; | ||
|
||
utils.defineGetter(o, 'foo', function (val) { | ||
originalCalled = true; | ||
}); | ||
|
||
utils.defineGetter(o, 'foo', function (val) { | ||
newCalled = true; | ||
}); | ||
|
||
var actual = o.foo; | ||
t.equal(originalCalled, false); | ||
t.equal(newCalled, true); | ||
|
||
t.done(); | ||
}; | ||
|
||
exports["defineGetter does not remove existing setters"] = function (t) { | ||
var o = {}; | ||
var called = false; | ||
var expected = 'bar'; | ||
var actual; | ||
|
||
utils.defineSetter(o, 'foo', function (val) { | ||
called = true; | ||
actual = val; | ||
}); | ||
|
||
utils.defineGetter(o, 'foo', function () { /* NOP */ }); | ||
|
||
o.foo = expected; | ||
t.equal(called, true); | ||
t.equal(actual, expected); | ||
|
||
t.done(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters