source: trip-planner-front/node_modules/events/tests/once.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22var common = require('./common');
23var assert = require('assert');
24var EventEmitter = require('../');
25
26var e = new EventEmitter();
27
28e.once('hello', common.mustCall());
29
30e.emit('hello', 'a', 'b');
31e.emit('hello', 'a', 'b');
32e.emit('hello', 'a', 'b');
33e.emit('hello', 'a', 'b');
34
35function remove() {
36 assert.fail('once->foo should not be emitted');
37}
38
39e.once('foo', remove);
40e.removeListener('foo', remove);
41e.emit('foo');
42
43e.once('e', common.mustCall(function() {
44 e.emit('e');
45}));
46
47e.once('e', common.mustCall());
48
49e.emit('e');
50
51// Verify that the listener must be a function
52assert.throws(function() {
53 var ee = new EventEmitter();
54
55 ee.once('foo', null);
56}, /^TypeError: The "listener" argument must be of type Function. Received type object$/);
57
58{
59 // once() has different code paths based on the number of arguments being
60 // emitted. Verify that all of the cases are covered.
61 var maxArgs = 4;
62
63 for (var i = 0; i <= maxArgs; ++i) {
64 var ee = new EventEmitter();
65 var args = ['foo'];
66
67 for (var j = 0; j < i; ++j)
68 args.push(j);
69
70 ee.once('foo', common.mustCall(function() {
71 var params = Array.prototype.slice.call(arguments);
72 var restArgs = args.slice(1);
73 assert.ok(Array.isArray(params));
74 assert.strictEqual(params.length, restArgs.length);
75 for (var index = 0; index < params.length; index++) {
76 var param = params[index];
77 assert.strictEqual(param, restArgs[index]);
78 }
79 }));
80
81 EventEmitter.prototype.emit.apply(ee, args);
82 }
83}
Note: See TracBrowser for help on using the repository browser.