2020-07-03 21:39:25 +00:00
|
|
|
test("normal methods named get and set", () => {
|
2020-07-06 14:37:45 +00:00
|
|
|
let o = {
|
|
|
|
get() {
|
|
|
|
return 5;
|
|
|
|
},
|
|
|
|
set() {
|
|
|
|
return 10;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(o.get()).toBe(5);
|
|
|
|
expect(o.set()).toBe(10);
|
2020-07-03 21:39:25 +00:00
|
|
|
});
|
2020-05-22 00:28:28 +00:00
|
|
|
|
2020-07-03 21:39:25 +00:00
|
|
|
test("basic get and set", () => {
|
2020-07-06 14:37:45 +00:00
|
|
|
let o = {
|
|
|
|
get x() {
|
|
|
|
return 5;
|
|
|
|
},
|
|
|
|
set x(_) {},
|
|
|
|
};
|
|
|
|
expect(o.x).toBe(5);
|
|
|
|
o.x = 10;
|
|
|
|
expect(o.x).toBe(5);
|
2020-07-03 21:39:25 +00:00
|
|
|
});
|
2020-05-22 00:28:28 +00:00
|
|
|
|
2020-07-03 21:39:25 +00:00
|
|
|
test("get and set with 'this'", () => {
|
2020-07-06 14:37:45 +00:00
|
|
|
let o = {
|
|
|
|
get x() {
|
|
|
|
return this._x + 1;
|
|
|
|
},
|
|
|
|
set x(value) {
|
|
|
|
this._x = value + 1;
|
|
|
|
},
|
|
|
|
};
|
2020-05-22 00:28:28 +00:00
|
|
|
|
2020-07-06 14:37:45 +00:00
|
|
|
expect(o.x).toBeNaN();
|
|
|
|
o.x = 10;
|
|
|
|
expect(o.x).toBe(12);
|
|
|
|
o.x = 20;
|
|
|
|
expect(o.x).toBe(22);
|
2020-07-03 21:39:25 +00:00
|
|
|
});
|
2020-05-22 00:28:28 +00:00
|
|
|
|
2020-07-03 21:39:25 +00:00
|
|
|
test("multiple getters", () => {
|
2020-07-06 14:37:45 +00:00
|
|
|
let o = {
|
|
|
|
get x() {
|
|
|
|
return 5;
|
|
|
|
},
|
|
|
|
get x() {
|
|
|
|
return 10;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(o.x).toBe(10);
|
2020-07-03 21:39:25 +00:00
|
|
|
});
|
2020-05-22 00:28:28 +00:00
|
|
|
|
2020-07-03 21:39:25 +00:00
|
|
|
test("setter return value", () => {
|
2020-07-06 14:37:45 +00:00
|
|
|
o = {
|
|
|
|
set x(value) {
|
|
|
|
return 10;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect((o.x = 20)).toBe(20);
|
2020-07-03 21:39:25 +00:00
|
|
|
});
|