From a624364d8bee2a65ef9094dfa2413811b5602bf4 Mon Sep 17 00:00:00 2001 From: Ward Date: Mon, 14 Oct 2019 07:38:04 +1100 Subject: [PATCH] feat: useList allow pushing multiple items (#621) --- src/__tests__/useList.test.ts | 13 +++++++++++++ src/useList.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/__tests__/useList.test.ts b/src/__tests__/useList.test.ts index f8dc9584e9..47b5beb73b 100644 --- a/src/__tests__/useList.test.ts +++ b/src/__tests__/useList.test.ts @@ -104,6 +104,19 @@ it('should push duplicated element at the end of the list', () => { expect(result.current[0]).not.toBe(initList); // checking immutability }); +it('should push multiple elements at the end of the list', () => { + const initList = [1, 2, 3]; + const { result } = setUp(initList); + const [, utils] = result.current; + + act(() => { + utils.push(4, 5, 6); + }); + + expect(result.current[0]).toEqual([1, 2, 3, 4, 5, 6]); + expect(result.current[0]).not.toBe(initList); // checking immutability +}); + it('should filter current list by provided function', () => { const initList = [1, -1, 2, -2, 3, -3]; const { result } = setUp(initList); diff --git a/src/useList.ts b/src/useList.ts index 500008180e..eecdc5073c 100644 --- a/src/useList.ts +++ b/src/useList.ts @@ -5,7 +5,7 @@ export interface Actions { clear: () => void; updateAt: (index: number, item: T) => void; remove: (index: number) => void; - push: (item: T) => void; + push: (...items: T[]) => void; filter: (fn: (value: T) => boolean) => void; sort: (fn?: (a: T, b: T) => number) => void; reset: () => void; @@ -21,7 +21,7 @@ const useList = (initialList: T[] = []): [T[], Actions] => { updateAt: (index, entry) => set(currentList => [...currentList.slice(0, index), entry, ...currentList.slice(index + 1)]), remove: index => set(currentList => [...currentList.slice(0, index), ...currentList.slice(index + 1)]), - push: entry => set(currentList => [...currentList, entry]), + push: (...entry) => set(currentList => [...currentList, ...entry]), filter: fn => set(currentList => currentList.filter(fn)), sort: (fn?) => set(currentList => [...currentList].sort(fn)), reset: () => set([...initialList]),