UNPKG

decorator-cache-getter

Version:
72 lines 2.71 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); var index_1 = require("./index"); describe("decorator cache getter", function () { it("should cache result of getter", function () { var Test = /** @class */ (function () { function Test() { this.i = 0; } Object.defineProperty(Test.prototype, "incr", { get: function () { return ++this.i; }, enumerable: true, configurable: true }); __decorate([ index_1.cache ], Test.prototype, "incr", null); return Test; }()); var test = new Test(); expect(test.incr).toEqual(1); expect(test.incr).toEqual(1); expect(test.incr).toEqual(1); expect(test.i).toEqual(1); }); it("should cache result of static getter", function () { var Test = /** @class */ (function () { function Test() { } Object.defineProperty(Test, "incr", { get: function () { return ++this.i; }, enumerable: true, configurable: true }); Test.i = 0; __decorate([ index_1.cache ], Test, "incr", null); return Test; }()); expect(Test.incr).toEqual(1); expect(Test.incr).toEqual(1); expect(Test.incr).toEqual(1); expect(Test.i).toEqual(1); }); it("should raise when decorating a non-getter", function () { expect(function () { var Test = /** @class */ (function () { function Test() { } Test.prototype.method = function () { /* Empty */ }; __decorate([ index_1.cache ], Test.prototype, "method", null); return Test; }()); }).toThrowError(TypeError); }); }); //# sourceMappingURL=index.spec.js.map