|
1 | 1 | module.exports = function testAdapter(options) { |
2 | 2 | describe('micro-analytics adapter ' + options.name, () => { |
3 | | - const adapter = require(options.modulePath) |
| 3 | + const adapter = require(options.modulePath); |
4 | 4 |
|
5 | 5 | if (typeof options.beforeEach === 'function') { |
6 | 6 | beforeEach(async () => { |
7 | | - await options.beforeEach(adapter) |
8 | | - }) |
| 7 | + await options.beforeEach(adapter); |
| 8 | + }); |
9 | 9 | } |
10 | 10 |
|
11 | 11 | if (typeof options.afterEach === 'function') { |
12 | 12 | afterEach(async () => { |
13 | | - await options.afterEach(adapter) |
14 | | - }) |
| 13 | + await options.afterEach(adapter); |
| 14 | + }); |
15 | 15 | } |
16 | 16 |
|
17 | 17 | if (typeof options.beforeAll === 'function') { |
18 | 18 | beforeAll(async () => { |
19 | | - await options.beforeAll(adapter) |
20 | | - }) |
| 19 | + await options.beforeAll(adapter); |
| 20 | + }); |
21 | 21 | } |
22 | 22 |
|
23 | 23 | if (typeof options.afterAll === 'function') { |
24 | 24 | afterAll(async () => { |
25 | | - await options.afterAll(adapter) |
| 25 | + await options.afterAll(adapter); |
26 | 26 | if (typeof adapter.close === 'function') { |
27 | | - await adapter.close() |
| 27 | + await adapter.close(); |
28 | 28 | } |
29 | | - }) |
| 29 | + }); |
30 | 30 | } |
31 | 31 |
|
32 | 32 | test('get() should return a promise', () => { |
33 | | - expect(adapter.get('/a-key').constructor.name).toEqual('Promise') |
34 | | - }) |
| 33 | + expect(adapter.get('/a-key').constructor.name).toEqual('Promise'); |
| 34 | + }); |
35 | 35 |
|
36 | 36 | test('getAll() should return a promise', () => { |
37 | 37 | expect(adapter.getAll({ pathname: '/' }).constructor.name).toEqual( |
38 | 38 | 'Promise' |
39 | | - ) |
40 | | - }) |
| 39 | + ); |
| 40 | + }); |
41 | 41 |
|
42 | 42 | test('has() should return a promise', () => { |
43 | | - expect(adapter.has('/a-key').constructor.name).toEqual('Promise') |
44 | | - }) |
| 43 | + expect(adapter.has('/a-key').constructor.name).toEqual('Promise'); |
| 44 | + }); |
45 | 45 |
|
46 | 46 | test('keys() should return a promise', () => { |
47 | | - expect(adapter.keys().constructor.name).toEqual('Promise') |
48 | | - }) |
| 47 | + expect(adapter.keys().constructor.name).toEqual('Promise'); |
| 48 | + }); |
49 | 49 |
|
50 | 50 | test('put() should return a promise', () => { |
51 | | - expect(adapter.put('/a-key', {}).constructor.name).toEqual('Promise') |
52 | | - }) |
| 51 | + expect(adapter.put('/a-key', {}).constructor.name).toEqual('Promise'); |
| 52 | + }); |
53 | 53 |
|
54 | 54 | if (typeof adapter.options !== 'undefined') { |
55 | 55 | test('options should be an array of args options', () => { |
56 | | - expect(Array.isArray(adapter.options)).toBe(true) |
| 56 | + expect(Array.isArray(adapter.options)).toBe(true); |
57 | 57 | if (adapter.options.length >= 0) { |
58 | | - let counter = 0 |
| 58 | + let counter = 0; |
59 | 59 | adapter.options.forEach(option => { |
60 | | - expect(option.name).toBeDefined() |
61 | | - expect(option.description).toBeDefined() |
62 | | - counter++ |
63 | | - }) |
| 60 | + expect(option.name).toBeDefined(); |
| 61 | + expect(option.description).toBeDefined(); |
| 62 | + counter++; |
| 63 | + }); |
64 | 64 |
|
65 | 65 | // if the forEach somehow breaks the test should break |
66 | | - expect(counter).toBe(adapter.options.length) |
| 66 | + expect(counter).toBe(adapter.options.length); |
67 | 67 | } |
68 | | - }) |
| 68 | + }); |
69 | 69 |
|
70 | 70 | test('init should be a defined when options is defined', () => { |
71 | | - expect(typeof adapter.init).not.toBe('undefined') |
72 | | - }) |
| 71 | + expect(typeof adapter.init).not.toBe('undefined'); |
| 72 | + }); |
73 | 73 | } else { |
74 | | - test.skip('options should be an array of args options', () => {}) |
75 | | - test.skip('init should be a defined when options is defined', () => {}) |
| 74 | + test.skip('options should be an array of args options', () => {}); |
| 75 | + test.skip('init should be a defined when options is defined', () => {}); |
76 | 76 | } |
77 | 77 |
|
78 | 78 | if (typeof adapter.init !== 'undefined') { |
79 | 79 | test('init should be a function', () => { |
80 | | - expect(typeof adapter.init).toBe('function') |
81 | | - }) |
| 80 | + expect(typeof adapter.init).toBe('function'); |
| 81 | + }); |
82 | 82 |
|
83 | 83 | test('call init should not throw', () => { |
84 | | - adapter.init(options.initOptions || {}) |
85 | | - }) |
| 84 | + adapter.init(options.initOptions || {}); |
| 85 | + }); |
86 | 86 | } else { |
87 | | - test.skip('init should be a function', () => {}) |
88 | | - test.skip('call init should not throw', () => {}) |
| 87 | + test.skip('init should be a function', () => {}); |
| 88 | + test.skip('call init should not throw', () => {}); |
89 | 89 | } |
90 | 90 |
|
91 | 91 | it('should save and read', async () => { |
92 | | - await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }) |
| 92 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
93 | 93 |
|
94 | 94 | expect(await adapter.get('/a-key')).toEqual({ |
95 | 95 | views: [{ time: 1490623474639 }] |
96 | | - }) |
97 | | - }) |
| 96 | + }); |
| 97 | + }); |
98 | 98 |
|
99 | 99 | it('should return empty list of views when key has no views', async () => { |
100 | | - expect(await adapter.get('/c-key')).toEqual({ views: [] }) |
101 | | - }) |
| 100 | + expect(await adapter.get('/c-key')).toEqual({ views: [] }); |
| 101 | + }); |
102 | 102 |
|
103 | 103 | it('should return all saves on getAll', async () => { |
104 | | - await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }) |
105 | | - await adapter.put('/another-key', { views: [{ time: 1490623474639 }] }) |
| 104 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 105 | + await adapter.put('/another-key', { views: [{ time: 1490623474639 }] }); |
106 | 106 |
|
107 | 107 | expect(await adapter.getAll({ pathname: '/' })).toEqual({ |
108 | 108 | '/a-key': { views: [{ time: 1490623474639 }] }, |
109 | 109 | '/another-key': { views: [{ time: 1490623474639 }] } |
110 | | - }) |
111 | | - }) |
| 110 | + }); |
| 111 | + }); |
112 | 112 |
|
113 | 113 | it('should return filtered saves from getAll based on pathname', async () => { |
114 | | - await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }) |
115 | | - await adapter.put('/another-key', { views: [{ time: 1490623474639 }] }) |
116 | | - await adapter.put('/b-key', { views: [{ time: 1490623474639 }] }) |
| 114 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 115 | + await adapter.put('/another-key', { views: [{ time: 1490623474639 }] }); |
| 116 | + await adapter.put('/b-key', { views: [{ time: 1490623474639 }] }); |
117 | 117 |
|
118 | 118 | expect(await adapter.getAll({ pathname: '/a' })).toEqual({ |
119 | 119 | '/a-key': { views: [{ time: 1490623474639 }] }, |
120 | 120 | '/another-key': { views: [{ time: 1490623474639 }] } |
121 | | - }) |
122 | | - }) |
| 121 | + }); |
| 122 | + }); |
123 | 123 |
|
124 | 124 | it('should return filtered saves from getAll based on before', async () => { |
125 | | - await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }) |
126 | | - await adapter.put('/another-key', { views: [{ time: 1490623478639 }] }) |
127 | | - await adapter.put('/b-key', { views: [{ time: 1490623484639 }] }) |
| 125 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 126 | + await adapter.put('/another-key', { views: [{ time: 1490623478639 }] }); |
| 127 | + await adapter.put('/b-key', { views: [{ time: 1490623484639 }] }); |
128 | 128 |
|
129 | 129 | expect( |
130 | 130 | await adapter.getAll({ pathname: '/', before: 1490623478640 }) |
131 | 131 | ).toEqual({ |
132 | 132 | '/a-key': { views: [{ time: 1490623474639 }] }, |
133 | 133 | '/another-key': { views: [{ time: 1490623478639 }] }, |
134 | 134 | '/b-key': { views: [] } |
135 | | - }) |
136 | | - }) |
| 135 | + }); |
| 136 | + }); |
137 | 137 |
|
138 | 138 | it('should return filtered saves from getAll based on after', async () => { |
139 | | - await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }) |
140 | | - await adapter.put('/another-key', { views: [{ time: 1490623478639 }] }) |
141 | | - await adapter.put('/b-key', { views: [{ time: 1490623484639 }] }) |
| 139 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 140 | + await adapter.put('/another-key', { views: [{ time: 1490623478639 }] }); |
| 141 | + await adapter.put('/b-key', { views: [{ time: 1490623484639 }] }); |
142 | 142 |
|
143 | 143 | expect( |
144 | 144 | await adapter.getAll({ pathname: '/', after: 1490623478638 }) |
145 | 145 | ).toEqual({ |
146 | 146 | '/a-key': { views: [] }, |
147 | 147 | '/another-key': { views: [{ time: 1490623478639 }] }, |
148 | 148 | '/b-key': { views: [{ time: 1490623484639 }] } |
149 | | - }) |
150 | | - }) |
| 149 | + }); |
| 150 | + }); |
151 | 151 |
|
152 | 152 | it('should return filtered saves from get based on before', async () => { |
153 | 153 | await adapter.put('/a-key', { |
154 | 154 | views: [{ time: 1490623474639 }, { time: 1490623478639 }] |
155 | | - }) |
| 155 | + }); |
156 | 156 |
|
157 | 157 | expect(await adapter.get('/a-key', { before: 1490623475640 })).toEqual({ |
158 | 158 | views: [{ time: 1490623474639 }] |
159 | | - }) |
160 | | - }) |
| 159 | + }); |
| 160 | + }); |
161 | 161 |
|
162 | 162 | it('should return filtered saves from get based on after', async () => { |
163 | 163 | await adapter.put('/a-key', { |
164 | 164 | views: [{ time: 1490623474639 }, { time: 1490623478639 }] |
165 | | - }) |
| 165 | + }); |
166 | 166 |
|
167 | 167 | expect(await adapter.get('/a-key', { after: 1490623475640 })).toEqual({ |
168 | 168 | views: [{ time: 1490623478639 }] |
169 | | - }) |
170 | | - }) |
| 169 | + }); |
| 170 | + }); |
171 | 171 |
|
172 | 172 | it('should have check whether a key is stored with has', async () => { |
173 | | - await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }) |
| 173 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
174 | 174 |
|
175 | | - expect(await adapter.has('/a-key')).toEqual(true) |
176 | | - expect(await adapter.has('/non-existing-key')).toEqual(false) |
177 | | - }) |
| 175 | + expect(await adapter.has('/a-key')).toEqual(true); |
| 176 | + expect(await adapter.has('/non-existing-key')).toEqual(false); |
| 177 | + }); |
178 | 178 |
|
179 | 179 | if (typeof adapter.subscribe === 'function') { |
180 | 180 | it('should allow subscription with observables', async () => { |
181 | | - const listener = jest.fn() |
182 | | - const unsubscribe = adapter.subscribe(listener) |
| 181 | + const listener = jest.fn(); |
| 182 | + const unsubscribe = adapter.subscribe(listener); |
183 | 183 |
|
184 | | - await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }) |
| 184 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
185 | 185 |
|
186 | 186 | expect(listener).toHaveBeenCalledWith({ |
187 | 187 | key: '/a-key', |
188 | 188 | value: { views: [{ time: 1490623474639 }] } |
189 | | - }) |
190 | | - }) |
| 189 | + }); |
| 190 | + }); |
191 | 191 |
|
192 | 192 | it('should allow multiple subscription with observables and handle unsubscribption', async () => { |
193 | | - const listener1 = jest.fn() |
194 | | - const listener2 = jest.fn() |
195 | | - const subscription = adapter.subscribe(listener1) |
196 | | - adapter.subscribe(listener2) |
| 193 | + const listener1 = jest.fn(); |
| 194 | + const listener2 = jest.fn(); |
| 195 | + const subscription = adapter.subscribe(listener1); |
| 196 | + adapter.subscribe(listener2); |
197 | 197 |
|
198 | | - await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }) |
199 | | - subscription.unsubscribe() |
200 | | - await adapter.put('/b-key', { views: [{ time: 1490623474639 }] }) |
| 198 | + await adapter.put('/a-key', { views: [{ time: 1490623474639 }] }); |
| 199 | + subscription.unsubscribe(); |
| 200 | + await adapter.put('/b-key', { views: [{ time: 1490623474639 }] }); |
201 | 201 |
|
202 | 202 | expect(listener1).toHaveBeenCalledWith({ |
203 | 203 | key: '/a-key', |
204 | 204 | value: { views: [{ time: 1490623474639 }] } |
205 | | - }) |
| 205 | + }); |
206 | 206 | expect(listener1).not.toHaveBeenCalledWith({ |
207 | 207 | key: '/b-key', |
208 | 208 | value: { views: [{ time: 1490623474639 }] } |
209 | | - }) |
| 209 | + }); |
210 | 210 | expect(listener2).toHaveBeenCalledWith({ |
211 | 211 | key: '/a-key', |
212 | 212 | value: { views: [{ time: 1490623474639 }] } |
213 | | - }) |
| 213 | + }); |
214 | 214 | expect(listener2).toHaveBeenCalledWith({ |
215 | 215 | key: '/b-key', |
216 | 216 | value: { views: [{ time: 1490623474639 }] } |
217 | | - }) |
218 | | - }) |
| 217 | + }); |
| 218 | + }); |
219 | 219 | } else { |
220 | | - it.skip('should allow subscription with observables', () => {}) |
| 220 | + it.skip('should allow subscription with observables', () => {}); |
221 | 221 | it.skip( |
222 | 222 | 'should allow multiple subscription with observables and handle unsubscribption', |
223 | 223 | () => {} |
224 | | - ) |
| 224 | + ); |
225 | 225 | } |
226 | | - }) |
227 | | -} |
| 226 | + }); |
| 227 | +}; |
0 commit comments