Skip to content

Commit

Permalink
added jest test suite for local-limit (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
DDibyajyot authored Oct 24, 2024
1 parent 4ef0df5 commit 11e924c
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 7 deletions.
10 changes: 10 additions & 0 deletions frontend/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// jest.config.js
module.exports = {
roots: ['<rootDir>/lib'],
testMatch: ['**/tests/**/*.test.+(ts|tsx)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
setupFilesAfterEnv: ['<rootDir>/lib/store/tests/setupTests.ts'],
testEnvironment: 'jest-environment-jsdom',
};
30 changes: 30 additions & 0 deletions frontend/lib/store/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Tests Overview

This project includes a set of tests to ensure the reliability and functionality of key features within the codebase. The primary focus of these tests is to validate state management using the `useSearchLimit` hook implemented with Zustand.

### Test Suite Description

- **useSearchLimit Hook Tests** (`lib/store/tests/local-limit.test.ts`):
This test suite checks the behavior of a custom hook `useSearchLimit`. The hook manages a limit on daily search operations, resets the count at the start of each new day, and provides methods to increment the count or check if more searches can be made.

**Tests Included**:
- **Initialization**: Verifies that the search count starts at zero with the current date.
- **Increment Logic**: Ensures that the search count increases correctly within the same day.
- **New Day Reset**: Confirms that the search count resets to zero when a new day is detected.
- **Search Limit Checks**: Tests that `canSearch` returns the correct status based on the current search count and date.

### Running the Tests

To run the tests in this project, follow these steps:

1. **Install Dependencies**
Ensure all dependencies are installed. If not, run:
```bash
npm install
```
2. **Run the Tests**
```bash
npm run test
```
This command will execute all test suites and display the results in the terminal.

86 changes: 86 additions & 0 deletions frontend/lib/store/tests/local-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { act, renderHook } from '@testing-library/react-hooks';
import useSearchLimit from '../local-limit';

// Utility function to reset Zustand store state for testing
const resetZustandStore = () => {
useSearchLimit.setState({
searchCount: 0,
lastSearchDate: new Date().toDateString(),
});
};

// Mock `localStorage` setup for testing persistence
const mockLocalStorage = (() => {
let store: Record<string, string> = {};
return {
getItem: (key: string) => store[key] || null,
setItem: (key: string, value: string) => {
store[key] = value.toString();
},
clear: () => {
store = {};
},
};
})();

describe('useSearchLimit Hook', () => {
beforeAll(() => {
Object.defineProperty(window, 'localStorage', {
value: mockLocalStorage,
writable: true,
});
});

beforeEach(() => {
localStorage.clear();
resetZustandStore();
});

it('should initialize with zero search count and current date', () => {
const { result } = renderHook(() => useSearchLimit());

const { searchCount, lastSearchDate } = result.current;
const currentDate = new Date().toDateString();

expect(searchCount).toBe(0);
expect(lastSearchDate).toBe(currentDate);
});

it('should increment search count correctly within the same day', () => {
const { result } = renderHook(() => useSearchLimit());

act(() => {
result.current.incrementSearchCount();
result.current.incrementSearchCount();
});

const { searchCount, lastSearchDate } = result.current;
const currentDate = new Date().toDateString();

expect(searchCount).toBe(2);
expect(lastSearchDate).toBe(currentDate);
});


it('should return true for canSearch if search count is below the maximum limit', () => {
const { result } = renderHook(() => useSearchLimit());

act(() => {
result.current.incrementSearchCount();
});

expect(result.current.canSearch()).toBe(true);
});

it('should return false for canSearch if maximum search count is reached within the same day', () => {
const { result } = renderHook(() => useSearchLimit());

act(() => {
for (let i = 0; i < 10; i++) {
result.current.incrementSearchCount();
}
});

expect(result.current.canSearch()).toBe(false);
});
});
1 change: 1 addition & 0 deletions frontend/lib/store/tests/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom';
20 changes: 14 additions & 6 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"lint": "next lint",
"prettier": "prettier --write --ignore-unknown .",
"prettier:check": "prettier --check --ignore-unknown .",
"start": "next start"
"start": "next start",
"test": "jest"
},
"dependencies": {
"@ai-sdk/anthropic": "^0.0.39",
Expand Down Expand Up @@ -92,20 +93,25 @@
"zustand": "^4.5.2"
},
"devDependencies": {
"autoprefixer": "10.4.15",
"@next/bundle-analyzer": "^14.2.12",
"@tailwindcss/line-clamp": "^0.4.4",
"@tailwindcss/typography": "^0.5.13",
"tailwindcss-animate": "^1.0.7",
"tailwind-merge": "^2.3.0",
"@testing-library/jest-dom": "^6.6.2",
"@testing-library/react": "^16.0.1",
"@testing-library/react-hooks": "^8.0.1",
"@types/google-one-tap": "^1.2.6",
"@types/jest": "^29.5.14",
"@types/react": "18.2.21",
"@types/react-dom": "18.2.14",
"@types/google-one-tap": "^1.2.6",
"@vercel/style-guide": "^5.0.1",
"autoprefixer": "10.4.15",
"dotenv": "^16.3.1",
"eslint": "^8.52.0",
"eslint-config-next": "^14.0.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-tailwindcss": "^3.15.1",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"mdast-util-toc": "^7.0.1",
"patch-package": "^8.0.0",
"prettier": "^3.0.3",
Expand All @@ -115,8 +121,10 @@
"rehype-slug": "^6.0.0",
"remark": "^15.0.1",
"remark-gfm": "^4.0.0",
"tailwind-merge": "^2.3.0",
"tailwindcss": "^3.4.3",
"@next/bundle-analyzer": "^14.2.12"
"tailwindcss-animate": "^1.0.7",
"ts-jest": "^29.2.5"
},
"engines": {
"node": ">=18.17.0"
Expand Down
3 changes: 2 additions & 1 deletion frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"paths": {
"@/*": ["./*"],
"contentlayer/generated": ["./.contentlayer/generated"]
}
},
"types": ["jest"],
},
"include": [
"next-env.d.ts",
Expand Down

0 comments on commit 11e924c

Please sign in to comment.