UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

46 lines (45 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntitySpy = void 0; const constants_js_1 = require("../../../entity/constants.js"); const index_js_1 = require("../../../entity/index.js"); const actionInspector_js_1 = require("./actionInspector.js"); const actionStub_js_1 = require("./actionStub.js"); const constants_js_2 = require("./constants.js"); class EntitySpy extends index_js_1.EntityAction { constructor(entity) { super(entity); this[constants_js_2.$mocks] = {}; this[constants_js_2.$sentActions] = {}; entity[constants_js_1.$interceptor] = (action) => { const actionName = action.constructor.actionName; const sentArgs = action[constants_js_1.$sentArgs](); const actionSentArgs = this[constants_js_2.$sentActions][actionName]; if (actionSentArgs !== undefined) { actionSentArgs.push(sentArgs); } else { this[constants_js_2.$sentActions][actionName] = [sentArgs]; } const actionMock = this[constants_js_2.$mocks][actionName]; if (actionMock !== undefined) { return actionMock(...sentArgs); } }; } reset() { this[constants_js_2.$mocks] = {}; this[constants_js_2.$sentActions] = {}; return this; } on(Action) { return new actionStub_js_1.EntityActionStub(this, Action); } sent(Action) { return new actionInspector_js_1.EntityActionInspector(this, Action); } restore() { delete this.entity[constants_js_1.$interceptor]; } } exports.EntitySpy = EntitySpy;