]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
bd07eb02ce67cd16059cebf179297aaa2eea4431
[thirdparty/paperless-ngx.git] /
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'
14 import {
15 OwnerFilterType,
16 PermissionsFilterDropdownComponent,
17 PermissionsSelectionModel,
18 } from './permissions-filter-dropdown.component'
19
20 const currentUserID = 13
21
22 describe('PermissionsFilterDropdownComponent', () => {
23 let component: PermissionsFilterDropdownComponent
24 let fixture: ComponentFixture<PermissionsFilterDropdownComponent>
25 let ownerFilterSetResult: PermissionsSelectionModel
26
27 beforeEach(async () => {
28 TestBed.configureTestingModule({
29 declarations: [
30 PermissionsFilterDropdownComponent,
31 ClearableBadgeComponent,
32 IfPermissionsDirective,
33 ],
34 imports: [
35 NgSelectModule,
36 FormsModule,
37 ReactiveFormsModule,
38 NgbModule,
39 NgxBootstrapIconsModule.pick(allIcons),
40 ],
41 providers: [
42 {
43 provide: UserService,
44 useValue: {
45 listAll: () =>
46 of({
47 results: [
48 {
49 id: 1,
50 username: 'user1',
51 },
52 {
53 id: 10,
54 username: 'user10',
55 },
56 ],
57 }),
58 },
59 },
60 {
61 provide: PermissionsService,
62 useValue: {
63 currentUserCan: () => true,
64 },
65 },
66 {
67 provide: SettingsService,
68 useValue: {
69 currentUser: {
70 id: currentUserID,
71 },
72 },
73 },
74 provideHttpClient(withInterceptorsFromDi()),
75 provideHttpClientTesting(),
76 ],
77 }).compileComponents()
78
79 fixture = TestBed.createComponent(PermissionsFilterDropdownComponent)
80 component = fixture.componentInstance
81 component.ownerFilterSet.subscribe(
82 (model) => (ownerFilterSetResult = model)
83 )
84 component.selectionModel = new PermissionsSelectionModel()
85
86 fixture.detectChanges()
87 })
88
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()
97 })
98
99 it('should support reset', () => {
100 component.setFilter(OwnerFilterType.OTHERS)
101 expect(component.selectionModel.ownerFilter).not.toEqual(
102 OwnerFilterType.NONE
103 )
104 component.reset()
105 expect(component.selectionModel.ownerFilter).toEqual(OwnerFilterType.NONE)
106 })
107
108 it('should toggle owner filter type when users selected', () => {
109 component.selectionModel.ownerFilter = OwnerFilterType.NONE
110
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)
115
116 // this would normally be done by select component
117 component.selectionModel.includeUsers = null
118 component.onUserSelect()
119
120 expect(component.selectionModel.ownerFilter).toEqual(OwnerFilterType.NONE)
121 })
122 it('should emit a selection model depending on the type of owner filter set', () => {
123 component.selectionModel.ownerFilter = OwnerFilterType.NONE
124
125 component.setFilter(OwnerFilterType.SELF)
126 expect(ownerFilterSetResult).toEqual({
127 excludeUsers: [],
128 hideUnowned: false,
129 includeUsers: [],
130 ownerFilter: OwnerFilterType.SELF,
131 userID: currentUserID,
132 })
133
134 component.setFilter(OwnerFilterType.NOT_SELF)
135 expect(ownerFilterSetResult).toEqual({
136 excludeUsers: [currentUserID],
137 hideUnowned: false,
138 includeUsers: [],
139 ownerFilter: OwnerFilterType.NOT_SELF,
140 userID: null,
141 })
142
143 component.setFilter(OwnerFilterType.NONE)
144 expect(ownerFilterSetResult).toEqual({
145 excludeUsers: [],
146 hideUnowned: false,
147 includeUsers: [],
148 ownerFilter: OwnerFilterType.NONE,
149 userID: null,
150 })
151
152 component.setFilter(OwnerFilterType.SHARED_BY_ME)
153 expect(ownerFilterSetResult).toEqual({
154 excludeUsers: [],
155 hideUnowned: false,
156 includeUsers: [],
157 ownerFilter: OwnerFilterType.SHARED_BY_ME,
158 userID: currentUserID,
159 })
160
161 component.setFilter(OwnerFilterType.UNOWNED)
162 expect(ownerFilterSetResult).toEqual({
163 excludeUsers: [],
164 hideUnowned: false,
165 includeUsers: [],
166 ownerFilter: OwnerFilterType.UNOWNED,
167 userID: null,
168 })
169 })
170 })