dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
42 lines (41 loc) • 1.38 kB
JavaScript
import { $interceptor, $sentArgs } from '../../../entity/constants.js';
import { EntityAction } from '../../../entity/index.js';
import { EntityActionInspector } from './actionInspector.js';
import { EntityActionStub } from './actionStub.js';
import { $mocks, $sentActions } from './constants.js';
export class EntitySpy extends EntityAction {
constructor(entity) {
super(entity);
this[$mocks] = {};
this[$sentActions] = {};
entity[$interceptor] = (action) => {
const actionName = action.constructor.actionName;
const sentArgs = action[$sentArgs]();
const actionSentArgs = this[$sentActions][actionName];
if (actionSentArgs !== undefined) {
actionSentArgs.push(sentArgs);
}
else {
this[$sentActions][actionName] = [sentArgs];
}
const actionMock = this[$mocks][actionName];
if (actionMock !== undefined) {
return actionMock(...sentArgs);
}
};
}
reset() {
this[$mocks] = {};
this[$sentActions] = {};
return this;
}
on(Action) {
return new EntityActionStub(this, Action);
}
sent(Action) {
return new EntityActionInspector(this, Action);
}
restore() {
delete this.entity[$interceptor];
}
}