summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/packages/puppeteer-core/src/common/EventEmitter.test.ts
blob: cf05ef670010a12506fd28b4efe46628da209c84 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
 * @license
 * Copyright 2023 Google Inc.
 * SPDX-License-Identifier: Apache-2.0
 */

import {describe, it, beforeEach} from 'node:test';

import expect from 'expect';
import sinon from 'sinon';

import {EventEmitter} from './EventEmitter.js';

describe('EventEmitter', () => {
  let emitter: EventEmitter<Record<string, unknown>>;

  beforeEach(() => {
    emitter = new EventEmitter();
  });

  describe('on', () => {
    const onTests = (methodName: 'on' | 'addListener'): void => {
      it(`${methodName}: adds an event listener that is fired when the event is emitted`, () => {
        const listener = sinon.spy();
        emitter[methodName]('foo', listener);
        emitter.emit('foo', undefined);
        expect(listener.callCount).toEqual(1);
      });

      it(`${methodName} sends the event data to the handler`, () => {
        const listener = sinon.spy();
        const data = {};
        emitter[methodName]('foo', listener);
        emitter.emit('foo', data);
        expect(listener.callCount).toEqual(1);
        expect(listener.firstCall.args[0]).toBe(data);
      });

      it(`${methodName}: supports chaining`, () => {
        const listener = sinon.spy();
        const returnValue = emitter[methodName]('foo', listener);
        expect(returnValue).toBe(emitter);
      });
    };
    onTests('on');
    // we support addListener for legacy reasons
    onTests('addListener');
  });

  describe('off', () => {
    const offTests = (methodName: 'off' | 'removeListener'): void => {
      it(`${methodName}: removes the listener so it is no longer called`, () => {
        const listener = sinon.spy();
        emitter.on('foo', listener);
        emitter.emit('foo', undefined);
        expect(listener.callCount).toEqual(1);
        emitter.off('foo', listener);
        emitter.emit('foo', undefined);
        expect(listener.callCount).toEqual(1);
      });

      it(`${methodName}: supports chaining`, () => {
        const listener = sinon.spy();
        emitter.on('foo', listener);
        const returnValue = emitter.off('foo', listener);
        expect(returnValue).toBe(emitter);
      });
    };
    offTests('off');
    // we support removeListener for legacy reasons
    offTests('removeListener');
  });

  describe('once', () => {
    it('only calls the listener once and then removes it', () => {
      const listener = sinon.spy();
      emitter.once('foo', listener);
      emitter.emit('foo', undefined);
      expect(listener.callCount).toEqual(1);
      emitter.emit('foo', undefined);
      expect(listener.callCount).toEqual(1);
    });

    it('supports chaining', () => {
      const listener = sinon.spy();
      const returnValue = emitter.once('foo', listener);
      expect(returnValue).toBe(emitter);
    });
  });

  describe('emit', () => {
    it('calls all the listeners for an event', () => {
      const listener1 = sinon.spy();
      const listener2 = sinon.spy();
      const listener3 = sinon.spy();
      emitter.on('foo', listener1).on('foo', listener2).on('bar', listener3);

      emitter.emit('foo', undefined);

      expect(listener1.callCount).toEqual(1);
      expect(listener2.callCount).toEqual(1);
      expect(listener3.callCount).toEqual(0);
    });

    it('passes data through to the listener', () => {
      const listener = sinon.spy();
      emitter.on('foo', listener);
      const data = {};

      emitter.emit('foo', data);
      expect(listener.callCount).toEqual(1);
      expect(listener.firstCall.args[0]).toBe(data);
    });

    it('returns true if the event has listeners', () => {
      const listener = sinon.spy();
      emitter.on('foo', listener);
      expect(emitter.emit('foo', undefined)).toBe(true);
    });

    it('returns false if the event has listeners', () => {
      const listener = sinon.spy();
      emitter.on('foo', listener);
      expect(emitter.emit('notFoo', undefined)).toBe(false);
    });
  });

  describe('listenerCount', () => {
    it('returns the number of listeners for the given event', () => {
      emitter.on('foo', () => {});
      emitter.on('foo', () => {});
      emitter.on('bar', () => {});
      expect(emitter.listenerCount('foo')).toEqual(2);
      expect(emitter.listenerCount('bar')).toEqual(1);
      expect(emitter.listenerCount('noListeners')).toEqual(0);
    });
  });

  describe('removeAllListeners', () => {
    it('removes every listener from all events by default', () => {
      emitter.on('foo', () => {}).on('bar', () => {});

      emitter.removeAllListeners();
      expect(emitter.emit('foo', undefined)).toBe(false);
      expect(emitter.emit('bar', undefined)).toBe(false);
    });

    it('returns the emitter for chaining', () => {
      expect(emitter.removeAllListeners()).toBe(emitter);
    });

    it('can filter to remove only listeners for a given event name', () => {
      emitter
        .on('foo', () => {})
        .on('bar', () => {})
        .on('bar', () => {});

      emitter.removeAllListeners('bar');
      expect(emitter.emit('foo', undefined)).toBe(true);
      expect(emitter.emit('bar', undefined)).toBe(false);
    });
  });

  describe('dispose', () => {
    it('should dispose higher order emitters properly', () => {
      let values = '';
      emitter.on('foo', () => {
        values += '1';
      });
      const higherOrderEmitter = new EventEmitter(emitter);

      higherOrderEmitter.on('foo', () => {
        values += '2';
      });
      higherOrderEmitter.emit('foo', undefined);

      expect(values).toMatch('12');

      higherOrderEmitter.off('foo');
      higherOrderEmitter.emit('foo', undefined);

      expect(values).toMatch('121');
    });
  });
});