Skip to content

Commit a624364

Browse files
authored
feat: useList allow pushing multiple items (#621)
1 parent a38f026 commit a624364

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/__tests__/useList.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ it('should push duplicated element at the end of the list', () => {
104104
expect(result.current[0]).not.toBe(initList); // checking immutability
105105
});
106106

107+
it('should push multiple elements at the end of the list', () => {
108+
const initList = [1, 2, 3];
109+
const { result } = setUp(initList);
110+
const [, utils] = result.current;
111+
112+
act(() => {
113+
utils.push(4, 5, 6);
114+
});
115+
116+
expect(result.current[0]).toEqual([1, 2, 3, 4, 5, 6]);
117+
expect(result.current[0]).not.toBe(initList); // checking immutability
118+
});
119+
107120
it('should filter current list by provided function', () => {
108121
const initList = [1, -1, 2, -2, 3, -3];
109122
const { result } = setUp(initList);

src/useList.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface Actions<T> {
55
clear: () => void;
66
updateAt: (index: number, item: T) => void;
77
remove: (index: number) => void;
8-
push: (item: T) => void;
8+
push: (...items: T[]) => void;
99
filter: (fn: (value: T) => boolean) => void;
1010
sort: (fn?: (a: T, b: T) => number) => void;
1111
reset: () => void;
@@ -21,7 +21,7 @@ const useList = <T>(initialList: T[] = []): [T[], Actions<T>] => {
2121
updateAt: (index, entry) =>
2222
set(currentList => [...currentList.slice(0, index), entry, ...currentList.slice(index + 1)]),
2323
remove: index => set(currentList => [...currentList.slice(0, index), ...currentList.slice(index + 1)]),
24-
push: entry => set(currentList => [...currentList, entry]),
24+
push: (...entry) => set(currentList => [...currentList, ...entry]),
2525
filter: fn => set(currentList => currentList.filter(fn)),
2626
sort: (fn?) => set(currentList => [...currentList].sort(fn)),
2727
reset: () => set([...initialList]),

0 commit comments

Comments
 (0)