dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
42 lines (41 loc) • 1.37 kB
JavaScript
import { $interceptor, $sentArgs } from '../../../table/constants.js';
import { TableAction } from '../../../table/index.js';
import { TableActionInspector } from './actionInspector.js';
import { TableActionStub } from './actionStub.js';
import { $mocks, $sentActions } from './constants.js';
export class TableSpy extends TableAction {
constructor(table) {
super(table);
this[$mocks] = {};
this[$sentActions] = {};
table[$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 TableActionStub(this, Action);
}
sent(Action) {
return new TableActionInspector(this, Action);
}
restore() {
delete this.table[$interceptor];
}
}