From d61da93878159d300f8b199cf01d240dd4bf429a Mon Sep 17 00:00:00 2001 From: Jeffrey Berry Date: Thu, 25 Apr 2019 04:12:25 -0700 Subject: [PATCH] test(accumulate): add test suite for accumulate function (#15159) * refactor(typo): remove typo 'be' * test(accumulate): add test suite for accumulate function --- .../__tests__/accumulate-test.internal.js | 49 +++++++++++++++++++ packages/events/accumulate.js | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 packages/events/__tests__/accumulate-test.internal.js diff --git a/packages/events/__tests__/accumulate-test.internal.js b/packages/events/__tests__/accumulate-test.internal.js new file mode 100644 index 0000000000000..b5efd6bfa171a --- /dev/null +++ b/packages/events/__tests__/accumulate-test.internal.js @@ -0,0 +1,49 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @emails react-core + */ + +'use strict'; + +let accumulate; + +describe('accumulate', () => { + beforeEach(() => { + accumulate = require('events/accumulate').default; + }); + + it('throws if the second item is null', () => { + expect(function() { + accumulate([], null); + }).toThrowError( + 'accumulate(...): Accumulated items must not be null or undefined.', + ); + }); + + it('return second item if first item is null', () => { + const a = []; + expect(accumulate(null, a)).toBe(a); + }); + + it('return concatenation of items if first item is an array', () => { + const a = ['hello']; + const b = 'world'; + expect(accumulate(a, b)).toEqual(['hello', 'world']); + }); + + it('return concatenation of items if second item is an array', () => { + const a = 'hello'; + const b = ['world']; + expect(accumulate(a, b)).toEqual(['hello', 'world']); + }); + + it('return an array containing both items if neither item is an array', () => { + const a = 'hello'; + const b = 'world'; + expect(accumulate(a, b)).toEqual(['hello', 'world']); + }); +}); diff --git a/packages/events/accumulate.js b/packages/events/accumulate.js index e1cf1187b9b29..6fcf9f1fd80c0 100644 --- a/packages/events/accumulate.js +++ b/packages/events/accumulate.js @@ -22,7 +22,7 @@ function accumulate( ): T | Array { invariant( next != null, - 'accumulate(...): Accumulated items must be not be null or undefined.', + 'accumulate(...): Accumulated items must not be null or undefined.', ); if (current == null) {