diff --git a/src/use-queue/use-queue.test.ts b/src/use-queue/use-queue.test.ts index 7477580..a68f357 100644 --- a/src/use-queue/use-queue.test.ts +++ b/src/use-queue/use-queue.test.ts @@ -1,14 +1,69 @@ -import { renderHook } from '@testing-library/react'; -import { expect, test } from 'vitest'; - +import { act, renderHook } from '@testing-library/react'; +import { describe, expect, it } from 'vitest'; import { useQueue } from './use-queue'; -test('should initiate queue with empty state', () => { - const { result } = renderHook(() => useQueue()); +describe('useQueue', () => { + it('should initialize with empty queues', () => { + const { result } = renderHook(() => useQueue()); + expect(result.current[0]).toEqual({ active: [], queue: [] }); + }); + + it('should initialize with provided values', () => { + const { result } = renderHook(() => useQueue([1, 2, 3], 2)); + expect(result.current[0]).toEqual({ active: [1, 2], queue: [3] }); + }); + + it('should enqueue items', () => { + const { result } = renderHook(() => useQueue([], 2)); + act(() => { + result.current[1].enqueue(1); + result.current[1].enqueue(2); + result.current[1].enqueue(3); + }); + expect(result.current[0]).toEqual({ active: [1, 2], queue: [3] }); + }); + + it('should dequeue items', () => { + const { result } = renderHook(() => useQueue([1, 2, 3], 2)); + let dequeuedItem: number | undefined; + act(() => { + dequeuedItem = result.current[1].dequeue(); + }); + expect(dequeuedItem).toBe(1); + expect(result.current[0]).toEqual({ active: [2, 3], queue: [] }); + }); + + it('should clear active items', () => { + const { result } = renderHook(() => useQueue([1, 2, 3, 4], 2)); + act(() => { + result.current[1].clearActive(); + }); + expect(result.current[0]).toEqual({ active: [3, 4], queue: [] }); + }); + + it('should clear queued items', () => { + const { result } = renderHook(() => useQueue([1, 2, 3, 4], 2)); + act(() => { + result.current[1].clearQueue(); + }); + expect(result.current[0]).toEqual({ active: [1, 2], queue: [] }); + }); + + it('should clear all items', () => { + const { result } = renderHook(() => useQueue([1, 2, 3, 4], 2)); + act(() => { + result.current[1].clear(); + }); + expect(result.current[0]).toEqual({ active: [], queue: [] }); + }); - // act(() => { - // result.current.increment(); - // }); - const [state] = result.current; - expect(state).toEqual({ active: [], queue: [] }); + it('should handle dequeue on empty queue', () => { + const { result } = renderHook(() => useQueue()); + let dequeuedItem: number | undefined; + act(() => { + dequeuedItem = result.current[1].dequeue(); + }); + expect(dequeuedItem).toBeUndefined(); + expect(result.current[0]).toEqual({ active: [], queue: [] }); + }); }); diff --git a/src/use-queue/use-queue.ts b/src/use-queue/use-queue.ts index 039908e..ddc57af 100644 --- a/src/use-queue/use-queue.ts +++ b/src/use-queue/use-queue.ts @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useState } from 'react'; +import { useState } from 'react'; type QueueState = { active: T[]; @@ -8,9 +8,9 @@ type QueueState = { type QueueActions = { enqueue: (item: T) => void; dequeue: () => T | undefined; + clear: () => void; clearQueue: () => void; clearActive: () => void; - clearAll: () => void; }; export function useQueue( @@ -22,20 +22,16 @@ export function useQueue( queue: initialValues.slice(limit) })); - const enqueue = useCallback( - (item: T) => { - setState((current) => { - const newState = [...current.active, ...current.queue, item]; - return { - active: newState.slice(0, limit), - queue: newState.slice(limit) - }; - }); - }, - [limit] - ); + const enqueue = (item: T) => + setState((current) => { + const newState = [...current.active, ...current.queue, item]; + return { + active: newState.slice(0, limit), + queue: newState.slice(limit) + }; + }); - const dequeue = useCallback(() => { + const dequeue = () => { let dequeuedItem: T | undefined; setState((current) => { const newState = [...current.active, ...current.queue]; @@ -46,9 +42,11 @@ export function useQueue( }; }); return dequeuedItem; - }, [limit]); + }; + + const clear = () => setState({ active: [], queue: [] }); - const clearActive = useCallback(() => { + const clearActive = () => setState((current) => { const newState = [...current.queue]; return { @@ -56,26 +54,21 @@ export function useQueue( queue: newState.slice(limit) }; }); - }, [limit]); - const clearQueue = useCallback(() => { - setState((current) => ({ ...current, queue: [] })); - }, []); + const clearQueue = () => + setState((current) => ({ + ...current, + queue: [] + })); - const clearAll = useCallback(() => { - setState({ active: [], queue: [] }); - }, []); - - const actions = useMemo( - () => ({ + return [ + state, + { enqueue, dequeue, + clear, clearActive, - clearQueue, - clearAll - }), - [enqueue, dequeue, clearQueue, clearActive, clearAll] - ); - - return [state, actions]; + clearQueue + } + ]; }