|
| 1 | +import { |
| 2 | + AccessibilitySettingsService, |
| 3 | + AccessibilitySetting, |
| 4 | + AccessibilitySettings, |
| 5 | + ACCESSIBILITY_SETTINGS_METADATA_KEY, |
| 6 | + ACCESSIBILITY_COOKIE |
| 7 | +} from './accessibility-settings.service'; |
| 8 | +import { CookieService } from '../core/services/cookie.service'; |
| 9 | +import { AuthService } from '../core/auth/auth.service'; |
| 10 | +import { EPersonDataService } from '../core/eperson/eperson-data.service'; |
| 11 | +import { CookieServiceMock } from '../shared/mocks/cookie.service.mock'; |
| 12 | +import { AuthServiceStub } from '../shared/testing/auth-service.stub'; |
| 13 | +import { of } from 'rxjs'; |
| 14 | +import { EPerson } from '../core/eperson/models/eperson.model'; |
| 15 | +import { fakeAsync, flush } from '@angular/core/testing'; |
| 16 | +import { createSuccessfulRemoteDataObject$, createFailedRemoteDataObject$ } from '../shared/remote-data.utils'; |
| 17 | + |
| 18 | + |
| 19 | +describe('accessibilitySettingsService', () => { |
| 20 | + let service: AccessibilitySettingsService; |
| 21 | + let cookieService: CookieServiceMock; |
| 22 | + let authService: AuthServiceStub; |
| 23 | + let ePersonService: EPersonDataService; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + cookieService = new CookieServiceMock(); |
| 27 | + authService = new AuthServiceStub(); |
| 28 | + |
| 29 | + ePersonService = jasmine.createSpyObj('ePersonService', { |
| 30 | + createPatchFromCache: of([{ |
| 31 | + op: 'add', |
| 32 | + value: null, |
| 33 | + }]), |
| 34 | + patch: of({}), |
| 35 | + }); |
| 36 | + |
| 37 | + service = new AccessibilitySettingsService( |
| 38 | + cookieService as unknown as CookieService, |
| 39 | + authService as unknown as AuthService, |
| 40 | + ePersonService, |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('getALlAccessibilitySettingsKeys', () => { |
| 45 | + it('should return an array containing all accessibility setting names', () => { |
| 46 | + const settingNames: AccessibilitySetting[] = [ |
| 47 | + AccessibilitySetting.NotificationTimeOut, |
| 48 | + AccessibilitySetting.LiveRegionTimeOut, |
| 49 | + ]; |
| 50 | + |
| 51 | + expect(service.getAllAccessibilitySettingKeys()).toEqual(settingNames); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('get', () => { |
| 56 | + it('should return the setting if it is set', () => { |
| 57 | + const settings: AccessibilitySettings = { |
| 58 | + notificationTimeOut: '1000', |
| 59 | + }; |
| 60 | + |
| 61 | + service.getAll = jasmine.createSpy('getAll').and.returnValue(of(settings)); |
| 62 | + |
| 63 | + service.get(AccessibilitySetting.NotificationTimeOut, 'default').subscribe(value => |
| 64 | + expect(value).toEqual('1000') |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return the default value if the setting is not set', () => { |
| 69 | + const settings: AccessibilitySettings = { |
| 70 | + notificationTimeOut: '1000', |
| 71 | + }; |
| 72 | + |
| 73 | + service.getAll = jasmine.createSpy('getAll').and.returnValue(of(settings)); |
| 74 | + |
| 75 | + service.get(AccessibilitySetting.LiveRegionTimeOut, 'default').subscribe(value => |
| 76 | + expect(value).toEqual('default') |
| 77 | + ); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('getAsNumber', () => { |
| 82 | + it('should return the setting as number if the value for the setting can be parsed to a number', () => { |
| 83 | + service.get = jasmine.createSpy('get').and.returnValue(of('1000')); |
| 84 | + |
| 85 | + service.getAsNumber(AccessibilitySetting.NotificationTimeOut).subscribe(value => |
| 86 | + expect(value).toEqual(1000) |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should return the default value if no value is set for the setting', () => { |
| 91 | + service.get = jasmine.createSpy('get').and.returnValue(of(null)); |
| 92 | + |
| 93 | + service.getAsNumber(AccessibilitySetting.NotificationTimeOut, 123).subscribe(value => |
| 94 | + expect(value).toEqual(123) |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return the default value if the value for the setting can not be parsed to a number', () => { |
| 99 | + service.get = jasmine.createSpy('get').and.returnValue(of('text')); |
| 100 | + |
| 101 | + service.getAsNumber(AccessibilitySetting.NotificationTimeOut, 123).subscribe(value => |
| 102 | + expect(value).toEqual(123) |
| 103 | + ); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('getAll', () => { |
| 108 | + it('should attempt to get the settings from metadata first', () => { |
| 109 | + service.getAllSettingsFromAuthenticatedUserMetadata = |
| 110 | + jasmine.createSpy('getAllSettingsFromAuthenticatedUserMetadata').and.returnValue(of({ })); |
| 111 | + |
| 112 | + service.getAll().subscribe(); |
| 113 | + expect(service.getAllSettingsFromAuthenticatedUserMetadata).toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should attempt to get the settings from the cookie if the settings from metadata are empty', () => { |
| 117 | + service.getAllSettingsFromAuthenticatedUserMetadata = |
| 118 | + jasmine.createSpy('getAllSettingsFromAuthenticatedUserMetadata').and.returnValue(of({ })); |
| 119 | + |
| 120 | + service.getAllSettingsFromCookie = jasmine.createSpy('getAllSettingsFromCookie').and.returnValue({ }); |
| 121 | + |
| 122 | + service.getAll().subscribe(); |
| 123 | + expect(service.getAllSettingsFromCookie).toHaveBeenCalled(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should not attempt to get the settings from the cookie if the settings from metadata are not empty', () => { |
| 127 | + const settings: AccessibilitySettings = { |
| 128 | + notificationTimeOut: '1000', |
| 129 | + }; |
| 130 | + |
| 131 | + service.getAllSettingsFromAuthenticatedUserMetadata = |
| 132 | + jasmine.createSpy('getAllSettingsFromAuthenticatedUserMetadata').and.returnValue(of(settings)); |
| 133 | + |
| 134 | + service.getAllSettingsFromCookie = jasmine.createSpy('getAllSettingsFromCookie').and.returnValue({ }); |
| 135 | + |
| 136 | + service.getAll().subscribe(); |
| 137 | + expect(service.getAllSettingsFromCookie).not.toHaveBeenCalled(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should return an empty object if both are empty', () => { |
| 141 | + service.getAllSettingsFromAuthenticatedUserMetadata = |
| 142 | + jasmine.createSpy('getAllSettingsFromAuthenticatedUserMetadata').and.returnValue(of({ })); |
| 143 | + |
| 144 | + service.getAllSettingsFromCookie = jasmine.createSpy('getAllSettingsFromCookie').and.returnValue({ }); |
| 145 | + |
| 146 | + service.getAll().subscribe(value => expect(value).toEqual({})); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('getAllSettingsFromCookie', () => { |
| 151 | + it('should retrieve the settings from the cookie', () => { |
| 152 | + cookieService.get = jasmine.createSpy(); |
| 153 | + |
| 154 | + service.getAllSettingsFromCookie(); |
| 155 | + expect(cookieService.get).toHaveBeenCalledWith(ACCESSIBILITY_COOKIE); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + describe('getAllSettingsFromAuthenticatedUserMetadata', () => { |
| 160 | + it('should retrieve all settings from the user\'s metadata', () => { |
| 161 | + const settings = { 'liveRegionTimeOut': '1000' }; |
| 162 | + |
| 163 | + const user = new EPerson(); |
| 164 | + user.setMetadata(ACCESSIBILITY_SETTINGS_METADATA_KEY, null, JSON.stringify(settings)); |
| 165 | + |
| 166 | + authService.getAuthenticatedUserFromStoreIfAuthenticated = |
| 167 | + jasmine.createSpy('getAuthenticatedUserFromStoreIfAuthenticated').and.returnValue(of(user)); |
| 168 | + |
| 169 | + service.getAllSettingsFromAuthenticatedUserMetadata().subscribe(value => |
| 170 | + expect(value).toEqual(settings) |
| 171 | + ); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + describe('set', () => { |
| 176 | + it('should correctly update the chosen setting', () => { |
| 177 | + service.updateSettings = jasmine.createSpy('updateSettings'); |
| 178 | + |
| 179 | + service.set(AccessibilitySetting.LiveRegionTimeOut, '500'); |
| 180 | + expect(service.updateSettings).toHaveBeenCalledWith({ liveRegionTimeOut: '500' }); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('setSettings', () => { |
| 185 | + beforeEach(() => { |
| 186 | + service.setSettingsInCookie = jasmine.createSpy('setSettingsInCookie'); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should attempt to set settings in metadata', () => { |
| 190 | + service.setSettingsInAuthenticatedUserMetadata = |
| 191 | + jasmine.createSpy('setSettingsInAuthenticatedUserMetadata').and.returnValue(of(false)); |
| 192 | + |
| 193 | + const settings: AccessibilitySettings = { |
| 194 | + notificationTimeOut: '1000', |
| 195 | + }; |
| 196 | + |
| 197 | + service.setSettings(settings).subscribe(); |
| 198 | + expect(service.setSettingsInAuthenticatedUserMetadata).toHaveBeenCalledWith(settings); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should set settings in cookie if metadata failed', () => { |
| 202 | + service.setSettingsInAuthenticatedUserMetadata = |
| 203 | + jasmine.createSpy('setSettingsInAuthenticatedUserMetadata').and.returnValue(of(false)); |
| 204 | + |
| 205 | + const settings: AccessibilitySettings = { |
| 206 | + notificationTimeOut: '1000', |
| 207 | + }; |
| 208 | + |
| 209 | + service.setSettings(settings).subscribe(); |
| 210 | + expect(service.setSettingsInCookie).toHaveBeenCalled(); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should not set settings in cookie if metadata succeeded', () => { |
| 214 | + service.setSettingsInAuthenticatedUserMetadata = |
| 215 | + jasmine.createSpy('setSettingsInAuthenticatedUserMetadata').and.returnValue(of(true)); |
| 216 | + |
| 217 | + const settings: AccessibilitySettings = { |
| 218 | + notificationTimeOut: '1000', |
| 219 | + }; |
| 220 | + |
| 221 | + service.setSettings(settings).subscribe(); |
| 222 | + expect(service.setSettingsInCookie).not.toHaveBeenCalled(); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should return \'metadata\' if settings are stored in metadata', () => { |
| 226 | + service.setSettingsInAuthenticatedUserMetadata = |
| 227 | + jasmine.createSpy('setSettingsInAuthenticatedUserMetadata').and.returnValue(of(true)); |
| 228 | + |
| 229 | + const settings: AccessibilitySettings = { |
| 230 | + notificationTimeOut: '1000', |
| 231 | + }; |
| 232 | + |
| 233 | + service.setSettings(settings).subscribe(value => |
| 234 | + expect(value).toEqual('metadata') |
| 235 | + ); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should return \'cookie\' if settings are stored in cookie', () => { |
| 239 | + service.setSettingsInAuthenticatedUserMetadata = |
| 240 | + jasmine.createSpy('setSettingsInAuthenticatedUserMetadata').and.returnValue(of(false)); |
| 241 | + |
| 242 | + const settings: AccessibilitySettings = { |
| 243 | + notificationTimeOut: '1000', |
| 244 | + }; |
| 245 | + |
| 246 | + service.setSettings(settings).subscribe(value => |
| 247 | + expect(value).toEqual('cookie') |
| 248 | + ); |
| 249 | + }); |
| 250 | + }); |
| 251 | + |
| 252 | + describe('updateSettings', () => { |
| 253 | + it('should call setSettings with the updated settings', () => { |
| 254 | + const beforeSettings: AccessibilitySettings = { |
| 255 | + notificationTimeOut: '1000', |
| 256 | + }; |
| 257 | + |
| 258 | + service.getAll = jasmine.createSpy('getAll').and.returnValue(of(beforeSettings)); |
| 259 | + service.setSettings = jasmine.createSpy('setSettings').and.returnValue(of('cookie')); |
| 260 | + |
| 261 | + const newSettings: AccessibilitySettings = { |
| 262 | + liveRegionTimeOut: '2000', |
| 263 | + }; |
| 264 | + |
| 265 | + const combinedSettings: AccessibilitySettings = { |
| 266 | + notificationTimeOut: '1000', |
| 267 | + liveRegionTimeOut: '2000', |
| 268 | + }; |
| 269 | + |
| 270 | + service.updateSettings(newSettings).subscribe(); |
| 271 | + expect(service.setSettings).toHaveBeenCalledWith(combinedSettings); |
| 272 | + }); |
| 273 | + }); |
| 274 | + |
| 275 | + describe('setSettingsInAuthenticatedUserMetadata', () => { |
| 276 | + beforeEach(() => { |
| 277 | + service.setSettingsInMetadata = jasmine.createSpy('setSettingsInMetadata').and.returnValue(of(null)); |
| 278 | + }); |
| 279 | + |
| 280 | + it('should store settings in metadata when the user is authenticated', fakeAsync(() => { |
| 281 | + const user = new EPerson(); |
| 282 | + authService.getAuthenticatedUserFromStoreIfAuthenticated = jasmine.createSpy().and.returnValue(of(user)); |
| 283 | + |
| 284 | + service.setSettingsInAuthenticatedUserMetadata({}).subscribe(); |
| 285 | + flush(); |
| 286 | + |
| 287 | + expect(service.setSettingsInMetadata).toHaveBeenCalled(); |
| 288 | + })); |
| 289 | + |
| 290 | + it('should emit false when the user is not authenticated', fakeAsync(() => { |
| 291 | + authService.getAuthenticatedUserFromStoreIfAuthenticated = jasmine.createSpy().and.returnValue(of(null)); |
| 292 | + |
| 293 | + service.setSettingsInAuthenticatedUserMetadata({}) |
| 294 | + .subscribe(value => expect(value).toBeFalse()); |
| 295 | + flush(); |
| 296 | + |
| 297 | + expect(service.setSettingsInMetadata).not.toHaveBeenCalled(); |
| 298 | + })); |
| 299 | + }); |
| 300 | + |
| 301 | + describe('setSettingsInMetadata', () => { |
| 302 | + const ePerson = new EPerson(); |
| 303 | + |
| 304 | + beforeEach(() => { |
| 305 | + ePerson.setMetadata = jasmine.createSpy('setMetadata'); |
| 306 | + ePerson.removeMetadata = jasmine.createSpy('removeMetadata'); |
| 307 | + }); |
| 308 | + |
| 309 | + it('should set the settings in metadata', () => { |
| 310 | + service.setSettingsInMetadata(ePerson, { [AccessibilitySetting.LiveRegionTimeOut]: '500' }).subscribe(); |
| 311 | + expect(ePerson.setMetadata).toHaveBeenCalled(); |
| 312 | + }); |
| 313 | + |
| 314 | + it('should remove the metadata when the settings are emtpy', () => { |
| 315 | + service.setSettingsInMetadata(ePerson, {}).subscribe(); |
| 316 | + expect(ePerson.setMetadata).not.toHaveBeenCalled(); |
| 317 | + expect(ePerson.removeMetadata).toHaveBeenCalled(); |
| 318 | + }); |
| 319 | + |
| 320 | + it('should create a patch with the metadata changes', () => { |
| 321 | + service.setSettingsInMetadata(ePerson, { [AccessibilitySetting.LiveRegionTimeOut]: '500' }).subscribe(); |
| 322 | + expect(ePersonService.createPatchFromCache).toHaveBeenCalled(); |
| 323 | + }); |
| 324 | + |
| 325 | + it('should send the patch request', () => { |
| 326 | + service.setSettingsInMetadata(ePerson, { [AccessibilitySetting.LiveRegionTimeOut]: '500' }).subscribe(); |
| 327 | + expect(ePersonService.patch).toHaveBeenCalled(); |
| 328 | + }); |
| 329 | + |
| 330 | + it('should emit true when the update succeeded', fakeAsync(() => { |
| 331 | + ePersonService.patch = jasmine.createSpy().and.returnValue(createSuccessfulRemoteDataObject$({})); |
| 332 | + |
| 333 | + service.setSettingsInMetadata(ePerson, { [AccessibilitySetting.LiveRegionTimeOut]: '500' }) |
| 334 | + .subscribe(value => { |
| 335 | + expect(value).toBeTrue(); |
| 336 | + }); |
| 337 | + |
| 338 | + flush(); |
| 339 | + })); |
| 340 | + |
| 341 | + it('should emit false when the update failed', fakeAsync(() => { |
| 342 | + ePersonService.patch = jasmine.createSpy().and.returnValue(createFailedRemoteDataObject$()); |
| 343 | + |
| 344 | + service.setSettingsInMetadata(ePerson, { [AccessibilitySetting.LiveRegionTimeOut]: '500' }) |
| 345 | + .subscribe(value => { |
| 346 | + expect(value).toBeFalse(); |
| 347 | + }); |
| 348 | + |
| 349 | + flush(); |
| 350 | + })); |
| 351 | + }); |
| 352 | + |
| 353 | + describe('setSettingsInCookie', () => { |
| 354 | + beforeEach(() => { |
| 355 | + cookieService.set = jasmine.createSpy('set'); |
| 356 | + cookieService.remove = jasmine.createSpy('remove'); |
| 357 | + }); |
| 358 | + |
| 359 | + it('should store the settings in a cookie', () => { |
| 360 | + service.setSettingsInCookie({ [AccessibilitySetting.LiveRegionTimeOut]: '500' }); |
| 361 | + expect(cookieService.set).toHaveBeenCalled(); |
| 362 | + }); |
| 363 | + |
| 364 | + it('should remove the cookie when the settings are empty', () => { |
| 365 | + service.setSettingsInCookie({}); |
| 366 | + expect(cookieService.set).not.toHaveBeenCalled(); |
| 367 | + expect(cookieService.remove).toHaveBeenCalled(); |
| 368 | + }); |
| 369 | + }); |
| 370 | + |
| 371 | + describe('getInputType', () => { |
| 372 | + it('should correctly return the input type', () => { |
| 373 | + expect(service.getInputType(AccessibilitySetting.NotificationTimeOut)).toEqual('number'); |
| 374 | + expect(service.getInputType(AccessibilitySetting.LiveRegionTimeOut)).toEqual('number'); |
| 375 | + expect(service.getInputType('unknownValue' as AccessibilitySetting)).toEqual('text'); |
| 376 | + }); |
| 377 | + }); |
| 378 | + |
| 379 | +}); |
0 commit comments