-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added jest test suite for local-limit (#85)
- Loading branch information
1 parent
4ef0df5
commit 11e924c
Showing
6 changed files
with
143 additions
and
7 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 |
---|---|---|
@@ -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', | ||
}; |
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,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. | ||
|
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,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); | ||
}); | ||
}); |
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 @@ | ||
import '@testing-library/jest-dom'; |
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