Commits
Alexandre Vryghem authored fb66b5abd6b
1 + | import { TestBed } from '@angular/core/testing'; |
1 2 | import { BitstreamDataService } from './bitstream-data.service'; |
2 3 | import { ObjectCacheService } from '../cache/object-cache.service'; |
3 4 | import { RequestService } from './request.service'; |
4 5 | import { Bitstream } from '../shared/bitstream.model'; |
5 6 | import { HALEndpointService } from '../shared/hal-endpoint.service'; |
6 7 | import { BitstreamFormatDataService } from './bitstream-format-data.service'; |
7 - | import { of as observableOf } from 'rxjs'; |
8 + | import { Observable, of as observableOf } from 'rxjs'; |
8 9 | import { BitstreamFormat } from '../shared/bitstream-format.model'; |
9 10 | import { BitstreamFormatSupportLevel } from '../shared/bitstream-format-support-level'; |
10 - | import { PutRequest } from './request.models'; |
11 + | import { PatchRequest, PutRequest } from './request.models'; |
11 12 | import { getMockRequestService } from '../../shared/mocks/request.service.mock'; |
12 13 | import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub'; |
13 14 | import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; |
14 15 | import { getMockRemoteDataBuildService } from '../../shared/mocks/remote-data-build.service.mock'; |
15 16 | import { testSearchDataImplementation } from './base/search-data.spec'; |
16 17 | import { testPatchDataImplementation } from './base/patch-data.spec'; |
17 18 | import { testDeleteDataImplementation } from './base/delete-data.spec'; |
19 + | import { DSOChangeAnalyzer } from './dso-change-analyzer.service'; |
20 + | import { NotificationsService } from '../../shared/notifications/notifications.service'; |
21 + | import objectContaining = jasmine.objectContaining; |
22 + | import { RemoteData } from './remote-data'; |
23 + | import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model'; |
18 24 | |
19 25 | describe('BitstreamDataService', () => { |
20 26 | let service: BitstreamDataService; |
21 27 | let objectCache: ObjectCacheService; |
22 28 | let requestService: RequestService; |
23 29 | let halService: HALEndpointService; |
24 30 | let bitstreamFormatService: BitstreamFormatDataService; |
25 31 | let rdbService: RemoteDataBuildService; |
26 32 | const bitstreamFormatHref = 'rest-api/bitstreamformats'; |
27 33 | |
28 - | const bitstream = Object.assign(new Bitstream(), { |
29 - | uuid: 'fake-bitstream', |
34 + | const bitstream1 = Object.assign(new Bitstream(), { |
35 + | id: 'fake-bitstream1', |
36 + | uuid: 'fake-bitstream1', |
30 37 | _links: { |
31 - | self: { href: 'fake-bitstream-self' } |
38 + | self: { href: 'fake-bitstream1-self' } |
39 + | } |
40 + | }); |
41 + | const bitstream2 = Object.assign(new Bitstream(), { |
42 + | id: 'fake-bitstream2', |
43 + | uuid: 'fake-bitstream2', |
44 + | _links: { |
45 + | self: { href: 'fake-bitstream2-self' } |
32 46 | } |
33 47 | }); |
34 48 | const format = Object.assign(new BitstreamFormat(), { |
35 49 | id: '2', |
36 50 | shortDescription: 'PNG', |
37 51 | description: 'Portable Network Graphics', |
38 52 | supportLevel: BitstreamFormatSupportLevel.Known |
39 53 | }); |
40 54 | const url = 'fake-bitstream-url'; |
41 55 | |
42 56 | beforeEach(() => { |
43 57 | objectCache = jasmine.createSpyObj('objectCache', { |
44 58 | remove: jasmine.createSpy('remove') |
45 59 | }); |
46 60 | requestService = getMockRequestService(); |
47 61 | halService = Object.assign(new HALEndpointServiceStub(url)); |
48 62 | bitstreamFormatService = jasmine.createSpyObj('bistreamFormatService', { |
49 63 | getBrowseEndpoint: observableOf(bitstreamFormatHref) |
50 64 | }); |
51 65 | rdbService = getMockRemoteDataBuildService(); |
52 66 | |
53 - | service = new BitstreamDataService(requestService, rdbService, objectCache, halService, null, bitstreamFormatService, null, null); |
67 + | TestBed.configureTestingModule({ |
68 + | providers: [ |
69 + | { provide: ObjectCacheService, useValue: objectCache }, |
70 + | { provide: RequestService, useValue: requestService }, |
71 + | { provide: HALEndpointService, useValue: halService }, |
72 + | { provide: BitstreamFormatDataService, useValue: bitstreamFormatService }, |
73 + | { provide: RemoteDataBuildService, useValue: rdbService }, |
74 + | { provide: DSOChangeAnalyzer, useValue: {} }, |
75 + | { provide: NotificationsService, useValue: {} }, |
76 + | ], |
77 + | }); |
78 + | service = TestBed.inject(BitstreamDataService); |
54 79 | }); |
55 80 | |
56 81 | describe('composition', () => { |
57 82 | const initService = () => new BitstreamDataService(null, null, null, null, null, null, null, null); |
58 83 | testSearchDataImplementation(initService); |
59 84 | testPatchDataImplementation(initService); |
60 85 | testDeleteDataImplementation(initService); |
61 86 | }); |
62 87 | |
63 88 | describe('when updating the bitstream\'s format', () => { |
64 89 | beforeEach(() => { |
65 - | service.updateFormat(bitstream, format); |
90 + | service.updateFormat(bitstream1, format); |
66 91 | }); |
67 92 | |
68 93 | it('should send a put request', () => { |
69 94 | expect(requestService.send).toHaveBeenCalledWith(jasmine.any(PutRequest)); |
70 95 | }); |
71 96 | }); |
97 + | |
98 + | describe('removeMultiple', () => { |
99 + | function mockBuildFromRequestUUIDAndAwait(requestUUID$: string | Observable<string>, callback: (rd?: RemoteData<any>) => Observable<unknown>, _linksToFollow: FollowLinkConfig<any>[]): Observable<RemoteData<any>> { |
100 + | callback(); |
101 + | return; |
102 + | } |
103 + | |
104 + | beforeEach(() => { |
105 + | spyOn(service, 'invalidateByHref'); |
106 + | spyOn(rdbService, 'buildFromRequestUUIDAndAwait').and.callFake((requestUUID$: string | Observable<string>, callback: (rd?: RemoteData<any>) => Observable<unknown>, linksToFollow: FollowLinkConfig<any>[]) => mockBuildFromRequestUUIDAndAwait(requestUUID$, callback, linksToFollow)); |
107 + | }); |
108 + | |
109 + | it('should be able to 1 bitstream', () => { |
110 + | service.removeMultiple([bitstream1]); |
111 + | |
112 + | expect(requestService.send).toHaveBeenCalledWith(objectContaining({ |
113 + | href: `${url}/bitstreams`, |
114 + | body: [ |
115 + | { op: 'remove', path: '/bitstreams/fake-bitstream1' }, |
116 + | ], |
117 + | } as PatchRequest)); |
118 + | expect(service.invalidateByHref).toHaveBeenCalledWith('fake-bitstream1-self'); |
119 + | }); |
120 + | |
121 + | it('should be able to delete multiple bitstreams', () => { |
122 + | service.removeMultiple([bitstream1, bitstream2]); |
123 + | |
124 + | expect(requestService.send).toHaveBeenCalledWith(objectContaining({ |
125 + | href: `${url}/bitstreams`, |
126 + | body: [ |
127 + | { op: 'remove', path: '/bitstreams/fake-bitstream1' }, |
128 + | { op: 'remove', path: '/bitstreams/fake-bitstream2' }, |
129 + | ], |
130 + | } as PatchRequest)); |
131 + | expect(service.invalidateByHref).toHaveBeenCalledWith('fake-bitstream1-self'); |
132 + | expect(service.invalidateByHref).toHaveBeenCalledWith('fake-bitstream2-self'); |
133 + | }); |
134 + | }); |
72 135 | }); |