cache.service.spec.ts 8.5 KB
Newer Older
1
2
3
import { HttpHeaderResponse } from '@angular/common/http';
import { inject, TestBed } from '@angular/core/testing';
import * as _ from 'lodash';
Guillaume Perréal's avatar
Guillaume Perréal committed
4
import { Observable } from 'rxjs';
5
6
import { catchError, map, mergeMap } from 'rxjs/operators';

Guillaume Perréal's avatar
Guillaume Perréal committed
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
import { ResourceCache } from './cache.service';

import { Collection, IRI, Resource } from '../types';

interface MyResource extends Resource {
    readonly '@id': IRI<MyResource>;
    readonly '@type': 'MyResource';
    value: string;
    value2?: string;
}

function iri(x: string): IRI<MyResource> {
    return x as any;
}

const MY_IRI = iri('/bla/a');
const OTHER_IRI = iri('/bla/B');
const VALUES: { [name: string]: MyResource } = {
    a: { '@id': MY_IRI, '@type': 'MyResource', value: 'foo' },
    b: { '@id': MY_IRI, '@type': 'MyResource', value: 'bar' },
    c: {
        '@id': MY_IRI,
        '@type': 'MyResource',
        value: 'bar',
        value2: 'quz',
    },
    d: { '@id': OTHER_IRI, '@type': 'MyResource', value: 'zig' },
};
let scheduler: MarbleTestScheduler<any>;
beforeEach(() => {
    scheduler = MarbleTestScheduler.create(VALUES, 'error');
});
39

