|
| 1 | +module.exports = function testAdapter(options) { |
| 2 | + describe('micro-analytics adapter ' + options.name, () => { |
| 3 | + const adapter = require(options.modulePath); |
| 4 | + |
| 5 | + if (typeof options.beforeEach === 'function') { |
| 6 | + beforeEach(async () => { await options.beforeEach(adapter); }); |
| 7 | + } |
| 8 | + |
| 9 | + if (typeof options.afterEach === 'function') { |
| 10 | + afterEach(async () => { await options.afterEach(adapter); }); |
| 11 | + } |
| 12 | + |
| 13 | + if (typeof options.beforeAll === 'function') { |
| 14 | + beforeAll(async () => { await options.beforeAll(adapter); }); |
| 15 | + } |
| 16 | + |
| 17 | + if (typeof options.afterAll === 'function') { |
| 18 | + afterAll(async () => { |
| 19 | + await options.afterAll(adapter); |
| 20 | + if (typeof adapter.close === 'function') { |
| 21 | + await adapter.close(); |
| 22 | + } |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + it('should save and read', async () => { |
| 27 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 28 | + |
| 29 | + expect(await adapter.get('/a-key')).toEqual({ |
| 30 | + views: [{ time: 1490623474639 }], |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return all saves on getAll', async () => { |
| 35 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 36 | + await adapter.put('/another-key', { views: [{ time: 1490623474639 }] }); |
| 37 | + |
| 38 | + expect(await adapter.getAll({ pathname: '/' })).toEqual({ |
| 39 | + '/a-key': { views: [{ time: 1490623474639 }] }, |
| 40 | + '/another-key': { views: [{ time: 1490623474639 }] }, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should have check whether a key is stored with has', async () => { |
| 45 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 46 | + |
| 47 | + expect(await adapter.has('/a-key')).toEqual(true); |
| 48 | + expect(await adapter.has('/non-existing-key')).toEqual(false); |
| 49 | + }); |
| 50 | + |
| 51 | + if (typeof adapter.subscribe === "function") { |
| 52 | + |
| 53 | + it('should allow subscription with observables', async () => { |
| 54 | + const listener = jest.fn(); |
| 55 | + const unsubscribe = adapter.subscribe(listener); |
| 56 | + |
| 57 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 58 | + |
| 59 | + expect(listener).toHaveBeenCalledWith({ |
| 60 | + key: '/a-key', |
| 61 | + value: { views: [{ time: 1490623474639 }] }, |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should allow multiple subscription with observables and handle unsubscribption', async () => { |
| 66 | + const listener1 = jest.fn(); |
| 67 | + const listener2 = jest.fn(); |
| 68 | + const subscription = adapter.subscribe(listener1); |
| 69 | + adapter.subscribe(listener2); |
| 70 | + |
| 71 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 72 | + subscription.unsubscribe(); |
| 73 | + await adapter.put('/b-key', { views: [{ time: 1490623474639 }] }); |
| 74 | + |
| 75 | + expect(listener1).toHaveBeenCalledWith({ |
| 76 | + key: '/a-key', |
| 77 | + value: { views: [{ time: 1490623474639 }] }, |
| 78 | + }); |
| 79 | + expect(listener1).not.toHaveBeenCalledWith({ |
| 80 | + key: '/b-key', |
| 81 | + value: { views: [{ time: 1490623474639 }] }, |
| 82 | + }); |
| 83 | + expect(listener2).toHaveBeenCalledWith({ |
| 84 | + key: '/a-key', |
| 85 | + value: { views: [{ time: 1490623474639 }] }, |
| 86 | + }); |
| 87 | + expect(listener2).toHaveBeenCalledWith({ |
| 88 | + key: '/b-key', |
| 89 | + value: { views: [{ time: 1490623474639 }] }, |
| 90 | + }); |
| 91 | + }); |
| 92 | + } |
| 93 | + }); |
| 94 | +}; |
0 commit comments