Skip to content

Commit

Permalink
move demo to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
DevAnsar committed Jan 4, 2024
1 parent acc0b06 commit dea1458
Show file tree
Hide file tree
Showing 21 changed files with 11 additions and 6 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions demo/package.json → docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": true,
"version": "0.0.0",
"type": "module",
"homepage": "https://devansar.github.io/react-search-hook",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
Expand Down
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion src/__tests__/hook/useSearch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SearchProvider from '../../SearchProvider'
import SearchContext from '../../SearchContext'
import { act } from 'react-dom/test-utils'
import { SearchContextType, UseSearchOptions } from '../../types'
import { invalidStoreNameError } from '../../utils/errors'

describe('useSearch', () => {
it('should thrown an error if the SearchContext is not available', () => {
Expand All @@ -20,7 +21,7 @@ describe('useSearch', () => {
<SearchContext.Provider value={fakeSearchState}>{children}</SearchContext.Provider>
)

expect(() => renderHook(() => useSearch('product'), { wrapper })).toThrow(Error(`Invalid store name: product`))
expect(() => renderHook(() => useSearch('product'), { wrapper })).toThrow(invalidStoreNameError('product'))
})

it('should get search value from context with useSearch successfully', () => {
Expand Down
7 changes: 2 additions & 5 deletions src/hooks/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as React from 'react'
import SearchContext from '../SearchContext'
import { getfilterItems } from '../utils/filter'
import { UseSearchOptions, UseSearchResponse } from '../types'
import { invalidStoreNameError } from '../utils/errors'

/**
* @function
Expand All @@ -43,11 +44,7 @@ const useSearch = <TItem extends any>(

// Check if the provided store is allowed
if (context.stores && context.stores[storeName] === undefined) {
throw new Error(`
Invalid store name: ${storeName}
Make sure you have defined the ${storeName} store for the SearchProvider
For more information, please refer to: https://github.com/DevAnsar/react-search-hook?tab=readme-ov-file#2-add-provider-to-top-of-your-component-tree
`)
throw invalidStoreNameError(storeName)
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const invalidStoreNameError = (storeName: string) =>
Error(`
Invalid store name: ${storeName}
Make sure you have defined the ${storeName} store for the SearchProvider
For more information, please refer to: https://github.com/DevAnsar/react-search-hook?tab=readme-ov-file#2-add-provider-to-top-of-your-component-tree
`)

0 comments on commit dea1458

Please sign in to comment.