-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters