-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathentity-data.service.spec.ts
168 lines (141 loc) · 5.19 KB
/
entity-data.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { NgModule, Optional } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { createEntityDefinition, EntityDefinition } from '../entity-metadata/entity-definition';
import { EntityMetadata, EntityMetadataMap, ENTITY_METADATA_TOKEN } from '../entity-metadata/entity-metadata';
import { DefaultDataService, DefaultDataServiceFactory } from './default-data.service';
import { HttpUrlGenerator, EntityHttpResourceUrls } from './http-url-generator';
import { EntityDataService } from './entity-data.service';
import { EntityCollectionDataService } from './interfaces';
import { QueryParams } from './interfaces';
import { Update } from '../utils/ngrx-entity-models';
// region Test Helpers
///// Test Helpers /////
export class CustomDataService {
name: string;
constructor(name: string) {
this.name = name + ' CustomDataService';
}
}
export class Bazinga {
id: number;
wow: string;
}
export class BazingaDataService implements EntityCollectionDataService<Bazinga> {
name: string;
// TestBed bug requires `@Optional` even though http is always provided.
constructor(@Optional() private http: HttpClient) {
if (!http) {
throw new Error('Where is HttpClient?');
}
this.name = 'Bazinga custom data service';
}
add(entity: Bazinga): Observable<Bazinga> {
return this.bazinga();
}
delete(id: any): Observable<null> {
return this.bazinga();
}
getAll(): Observable<Bazinga[]> {
return this.bazinga();
}
getById(id: any): Observable<Bazinga> {
return this.bazinga();
}
getWithQuery(params: string | QueryParams): Observable<Bazinga[]> {
return this.bazinga();
}
update(update: Update<Bazinga>): Observable<Bazinga> {
return this.bazinga();
}
upsert(entity: Bazinga): Observable<Bazinga> {
return this.bazinga();
}
private bazinga(): any {
bazingaFail();
return undefined;
}
}
@NgModule({
providers: [BazingaDataService]
})
export class CustomDataServiceModule {
constructor(entityDataService: EntityDataService, bazingaService: BazingaDataService) {
entityDataService.registerService('Bazinga', bazingaService);
}
}
function bazingaFail() {
throw new Error('Bazinga! This method is not implemented.');
}
/** Test version always returns canned Hero resource base URLs */
class TestHttpUrlGenerator implements HttpUrlGenerator {
entityResource(entityName: string, root: string): string {
return 'api/hero/';
}
collectionResource(entityName: string, root: string): string {
return 'api/heroes/';
}
registerHttpResourceUrls(entityHttpResourceUrls: EntityHttpResourceUrls): void {}
}
// endregion
///// Tests begin ////
describe('EntityDataService', () => {
const nullHttp = {};
let entityDataService: EntityDataService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [CustomDataServiceModule],
providers: [
DefaultDataServiceFactory,
EntityDataService,
{ provide: HttpClient, useValue: nullHttp },
{ provide: HttpUrlGenerator, useClass: TestHttpUrlGenerator }
]
});
entityDataService = TestBed.get(EntityDataService);
});
describe('#getService', () => {
it('can create a data service for "Hero" entity', () => {
const service = entityDataService.getService('Hero');
expect(service).toBeDefined();
});
it('data service should be a DefaultDataService by default', () => {
const service = entityDataService.getService('Hero');
expect(service instanceof DefaultDataService).toBe(true);
});
it('gets the same service every time you ask for it', () => {
const service1 = entityDataService.getService('Hero');
const service2 = entityDataService.getService('Hero');
expect(service1).toBe(service2);
});
});
describe('#register...', () => {
it('can register a custom service for "Hero"', () => {
const customService: any = new CustomDataService('Hero');
entityDataService.registerService('Hero', customService);
const service = entityDataService.getService('Hero');
expect(service).toBe(customService);
});
it('can register multiple custom services at the same time', () => {
const customHeroService: any = new CustomDataService('Hero');
const customVillainService: any = new CustomDataService('Villain');
entityDataService.registerServices({
Hero: customHeroService,
Villain: customVillainService
});
let service = entityDataService.getService('Hero');
expect(service).toBe(customHeroService, 'custom Hero data service');
expect(service.name).toBe('Hero CustomDataService');
service = entityDataService.getService('Villain');
expect(service).toBe(customVillainService, 'custom Villain data service');
// Other services are still DefaultDataServices
service = entityDataService.getService('Foo');
expect(service.name).toBe('Foo DefaultDataService');
});
it('can register a custom service using a module import', () => {
const service = entityDataService.getService('Bazinga');
expect(service instanceof BazingaDataService).toBe(true);
});
});
});