-
Notifications
You must be signed in to change notification settings - Fork 168
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
Sadanand Pai
committed
Oct 4, 2023
1 parent
4ccdb3e
commit b4ab208
Showing
5 changed files
with
165 additions
and
30 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
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
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
118 changes: 118 additions & 0 deletions
118
src/apps/sorting-visualizer/__tests__/hooks/use-algo.hook.spec.ts
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { | ||
playSimulation, | ||
setHighlightInterval, | ||
setSwapInterval, | ||
} from '@/apps/sorting-visualizer/store/global.state'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
|
||
import { algoList } from '@/apps/sorting-visualizer/sorting-algorithms/algo-list'; | ||
import { getRndmNumInRange } from '../../helpers/array-helpers'; | ||
import { initialArray } from '@/apps/sorting-visualizer/config'; | ||
import useAlgo from '@/apps/sorting-visualizer/hooks/use-algo.hook'; | ||
|
||
describe('useAlgo hook', () => { | ||
beforeAll(() => { | ||
setSwapInterval(0); | ||
setHighlightInterval(0); | ||
playSimulation(); | ||
}); | ||
|
||
it('should sort initial array', async () => { | ||
for (const algo of algoList) { | ||
const array = initialArray; | ||
const { result } = renderHook(() => useAlgo(array, algo.fn)); | ||
await waitFor(() => expect(result.current.isCompleted).toBe(true)); | ||
|
||
const sortedArray = [...array].sort((a, b) => a - b); | ||
expect(array).toEqual(sortedArray); | ||
} | ||
}); | ||
|
||
it('should sort random array of small length', async () => { | ||
const smallArray = Array.from(new Array(10), () => getRndmNumInRange()); | ||
|
||
for (const algo of algoList) { | ||
const array = [...smallArray]; | ||
|
||
const { result } = renderHook(() => useAlgo(array, algo.fn)); | ||
await waitFor(() => expect(result.current.isCompleted).toBe(true)); | ||
|
||
const sortedArray = [...array].sort((a, b) => a - b); | ||
expect(array).toEqual(sortedArray); | ||
} | ||
}); | ||
|
||
it('should sort random array of large length', async () => { | ||
const largeArray = Array.from(new Array(25), () => getRndmNumInRange()); | ||
|
||
for (const algo of algoList) { | ||
const array = [...largeArray]; | ||
|
||
const { result } = renderHook(() => useAlgo(array, algo.fn)); | ||
await waitFor(() => expect(result.current.isCompleted).toBe(true)); | ||
|
||
const sortedArray = [...array].sort((a, b) => a - b); | ||
expect(array).toEqual(sortedArray); | ||
} | ||
}, 10000); | ||
|
||
it('should sort an array of single element', async () => { | ||
for (const algo of algoList) { | ||
const array = [5]; | ||
|
||
const { result } = renderHook(() => useAlgo(array, algo.fn)); | ||
await waitFor(() => expect(result.current.isCompleted).toBe(true)); | ||
|
||
const sortedArray = [...array].sort((a, b) => a - b); | ||
expect(array).toEqual(sortedArray); | ||
} | ||
}); | ||
|
||
it('should sort an array of two elements', async () => { | ||
for (const algo of algoList) { | ||
const array = [5, 2]; | ||
|
||
const { result } = renderHook(() => useAlgo(array, algo.fn)); | ||
await waitFor(() => expect(result.current.isCompleted).toBe(true)); | ||
|
||
const sortedArray = [...array].sort((a, b) => a - b); | ||
expect(array).toEqual(sortedArray); | ||
} | ||
}); | ||
|
||
it('should sort an array of duplicate elements', async () => { | ||
for (const algo of algoList) { | ||
const array = [5, 2, 7, 2, 4, 2, 8, 1, 5]; | ||
|
||
const { result } = renderHook(() => useAlgo(array, algo.fn)); | ||
await waitFor(() => expect(result.current.isCompleted).toBe(true)); | ||
|
||
const sortedArray = [...array].sort((a, b) => a - b); | ||
expect(array).toEqual(sortedArray); | ||
} | ||
}); | ||
|
||
it('should sort an array of sorted elements', async () => { | ||
for (const algo of algoList) { | ||
const array = [3, 5, 7, 8, 9, 12]; | ||
|
||
const { result } = renderHook(() => useAlgo(array, algo.fn)); | ||
await waitFor(() => expect(result.current.isCompleted).toBe(true)); | ||
|
||
const sortedArray = [...array].sort((a, b) => a - b); | ||
expect(array).toEqual(sortedArray); | ||
} | ||
}); | ||
|
||
it('should sort an array of reverse sorted elements', async () => { | ||
for (const algo of algoList) { | ||
const array = [12, 9, 8, 6, 4, 2, 1, 0]; | ||
|
||
const { result } = renderHook(() => useAlgo(array, algo.fn)); | ||
await waitFor(() => expect(result.current.isCompleted).toBe(true)); | ||
|
||
const sortedArray = [...array].sort((a, b) => a - b); | ||
expect(array).toEqual(sortedArray); | ||
} | ||
}); | ||
}); |
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