1 import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
2 import { provideHttpClientTesting } from '@angular/common/http/testing'
3 import { ComponentFixture, TestBed } from '@angular/core/testing'
4 import { FormsModule, ReactiveFormsModule } from '@angular/forms'
5 import { NgbModule } from '@ng-bootstrap/ng-bootstrap'
6 import { NgSelectModule } from '@ng-select/ng-select'
7 import { NgxBootstrapIconsModule, allIcons } from 'ngx-bootstrap-icons'
8 import { of } from 'rxjs'
9 import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive'
10 import { PermissionsService } from 'src/app/services/permissions.service'
11 import { UserService } from 'src/app/services/rest/user.service'
12 import { SettingsService } from 'src/app/services/settings.service'
13 import { ClearableBadgeComponent } from '../clearable-badge/clearable-badge.component'
16 PermissionsFilterDropdownComponent,
17 PermissionsSelectionModel,
18 } from './permissions-filter-dropdown.component'
20 const currentUserID = 13
22 describe('PermissionsFilterDropdownComponent', () => {
23 let component: PermissionsFilterDropdownComponent
24 let fixture: ComponentFixture<PermissionsFilterDropdownComponent>
25 let ownerFilterSetResult: PermissionsSelectionModel
27 beforeEach(async () => {
28 TestBed.configureTestingModule({
30 PermissionsFilterDropdownComponent,
31 ClearableBadgeComponent,
32 IfPermissionsDirective,
39 NgxBootstrapIconsModule.pick(allIcons),
61 provide: PermissionsService,
63 currentUserCan: () => true,
67 provide: SettingsService,
74 provideHttpClient(withInterceptorsFromDi()),
75 provideHttpClientTesting(),
77 }).compileComponents()
79 fixture = TestBed.createComponent(PermissionsFilterDropdownComponent)
80 component = fixture.componentInstance
81 component.ownerFilterSet.subscribe(
82 (model) => (ownerFilterSetResult = model)
84 component.selectionModel = new PermissionsSelectionModel()
86 fixture.detectChanges()
89 it('should report is active', () => {
90 component.setFilter(OwnerFilterType.NONE)
91 expect(component.isActive).toBeFalsy()
92 component.setFilter(OwnerFilterType.OTHERS)
93 expect(component.isActive).toBeTruthy()
94 component.setFilter(OwnerFilterType.NONE)
95 component.selectionModel.hideUnowned = true
96 expect(component.isActive).toBeTruthy()
99 it('should support reset', () => {
100 component.setFilter(OwnerFilterType.OTHERS)
101 expect(component.selectionModel.ownerFilter).not.toEqual(
105 expect(component.selectionModel.ownerFilter).toEqual(OwnerFilterType.NONE)
108 it('should toggle owner filter type when users selected', () => {
109 component.selectionModel.ownerFilter = OwnerFilterType.NONE
111 // this would normally be done by select component
112 component.selectionModel.includeUsers = [12]
113 component.onUserSelect()
114 expect(component.selectionModel.ownerFilter).toEqual(OwnerFilterType.OTHERS)
116 // this would normally be done by select component
117 component.selectionModel.includeUsers = null
118 component.onUserSelect()
120 expect(component.selectionModel.ownerFilter).toEqual(OwnerFilterType.NONE)
122 it('should emit a selection model depending on the type of owner filter set', () => {
123 component.selectionModel.ownerFilter = OwnerFilterType.NONE
125 component.setFilter(OwnerFilterType.SELF)
126 expect(ownerFilterSetResult).toEqual({
130 ownerFilter: OwnerFilterType.SELF,
131 userID: currentUserID,
134 component.setFilter(OwnerFilterType.NOT_SELF)
135 expect(ownerFilterSetResult).toEqual({
136 excludeUsers: [currentUserID],
139 ownerFilter: OwnerFilterType.NOT_SELF,
143 component.setFilter(OwnerFilterType.NONE)
144 expect(ownerFilterSetResult).toEqual({
148 ownerFilter: OwnerFilterType.NONE,
152 component.setFilter(OwnerFilterType.SHARED_BY_ME)
153 expect(ownerFilterSetResult).toEqual({
157 ownerFilter: OwnerFilterType.SHARED_BY_ME,
158 userID: currentUserID,
161 component.setFilter(OwnerFilterType.UNOWNED)
162 expect(ownerFilterSetResult).toEqual({
166 ownerFilter: OwnerFilterType.UNOWNED,