Skip to content

Commit

Permalink
useAlgo hook UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sadanand Pai committed Oct 4, 2023
1 parent 4ccdb3e commit b4ab208
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- name: setup deployment
run: |
npm install
npm test
rm -rf dist
npm run build
git add dist -f
Expand Down
35 changes: 24 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<div align="center">
<h1>Sorting Visualizer</h1>
<h2>Responsive visualizer built using React for popular algorithms</h2>
<a href="https://sadanandpai.github.io/sorting-visualizer/dist/"><img src="./public/images/cell.png" alt="cover" /></a>
<br/>
<a href="https://sadanandpai.github.io/sorting-visualizer/dist/"><img src="./public/images/bar.png" alt="cover" /></a>
<a href="https://sadanandpai.github.io/sorting-visualizer/dist/">See it in action</a>
<h1>Sorting Visualizer</h1>
<h2>Responsive visualizer built using React for popular algorithms</h2>
<div>
<a name="stars"><img src="https://img.shields.io/github/stars/sadanandpai/sorting-visualizer?style=for-the-badge"></a>
<a name="forks"><img src="https://img.shields.io/github/forks/sadanandpai/sorting-visualizer?logoColor=green&style=for-the-badge"></a>
<a name="license"><img src="https://img.shields.io/github/license/sadanandpai/sorting-visualizer?style=for-the-badge"></a>
</div>

[![built with Codeium](https://codeium.com/badges/main)](https://codeium.com)

<a href="https://sadanandpai.github.io/sorting-visualizer/dist/"><img src="./public/images/cell.png" alt="cover" /></a>
<br/>
<a href="https://sadanandpai.github.io/sorting-visualizer/dist/"><img src="./public/images/bar.png" alt="cover" /></a>
<a href="https://sadanandpai.github.io/sorting-visualizer/dist/">See it in
action</a>

</div>

---
Expand Down Expand Up @@ -35,9 +45,12 @@ Features

### Features built using

- Animations are done using pure CSS and uses [FLIP principle](https://aerotwist.com/blog/flip-your-animations/)
- Flex is used for array display and manipulation of positions (flex order property)
- JavaScript async generators are used heavily for the controlled execution of algorithms
- Animations are done using pure CSS and uses
[FLIP principle](https://aerotwist.com/blog/flip-your-animations/)
- Flex is used for array display and manipulation of positions (flex order
property)
- JavaScript async generators are used heavily for the controlled execution of
algorithms

### Run in your local

Expand All @@ -49,8 +62,8 @@ Features

### Contributing Guide

If you want to contribute, improve or fix bugs in this repo, then check out the [Contributing Guide](./CONTRIBUTING.md)
<br/>
If you want to contribute, improve or fix bugs in this repo, then check out the
[Contributing Guide](./CONTRIBUTING.md) <br/>

### License

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
"preview": "vite preview",
"test": "vitest"
},
"dependencies": {
"@reduxjs/toolkit": "^1.9.5",
Expand Down
118 changes: 118 additions & 0 deletions src/apps/sorting-visualizer/__tests__/hooks/use-algo.hook.spec.ts
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);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ describe('bubble sort', () => {
});

it('should sort initial array', async () => {
for (const sort of algoList) {
for (const algo of algoList) {
const array = initialArray;
const it = sort.fn(array);
const it = algo.fn(array);
for await (const _ of it) {
/* empty */
}
Expand All @@ -29,10 +29,12 @@ describe('bubble sort', () => {
});

it('should sort random array of small length', async () => {
for (const sort of algoList) {
const array = Array.from(new Array(10), () => getRndmNumInRange());
const smallArray = Array.from(new Array(10), () => getRndmNumInRange());

const it = sort.fn(array);
for (const algo of algoList) {
const array = [...smallArray];

const it = algo.fn(array);
for await (const _ of it) {
/* empty */
}
Expand All @@ -45,24 +47,24 @@ describe('bubble sort', () => {
it('should sort random array of large length', async () => {
const largeArray = Array.from(new Array(30), () => getRndmNumInRange());

for (const sort of algoList) {
for (const algo of algoList) {
const array = [...largeArray];

const it = sort.fn(array);
const it = algo.fn(array);
for await (const _ of it) {
/* empty */
}

const sortedArray = [...array].sort((a, b) => a - b);
expect(array).toEqual(sortedArray);
}
}, 15000);
}, 10000);

it('should sort an array of single element', async () => {
for (const sort of algoList) {
for (const algo of algoList) {
const array = [5];

const it = sort.fn(array);
const it = algo.fn(array);
for await (const _ of it) {
/* empty */
}
Expand All @@ -73,10 +75,10 @@ describe('bubble sort', () => {
});

it('should sort an array of two elements', async () => {
for (const sort of algoList) {
for (const algo of algoList) {
const array = [5, 2];

const it = sort.fn(array);
const it = algo.fn(array);
for await (const _ of it) {
/* empty */
}
Expand All @@ -87,10 +89,10 @@ describe('bubble sort', () => {
});

it('should sort an array of duplicate elements', async () => {
for (const sort of algoList) {
for (const algo of algoList) {
const array = [5, 2, 7, 2, 4, 2, 8, 1, 5];

const it = sort.fn(array);
const it = algo.fn(array);
for await (const _ of it) {
/* empty */
}
Expand All @@ -101,10 +103,10 @@ describe('bubble sort', () => {
});

it('should sort an array of sorted elements', async () => {
for (const sort of algoList) {
for (const algo of algoList) {
const array = [3, 5, 7, 8, 9, 12];

const it = sort.fn(array);
const it = algo.fn(array);
for await (const _ of it) {
/* empty */
}
Expand All @@ -115,10 +117,10 @@ describe('bubble sort', () => {
});

it('should sort an array of reverse sorted elements', async () => {
for (const sort of algoList) {
for (const algo of algoList) {
const array = [12, 9, 8, 6, 4, 2, 1, 0];

const it = sort.fn(array);
const it = algo.fn(array);
for await (const _ of it) {
/* empty */
}
Expand Down

0 comments on commit b4ab208

Please sign in to comment.