|
| 1 | +import { createComponentFactory, mockProvider, Spectator, SpyObject } from '@ngneat/spectator/jest'; |
| 2 | +import { of } from 'rxjs'; |
| 3 | + |
| 4 | +import { By } from '@angular/platform-browser'; |
| 5 | + |
| 6 | +import { MultiSelect, MultiSelectChangeEvent } from 'primeng/multiselect'; |
| 7 | + |
| 8 | +import { DotContentTypeService, DotMessageService } from '@dotcms/data-access'; |
| 9 | + |
| 10 | +import { DotContentDriveBaseTypeSelectorComponent } from './dot-content-drive-base-type-selector.component'; |
| 11 | + |
| 12 | +import { MOCK_BASE_TYPES } from '../../../../shared/mocks'; |
| 13 | +import { DotContentDriveStore } from '../../../../store/dot-content-drive.store'; |
| 14 | + |
| 15 | +describe('DotContentDriveBaseTypeSelectorComponent', () => { |
| 16 | + let spectator: Spectator<DotContentDriveBaseTypeSelectorComponent>; |
| 17 | + let component: DotContentDriveBaseTypeSelectorComponent; |
| 18 | + let store: SpyObject<InstanceType<typeof DotContentDriveStore>>; |
| 19 | + let contentTypeService: SpyObject<DotContentTypeService>; |
| 20 | + |
| 21 | + const createComponent = createComponentFactory({ |
| 22 | + component: DotContentDriveBaseTypeSelectorComponent, |
| 23 | + providers: [ |
| 24 | + mockProvider(DotMessageService, { |
| 25 | + get: jest.fn() |
| 26 | + }), |
| 27 | + mockProvider(DotContentDriveStore, { |
| 28 | + patchFilters: jest.fn(), |
| 29 | + removeFilter: jest.fn(), |
| 30 | + getFilterValue: jest.fn() |
| 31 | + }), |
| 32 | + mockProvider(DotContentTypeService, { |
| 33 | + getAllContentTypes: jest.fn().mockReturnValue(of(MOCK_BASE_TYPES)) |
| 34 | + }) |
| 35 | + ], |
| 36 | + detectChanges: false |
| 37 | + }); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + spectator = createComponent(); |
| 41 | + component = spectator.component; |
| 42 | + store = spectator.inject(DotContentDriveStore, true); |
| 43 | + contentTypeService = spectator.inject(DotContentTypeService); |
| 44 | + store.getFilterValue.mockReturnValue([]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should fetch content types and filter out FORM type', () => { |
| 48 | + spectator.detectChanges(); |
| 49 | + |
| 50 | + expect(contentTypeService.getAllContentTypes).toHaveBeenCalled(); |
| 51 | + expect(component.$state().baseTypes).toEqual([ |
| 52 | + { name: 'Content', label: 'Content', types: null }, |
| 53 | + { name: 'Widget', label: 'Widget', types: null }, |
| 54 | + { name: 'FileAsset', label: 'FileAsset', types: null } |
| 55 | + ]); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should set selectedBaseTypes when store has baseType filter', () => { |
| 59 | + store.getFilterValue.mockReturnValue(['1', '2']); |
| 60 | + |
| 61 | + spectator.detectChanges(); |
| 62 | + |
| 63 | + expect(store.getFilterValue).toHaveBeenCalledWith('baseType'); |
| 64 | + expect(component.$selectedBaseTypes()).toEqual(['CONTENT', 'WIDGET']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should patch filters with keys when selectedBaseTypes has values', () => { |
| 68 | + spectator.detectChanges(); |
| 69 | + |
| 70 | + const multiSelectDebugElement = spectator.fixture.debugElement.query( |
| 71 | + By.directive(MultiSelect) |
| 72 | + ); |
| 73 | + |
| 74 | + spectator.triggerEventHandler(multiSelectDebugElement, 'ngModelChange', [ |
| 75 | + 'CONTENT', |
| 76 | + 'WIDGET' |
| 77 | + ]); |
| 78 | + |
| 79 | + expect(component.$selectedBaseTypes()).toEqual(['CONTENT', 'WIDGET']); |
| 80 | + |
| 81 | + spectator.triggerEventHandler(MultiSelect, 'onChange', {} as MultiSelectChangeEvent); |
| 82 | + |
| 83 | + expect(store.patchFilters).toHaveBeenCalledWith({ |
| 84 | + baseType: ['1', '2'] |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should remove filter when selectedBaseTypes is empty', () => { |
| 89 | + component.$selectedBaseTypes.set([]); |
| 90 | + |
| 91 | + const multiSelectDebugElement = spectator.fixture.debugElement.query( |
| 92 | + By.directive(MultiSelect) |
| 93 | + ); |
| 94 | + |
| 95 | + spectator.triggerEventHandler(multiSelectDebugElement, 'ngModelChange', []); |
| 96 | + |
| 97 | + spectator.triggerEventHandler(MultiSelect, 'onChange', {} as MultiSelectChangeEvent); |
| 98 | + |
| 99 | + expect(store.removeFilter).toHaveBeenCalledWith('baseType'); |
| 100 | + }); |
| 101 | +}); |
0 commit comments