Guillaume Perréal's avatar
Guillaume Perréal committed
40
41
42
43
44
45
describe('ResourceCache', () => {
    beforeEach(() =>
        TestBed.configureTestingModule({
            providers: [ResourceCache],
        }),
    );
46

Guillaume Perréal's avatar
Guillaume Perréal committed
47
48
49
    it('should be created', inject([ResourceCache], (service: ResourceCache) => {
        expect(service).toBeDefined();
    }));
50

Guillaume Perréal's avatar
Guillaume Perréal committed
51
52
53
54
    describe('.get()', () => {
        it('should provide the value', inject([ResourceCache], (service: ResourceCache) =>
            scheduler.run(({ cold, expectObservable }) => {
                const getQuery$ = cold('a|');
55

Guillaume Perréal's avatar
Guillaume Perréal committed
56
57
58
59
60
61
62
63
64
65
66
67
                expectObservable(service.get(MY_IRI, () => getQuery$)).toBe('(a|)');
            }),
        ));

        it('should cache the value', inject([ResourceCache], (service: ResourceCache) =>
            scheduler.run(({ cold, expectObservable }) => {
                const getQuery$ = cold('a|');
                const getQuery2$ = cold('b|');
                // tslint:disable-next-line:rxjs-finnish
                const queries$ = cold('ab|', {
                    a: getQuery$,
                    b: getQuery2$,
68
69
                });

Guillaume Perréal's avatar
Guillaume Perréal committed
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
                expectObservable(queries$.pipe(mergeMap((query$) => service.get(MY_IRI, () => query$)))).toBe('aa|');
            }),
        ));

        it('should propagate errors', inject([ResourceCache], (service: ResourceCache) =>
            scheduler.run(({ cold, expectObservable }) => {
                const getQuery$ = cold('#');

                expectObservable(service.get(MY_IRI, () => getQuery$)).toBe('#');
            }),
        ));
    });

    describe('.put()', () => {
        it('should provide the value', inject([ResourceCache], (service: ResourceCache) => {
            const putRequest$ = scheduler.createColdObservable('a|');

            scheduler.expectObservable(service.put(MY_IRI, putRequest$)).toBe('a|');
        }));

        it('should not cache the value', inject([ResourceCache], (service: ResourceCache) =>
            scheduler.run(({ cold, expectObservable }) => {
                const putRequest$ = cold('a|');
                const putRequest2$ = cold('b|');
                // tslint:disable-next-line:rxjs-finnish
                const requests$ = cold('ab|', {
                    a: putRequest$,
                    b: putRequest2$,
98
                });
Guillaume Perréal's avatar
Guillaume Perréal committed
99
100
101
102
103
104
105
106
107
108
109

                expectObservable(
                    requests$.pipe(
                        mergeMap((request$: Observable<MyResource>) => service.put(MY_IRI, request$)),
                        map((x) => _.clone(x)),
                    ),
                ).toBe('ab|');
            }),
        ));

        it('should propagate errors', inject([ResourceCache], (service: ResourceCache) =>
110
            scheduler.run(({ cold, expectObservable }) => {
Guillaume Perréal's avatar
Guillaume Perréal committed
111
112
113
114
115
                const putRequest$ = cold('#');

                expectObservable(service.put(MY_IRI, putRequest$)).toBe('#');
            }),
        ));
116
117
    });

Guillaume Perréal's avatar
Guillaume Perréal committed
118
119
120
121
122
123
    describe('.post()', () => {
        it('should provide the value', inject([ResourceCache], (service: ResourceCache) =>
            scheduler.run(({ cold, expectObservable }) => {
                const postRequest$ = cold('a|');

                expectObservable(service.post(postRequest$)).toBe('a|');
124
            }),
Guillaume Perréal's avatar
Guillaume Perréal committed
125
        ));
126

Guillaume Perréal's avatar
Guillaume Perréal committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
        it('should propagate errors', inject([ResourceCache], (service: ResourceCache) =>
            scheduler.run(({ cold, expectObservable }) => {
                const postRequest$ = cold('#');

                expectObservable(service.post(postRequest$)).toBe('#');
            }),
        ));
    });

    describe('.delete()', () => {
        it('should clear the cache on successful fetch', inject([ResourceCache], (service: ResourceCache) => {
            const response = new HttpHeaderResponse({ status: 200 });
            const values = { r: response, a: VALUES.a, b: VALUES.b };
            const sched = scheduler.withValues(values);

            sched.run(async ({ cold, expectObservable }) => {
                await service.get(MY_IRI, () => cold('a|')).toPromise();
                await service.delete(MY_IRI, cold('r|')).toPromise();

                return expectObservable(service.get(MY_IRI, () => cold('b|'))).toBe('(b|)');
            });
148
149
        }));

Guillaume Perréal's avatar
Guillaume Perréal committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        it('should propagate errors', inject([ResourceCache], (service: ResourceCache) =>
            scheduler.run(({ cold, expectObservable }) => {
                const deleteRequest$ = cold('#');

                expectObservable(service.delete(MY_IRI, deleteRequest$)).toBe('#');
            }),
        ));

        it('should not clear the cache on error', inject([ResourceCache], (service: ResourceCache) => {
            const error = new HttpHeaderResponse({ status: 500 });
            const values = { a: VALUES.a, b: VALUES.b, e: error };
            scheduler
                .withValues(values)
                .withError(error)
                .run(async ({ cold, expectObservable }) => {
165
                    await service.get(MY_IRI, () => cold('a|')).toPromise();
Guillaume Perréal's avatar
Guillaume Perréal committed
166
167
168
169
                    await service
                        .delete(MY_IRI, cold('#'))
                        .pipe(catchError((e) => e))
                        .toPromise();
170

Guillaume Perréal's avatar
Guillaume Perréal committed
171
                    return expectObservable(service.get(MY_IRI, () => cold('b|'))).toBe('(a|)');
172
                });
Guillaume Perréal's avatar
Guillaume Perréal committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        }));
    });

    describe('.getAll()', () => {
        it('should provide the returned value', inject([ResourceCache], (service: ResourceCache) => {
            const values = {
                a: {
                    'hydra:member': [VALUES.a, VALUES.d],
                    'hydra:totalItems': 2,
                },
            };
            scheduler.withValues(values).run(({ cold, expectObservable }) => {
                const getAllRequest$ = cold('a|');
                expectObservable(service.getAll(getAllRequest$)).toBe('a|');
            });
        }));

        it('should nicely handle empty collections', inject([ResourceCache], (service: ResourceCache) => {
            const values = {
                a: { 'hydra:member': [] as any, 'hydra:totalItems': 0 },
            };
            scheduler.withValues(values).run(({ cold, expectObservable }) => {
                const getAllRequest$ = cold('a|');
                expectObservable(service.getAll(getAllRequest$)).toBe('a|');
            });
        }));

        it('should propagate errors', inject([ResourceCache], (service: ResourceCache) =>
            scheduler.run(({ cold, expectObservable }) => {
                const getAllRequest$ = cold<Collection<MyResource>>('#');
                expectObservable(service.getAll(getAllRequest$)).toBe('#');
            }),
        ));

        it('should populate the cache', inject([ResourceCache], (service: ResourceCache) => {
            const values = {
                a: VALUES.a,
                b: VALUES.d,
                h: {
                    'hydra:member': [VALUES.a, VALUES.d],
                    'hydra:totalItems': 2,
                },
            };
            scheduler.withValues(values).run(({ cold, expectObservable }) => {
                const getAllRequest$ = cold('h|');
                const getRequest$ = cold('b|');
                const requests$ = cold<() => Observable<any>>('ab|', {
                    a: () => service.getAll(getAllRequest$),
                    b: () => service.get(MY_IRI, () => getRequest$),
222
                });
Guillaume Perréal's avatar
Guillaume Perréal committed
223
224
225
226
227
228

                expectObservable(requests$.pipe(mergeMap((sendRequest: () => Observable<any>) => sendRequest()))).toBe(
                    'ha|',
                );
            });
        }));
229
230
    });
});