|
| 1 | +import React from 'react' |
| 2 | +import { renderHook } from '@testing-library/react' |
| 3 | +import useSearch from '../../hooks/useSearch' |
| 4 | +import SearchProvider from '../../SearchProvider' |
| 5 | +import SearchContext from '../../SearchContext' |
| 6 | +import { act } from 'react-dom/test-utils' |
| 7 | +import { SearchContextType, UseSearchOptions } from '../../types' |
| 8 | + |
| 9 | +describe('useSearch', () => { |
| 10 | + it('should thrown an error if the SearchContext is not available', () => { |
| 11 | + expect(useSearch).toThrow(Error) |
| 12 | + }) |
| 13 | + |
| 14 | + it('useSearch should throw Error when store name is not find', () => { |
| 15 | + const fakeSearchState = { |
| 16 | + stores: {}, |
| 17 | + changeStoreValue: jest.fn(), |
| 18 | + } |
| 19 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 20 | + <SearchContext.Provider value={fakeSearchState}>{children}</SearchContext.Provider> |
| 21 | + ) |
| 22 | + |
| 23 | + expect(() => renderHook(() => useSearch('product'), { wrapper })).toThrow(Error(`Invalid store name: product`)) |
| 24 | + }) |
| 25 | + |
| 26 | + it('should get search value from context with useSearch successfully', () => { |
| 27 | + const fakeSearchState = { |
| 28 | + stores: { product: 'test_product' }, |
| 29 | + changeStoreValue: jest.fn(), |
| 30 | + } |
| 31 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 32 | + <SearchContext.Provider value={fakeSearchState}>{children}</SearchContext.Provider> |
| 33 | + ) |
| 34 | + const { result } = renderHook(() => useSearch('product'), { wrapper }) |
| 35 | + expect(result.current.search).toEqual('test_product') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should define search when it specify with SearchProvider', () => { |
| 39 | + const fakeSearchState = { |
| 40 | + stores: {}, |
| 41 | + changeStoreValue: jest.fn(), |
| 42 | + } |
| 43 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 44 | + <SearchContext.Provider value={fakeSearchState}> |
| 45 | + <SearchProvider stores={['product']}>{children}</SearchProvider> |
| 46 | + </SearchContext.Provider> |
| 47 | + ) |
| 48 | + const { result } = renderHook(() => useSearch('product'), { wrapper }) |
| 49 | + expect(result.current.search).toBeDefined() |
| 50 | + }) |
| 51 | + |
| 52 | + it('should update search state with setSearch', () => { |
| 53 | + const fakeSearchState = { |
| 54 | + stores: {}, |
| 55 | + changeStoreValue: jest.fn(), |
| 56 | + } |
| 57 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 58 | + <SearchContext.Provider value={fakeSearchState}> |
| 59 | + <SearchProvider stores={['product']}>{children}</SearchProvider> |
| 60 | + </SearchContext.Provider> |
| 61 | + ) |
| 62 | + const { result } = renderHook(() => useSearch('product'), { wrapper }) |
| 63 | + act(() => { |
| 64 | + result.current.setSearch('product_search_value') |
| 65 | + }) |
| 66 | + expect(result.current.search).toEqual('product_search_value') |
| 67 | + }) |
| 68 | + |
| 69 | + it('should filter data with search value (array of string)', () => { |
| 70 | + const fakeSearchContextValue: SearchContextType = { |
| 71 | + stores: {}, |
| 72 | + changeStoreValue: jest.fn(), |
| 73 | + } |
| 74 | + |
| 75 | + const fakeItems: string[] = ['product1', 'product2', 'product3', 'product3', 'product4'] |
| 76 | + const fakeUseSearchOptions: UseSearchOptions = { items: fakeItems } |
| 77 | + |
| 78 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 79 | + <SearchContext.Provider value={fakeSearchContextValue}> |
| 80 | + <SearchProvider stores={['products']}>{children}</SearchProvider> |
| 81 | + </SearchContext.Provider> |
| 82 | + ) |
| 83 | + const { result } = renderHook(() => useSearch('products', fakeUseSearchOptions), { wrapper }) |
| 84 | + act(() => { |
| 85 | + result.current.setSearch('product3') |
| 86 | + }) |
| 87 | + expect(result.current.searchResult).toEqual(['product3', 'product3']) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should filter data with search value (array of object - with correct searchProp)', () => { |
| 91 | + const fakeSearchContextValue: SearchContextType = { |
| 92 | + stores: {}, |
| 93 | + changeStoreValue: jest.fn(), |
| 94 | + } |
| 95 | + |
| 96 | + const fakeItems = [ |
| 97 | + { name: 'product1' }, |
| 98 | + { name: 'product2' }, |
| 99 | + { name: 'product3' }, |
| 100 | + { name: 'product3' }, |
| 101 | + { name: 'product4' }, |
| 102 | + ] |
| 103 | + const fakeUseSearchOptions: UseSearchOptions<{ name: string }> = { items: fakeItems, searchProp: 'name' } |
| 104 | + |
| 105 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 106 | + <SearchContext.Provider value={fakeSearchContextValue}> |
| 107 | + <SearchProvider stores={['products']}>{children}</SearchProvider> |
| 108 | + </SearchContext.Provider> |
| 109 | + ) |
| 110 | + const { result } = renderHook(() => useSearch('products', fakeUseSearchOptions), { wrapper }) |
| 111 | + act(() => { |
| 112 | + result.current.setSearch('product3') |
| 113 | + }) |
| 114 | + expect(result.current.searchResult).toHaveLength(2) |
| 115 | + }) |
| 116 | + |
| 117 | + it('should filter data with search value (array of object - with non-correct searchProp)', () => { |
| 118 | + const fakeSearchContextValue: SearchContextType = { |
| 119 | + stores: {}, |
| 120 | + changeStoreValue: jest.fn(), |
| 121 | + } |
| 122 | + |
| 123 | + const fakeItems = [{ name: 'product1' }, { name: 'product2' }] |
| 124 | + const fakeUseSearchOptions: UseSearchOptions<{ name: string }> = { items: fakeItems, searchProp: 'name1' } |
| 125 | + |
| 126 | + const wrapper = ({ children }: { children: React.ReactNode }) => ( |
| 127 | + <SearchContext.Provider value={fakeSearchContextValue}> |
| 128 | + <SearchProvider stores={['products']}>{children}</SearchProvider> |
| 129 | + </SearchContext.Provider> |
| 130 | + ) |
| 131 | + const { result } = renderHook(() => useSearch('products', fakeUseSearchOptions), { wrapper }) |
| 132 | + act(() => { |
| 133 | + result.current.setSearch('product1') |
| 134 | + }) |
| 135 | + expect(result.current.searchResult).toHaveLength(0) |
| 136 | + }) |
| 137 | +}) |
0 commit comments