summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/packages/puppeteer-core/src/util/decorators.test.ts
blob: bc476b0153eac27220342afe4d8e4d2183a317de (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * @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 {EventEmitter} from '../common/EventEmitter.js';

import {bubble, 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();
    });
  });

  describe('bubble', () => {
    it('should work', () => {
      class Test extends EventEmitter<any> {
        @bubble()
        accessor field = new EventEmitter();
      }

      const t = new Test();
      let a = false;
      t.on('a', (value: boolean) => {
        a = value;
      });

      t.field.emit('a', true);
      expect(a).toBeTruthy();

      // Set a new emitter.
      t.field = new EventEmitter();
      a = false;

      t.field.emit('a', true);
      expect(a).toBeTruthy();
    });

    it('should not bubble down', () => {
      class Test extends EventEmitter<any> {
        @bubble()
        accessor field = new EventEmitter<any>();
      }

      const t = new Test();
      let a = false;
      t.field.on('a', (value: boolean) => {
        a = value;
      });

      t.emit('a', true);
      expect(a).toBeFalsy();

      t.field.emit('a', true);
      expect(a).toBeTruthy();
    });
  });
});