Created
July 21, 2021 05:30
-
-
Save koresar/21bd323148b2645d1d91601d8e2a8361 to your computer and use it in GitHub Desktop.
getters-setters test
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
const test = require("tape"); | |
const stampit = require("../src/stampit"); | |
function hasStringAccessor(name) { | |
let storedValue; | |
return stampit({ | |
props: { | |
get [name]() { | |
return storedValue; | |
}, | |
set [name](value) { | |
if (!["undefined", "string"].includes(typeof value)) return; | |
storedValue = value; | |
} | |
}, | |
init(opts) { | |
this[name] = opts[name]; | |
} | |
}); | |
} | |
test(".create()", (t) => { | |
const TestAccessors = stampit( | |
hasStringAccessor("one"), | |
{ name: "TestAccessors" } | |
); | |
const instance1 = TestAccessors({ one: "mango" }); | |
t.equal(instance1.one, "mango"); | |
const instance2 = TestAccessors({ one: "banana" }); | |
t.equal(instance2.one, "banana"); | |
// check instance1 again | |
t.equal(instance1.one, "mango"); | |
t.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment