|
| 1 | +import { STATUS } from 'angular-in-memory-web-api'; |
| 2 | + |
| 3 | +import { |
| 4 | + DaffInMemoryBackendInterface, |
| 5 | + DaffInMemoryDataServiceInterface, |
| 6 | +} from '@daffodil/driver/in-memory'; |
| 7 | + |
| 8 | +import { DaffInMemoryBackendDelegate } from './delegate.service'; |
| 9 | + |
| 10 | +describe('@daffodil/driver/in-memory', () => { |
| 11 | + let service: DaffInMemoryBackendDelegate; |
| 12 | + let backends: Array<jasmine.SpyObj<DaffInMemoryBackendInterface>>; |
| 13 | + let methods: Array<keyof DaffInMemoryDataServiceInterface>; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + methods = ['get', 'put', 'post', 'delete']; |
| 17 | + backends = [ |
| 18 | + jasmine.createSpyObj('a', methods, { |
| 19 | + collectionName: 'a', |
| 20 | + }), |
| 21 | + jasmine.createSpyObj('b', methods, { |
| 22 | + collectionName: 'b', |
| 23 | + }), |
| 24 | + jasmine.createSpyObj('c', [], { |
| 25 | + collectionName: 'c', |
| 26 | + }), |
| 27 | + ]; |
| 28 | + service = new DaffInMemoryBackendDelegate(backends); |
| 29 | + }); |
| 30 | + |
| 31 | + // eslint-disable-next-line guard-for-in |
| 32 | + for (const method in methods) { |
| 33 | + describe(method, () => { |
| 34 | + it('should delegate requests to the correct backend', () => { |
| 35 | + const reqInfo: any = { |
| 36 | + collectionName: 'a', |
| 37 | + }; |
| 38 | + service[method](reqInfo); |
| 39 | + expect(backends[0][method]).toHaveBeenCalledWith(reqInfo); |
| 40 | + reqInfo.collectionName = 'b'; |
| 41 | + service[method](reqInfo); |
| 42 | + expect(backends[1][method]).toHaveBeenCalledWith(reqInfo); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should return a "not found" if the method is missing from the backend', (done) => { |
| 46 | + const reqInfo: any = { |
| 47 | + collectionName: 'c', |
| 48 | + }; |
| 49 | + service[method](reqInfo).subscribe((res) => { |
| 50 | + expect(res.status).toEqual(STATUS.NOT_FOUND); |
| 51 | + done(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should return a "not found" if there isn\'t a backend for the collection name', (done) => { |
| 56 | + const reqInfo: any = { |
| 57 | + collectionName: '404backendnotfound', |
| 58 | + }; |
| 59 | + service[method](reqInfo).subscribe((res) => { |
| 60 | + expect(res.status).toEqual(STATUS.NOT_FOUND); |
| 61 | + done(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + }); |
| 65 | + } |
| 66 | +}); |
0 commit comments