1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/**
* @license
* Copyright 2023 Google Inc.
* SPDX-License-Identifier: Apache-2.0
*/
import {describe, it} from 'node:test';
import expect from 'expect';
import sinon from 'sinon';
import {invokeAtMostOnceForArguments} from './decorators.js';
describe('decorators', function () {
describe('invokeAtMostOnceForArguments', () => {
it('should delegate calls', () => {
const spy = sinon.spy();
class Test {
@invokeAtMostOnceForArguments
test(obj1: object, obj2: object) {
spy(obj1, obj2);
}
}
const t = new Test();
expect(spy.callCount).toBe(0);
const obj1 = {};
const obj2 = {};
t.test(obj1, obj2);
expect(spy.callCount).toBe(1);
});
it('should prevent repeated calls', () => {
const spy = sinon.spy();
class Test {
@invokeAtMostOnceForArguments
test(obj1: object, obj2: object) {
spy(obj1, obj2);
}
}
const t = new Test();
expect(spy.callCount).toBe(0);
const obj1 = {};
const obj2 = {};
t.test(obj1, obj2);
expect(spy.callCount).toBe(1);
expect(spy.lastCall.calledWith(obj1, obj2)).toBeTruthy();
t.test(obj1, obj2);
expect(spy.callCount).toBe(1);
expect(spy.lastCall.calledWith(obj1, obj2)).toBeTruthy();
const obj3 = {};
t.test(obj1, obj3);
expect(spy.callCount).toBe(2);
expect(spy.lastCall.calledWith(obj1, obj3)).toBeTruthy();
});
it('should throw an error for dynamic argumetns', () => {
class Test {
@invokeAtMostOnceForArguments
test(..._args: unknown[]) {}
}
const t = new Test();
t.test({});
expect(() => {
t.test({}, {});
}).toThrow();
});
it('should throw an error for non object arguments', () => {
class Test {
@invokeAtMostOnceForArguments
test(..._args: unknown[]) {}
}
const t = new Test();
expect(() => {
t.test(1);
}).toThrow();
});
});
});
|