|
| 1 | +import { Store } from '@ngxs/store'; |
| 2 | + |
1 | 3 | import { MockComponents } from 'ng-mocks'; |
2 | 4 |
|
3 | 5 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
4 | 6 |
|
5 | 7 | import { FilterChipsComponent } from '@osf/shared/components/filter-chips/filter-chips.component'; |
6 | 8 | import { SearchFiltersComponent } from '@osf/shared/components/search-filters/search-filters.component'; |
| 9 | +import { DiscoverableFilter, FilterOption } from '@osf/shared/models/search/discaverable-filter.model'; |
| 10 | +import { |
| 11 | + ClearFilterSearchResults, |
| 12 | + FetchResources, |
| 13 | + GlobalSearchSelectors, |
| 14 | + LoadFilterOptions, |
| 15 | + LoadFilterOptionsWithSearch, |
| 16 | + LoadMoreFilterOptions, |
| 17 | + UpdateSelectedFilterOption, |
| 18 | +} from '@shared/stores/global-search'; |
7 | 19 |
|
8 | 20 | import { FiltersSectionComponent } from './filters-section.component'; |
9 | 21 |
|
10 | | -describe.skip('FiltersSectionComponent', () => { |
| 22 | +import { OSFTestingModule } from '@testing/osf.testing.module'; |
| 23 | +import { provideMockStore } from '@testing/providers/store-provider.mock'; |
| 24 | + |
| 25 | +describe('FiltersSectionComponent', () => { |
11 | 26 | let component: FiltersSectionComponent; |
12 | 27 | let fixture: ComponentFixture<FiltersSectionComponent>; |
| 28 | + let store: Store; |
| 29 | + |
| 30 | + const mockFilters = [{ key: 'filter1', label: 'Filter 1' }] as DiscoverableFilter[]; |
| 31 | + const mockSelectedOptions = { filter1: [{ value: 'option1', label: 'Option 1' }] as FilterOption[] }; |
| 32 | + const mockFilterSearchCache = { filter1: [] }; |
13 | 33 |
|
14 | 34 | beforeEach(async () => { |
15 | 35 | await TestBed.configureTestingModule({ |
16 | | - imports: [FiltersSectionComponent, ...MockComponents(FilterChipsComponent, SearchFiltersComponent)], |
| 36 | + imports: [ |
| 37 | + FiltersSectionComponent, |
| 38 | + OSFTestingModule, |
| 39 | + ...MockComponents(FilterChipsComponent, SearchFiltersComponent), |
| 40 | + ], |
| 41 | + providers: [ |
| 42 | + provideMockStore({ |
| 43 | + signals: [ |
| 44 | + { selector: GlobalSearchSelectors.getFilters, value: mockFilters }, |
| 45 | + { selector: GlobalSearchSelectors.getSelectedOptions, value: mockSelectedOptions }, |
| 46 | + { selector: GlobalSearchSelectors.getFilterSearchCache, value: mockFilterSearchCache }, |
| 47 | + { selector: GlobalSearchSelectors.getResourcesLoading, value: false }, |
| 48 | + ], |
| 49 | + }), |
| 50 | + ], |
17 | 51 | }).compileComponents(); |
18 | 52 |
|
19 | 53 | fixture = TestBed.createComponent(FiltersSectionComponent); |
20 | 54 | component = fixture.componentInstance; |
| 55 | + store = TestBed.inject(Store); |
21 | 56 | fixture.detectChanges(); |
22 | 57 | }); |
23 | 58 |
|
24 | 59 | it('should create', () => { |
25 | 60 | expect(component).toBeTruthy(); |
26 | 61 | }); |
| 62 | + |
| 63 | + it('should dispatch UpdateSelectedFilterOption and FetchResources when filter options change', () => { |
| 64 | + const filter = mockFilters[0]; |
| 65 | + const filterOption = [{ value: 'option2', label: 'Option 2' }] as FilterOption[]; |
| 66 | + |
| 67 | + component.onSelectedFilterOptionsChanged({ filter, filterOption }); |
| 68 | + |
| 69 | + expect(store.dispatch).toHaveBeenCalledWith(new UpdateSelectedFilterOption(filter.key, filterOption)); |
| 70 | + expect(store.dispatch).toHaveBeenCalledWith(new FetchResources()); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should dispatch LoadFilterOptions when loading filter options', () => { |
| 74 | + const filter = mockFilters[0]; |
| 75 | + |
| 76 | + component.onLoadFilterOptions(filter); |
| 77 | + |
| 78 | + expect(store.dispatch).toHaveBeenCalledWith(new LoadFilterOptions(filter.key)); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should dispatch LoadMoreFilterOptions when loading more filter options', () => { |
| 82 | + const filter = mockFilters[0]; |
| 83 | + |
| 84 | + component.onLoadMoreFilterOptions(filter); |
| 85 | + |
| 86 | + expect(store.dispatch).toHaveBeenCalledWith(new LoadMoreFilterOptions(filter.key)); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should dispatch LoadFilterOptionsWithSearch when searching with text', () => { |
| 90 | + const filter = mockFilters[0]; |
| 91 | + const searchText = 'test'; |
| 92 | + |
| 93 | + component.onSearchFilterOptions({ searchText, filter }); |
| 94 | + |
| 95 | + expect(store.dispatch).toHaveBeenCalledWith(new LoadFilterOptionsWithSearch(filter.key, searchText)); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should dispatch ClearFilterSearchResults when searching with empty text', () => { |
| 99 | + const filter = mockFilters[0]; |
| 100 | + const searchText = ' '; |
| 101 | + |
| 102 | + component.onSearchFilterOptions({ searchText, filter }); |
| 103 | + |
| 104 | + expect(store.dispatch).toHaveBeenCalledWith(new ClearFilterSearchResults(filter.key)); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should dispatch UpdateSelectedFilterOption and FetchResources when filter chip is removed', () => { |
| 108 | + const filterKey = 'filter1'; |
| 109 | + const optionRemoved = mockSelectedOptions.filter1[0]; |
| 110 | + |
| 111 | + component.onFilterChipRemoved({ filterKey, optionRemoved }); |
| 112 | + |
| 113 | + expect(store.dispatch).toHaveBeenCalledWith(new UpdateSelectedFilterOption(filterKey, [])); |
| 114 | + expect(store.dispatch).toHaveBeenCalledWith(new FetchResources()); |
| 115 | + }); |
27 | 116 | }); |
0 commit comments