From 9f8e3d961d44fd7e6398ea5e24904a4ba0420a53 Mon Sep 17 00:00:00 2001 From: alin Date: Wed, 27 Mar 2024 14:17:47 +0100 Subject: [PATCH] Add tests for custom merge options and callback --- .../packages/support/arrays/merge.test.js | 57 ++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/tests/browser/packages/support/arrays/merge.test.js b/tests/browser/packages/support/arrays/merge.test.js index c25f5712..f20d304c 100644 --- a/tests/browser/packages/support/arrays/merge.test.js +++ b/tests/browser/packages/support/arrays/merge.test.js @@ -1,4 +1,8 @@ -import { ArrayMergeError, merge } from "@aedart/support/arrays"; +import { + ArrayMergeError, + merge, + Merger +} from "@aedart/support/arrays"; describe('@aedart/support/arrays', () => { describe('merge()', () => { @@ -60,5 +64,56 @@ describe('@aedart/support/arrays', () => { expect(callback) .toThrowError(ArrayMergeError); }); + + it('returns merger object when no args given', () => { + const merger = merge(); + + expect(merger) + .toBeInstanceOf(Merger); + }); + + it('can transfer functions', () => { + + const fnA = () => false; + const fnB = () => true; + + const a = [ fnA ]; + const b = [ fnB ]; + + // --------------------------------------------------------------- // + + const result = merge() + .using({ transferFunctions: true }) + .of(a, b); + + expect(result.length) + .withContext('Incorrect amount of elements in output') + .toBe(2); + + expect(result[0]) + .withContext('Function A not transferred') + .toBe(fnA); + + expect(result[1]) + .withContext('Function B not transferred') + .toBe(fnB); + }); + + it('can apply custom merge callback', () => { + + const a = [ 1, 2, 3 ]; + const b = [ 4, 5, 6 ]; + + // --------------------------------------------------------------- // + + const result = merge() + .using((element) => { + return element * 2; + }) + .of(a, b); + + expect(result) + .toEqual([ 2, 4, 6, 8, 10, 12 ]); + }); }); }); \ No newline at end of file