From 07c21083d61ff7784b15158430056fa843cdb635 Mon Sep 17 00:00:00 2001 From: Oleg Maslov Date: Thu, 17 Dec 2020 11:36:00 +0300 Subject: [PATCH] Add missing LinkedList tests (#151) Co-authored-by: Oleksii Trekhleb --- .../linked-list/__test__/LinkedList.test.js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/data-structures/linked-list/__test__/LinkedList.test.js b/src/data-structures/linked-list/__test__/LinkedList.test.js index f80187a7..bce06f72 100644 --- a/src/data-structures/linked-list/__test__/LinkedList.test.js +++ b/src/data-structures/linked-list/__test__/LinkedList.test.js @@ -218,6 +218,29 @@ describe('LinkedList', () => { expect(linkedList.find({ value: 2, customValue: 'test5' })).toBeNull(); }); + it('should find preferring callback over compare function', () => { + const greaterThan = (value, compareTo) => (value > compareTo ? 0 : 1); + + const linkedList = new LinkedList(greaterThan); + linkedList.fromArray([1, 2, 3, 4, 5]); + + let node = linkedList.find({ value: 3 }); + expect(node.value).toBe(4); + + node = linkedList.find({ callback: value => value < 3 }); + expect(node.value).toBe(1); + }); + + it('should convert to array', () => { + const linkedList = new LinkedList(); + + linkedList.append(1); + linkedList.append(2); + linkedList.append(3); + + expect(linkedList.toArray().join(',')).toBe('1,2,3'); + }); + it('should reverse linked list', () => { const linkedList = new LinkedList();