Skip to content

Commit

Permalink
Add tests to use-queue hook (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhdcodes authored Aug 7, 2024
1 parent 71ed6c2 commit 21d7fdb
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 44 deletions.
75 changes: 65 additions & 10 deletions src/use-queue/use-queue.test.ts
Original file line number Diff line number Diff line change
@@ -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<number>());
expect(result.current[0]).toEqual({ active: [], queue: [] });
});

it('should initialize with provided values', () => {
const { result } = renderHook(() => useQueue<number>([1, 2, 3], 2));
expect(result.current[0]).toEqual({ active: [1, 2], queue: [3] });
});

it('should enqueue items', () => {
const { result } = renderHook(() => useQueue<number>([], 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<number>([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<number>([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<number>([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<number>([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<number>());
let dequeuedItem: number | undefined;
act(() => {
dequeuedItem = result.current[1].dequeue();
});
expect(dequeuedItem).toBeUndefined();
expect(result.current[0]).toEqual({ active: [], queue: [] });
});
});
61 changes: 27 additions & 34 deletions src/use-queue/use-queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo, useState } from 'react';
import { useState } from 'react';

type QueueState<T> = {
active: T[];
Expand All @@ -8,9 +8,9 @@ type QueueState<T> = {
type QueueActions<T> = {
enqueue: (item: T) => void;
dequeue: () => T | undefined;
clear: () => void;
clearQueue: () => void;
clearActive: () => void;
clearAll: () => void;
};

export function useQueue<T>(
Expand All @@ -22,20 +22,16 @@ export function useQueue<T>(
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];
Expand All @@ -46,36 +42,33 @@ export function useQueue<T>(
};
});
return dequeuedItem;
}, [limit]);
};

const clear = () => setState({ active: [], queue: [] });

const clearActive = useCallback(() => {
const clearActive = () =>
setState((current) => {
const newState = [...current.queue];
return {
active: newState.slice(0, limit),
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
}
];
}

0 comments on commit 21d7fdb

Please sign in to comment.