| 1 |  |  | import test from 'ava'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import sinon from 'sinon'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import * as applyMiddleware from '../src/applyMiddleware'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | import callAPIMethod from '../src/callAPIMethod'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import createAPI from '../src/createAPI'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | let stubApplyMiddleware; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | let stubCallAPIMethod; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | const boundCallAPIMethod = _ => _; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | test.beforeEach(t => { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     stubCallAPIMethod = sinon.stub(callAPIMethod, 'bind'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     stubCallAPIMethod.returns(boundCallAPIMethod); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     stubApplyMiddleware = sinon.stub(applyMiddleware, 'default'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | test.afterEach.always(t => { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     stubApplyMiddleware.restore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     stubCallAPIMethod.restore(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | test('undefined resources', t => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const API = createAPI(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     t.deepEqual(API, {}, 'empty API object'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | test('0 resources', t => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     const API = createAPI({}); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     t.deepEqual(API, {}, 'empty API object'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | test('supports resource prefix insted of namespace', t => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     const user = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         prefix: 'user-endpoint', | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |         methods: { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             get: ({ id }) => ({ path: ['get', id] }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     const resources = { user }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     const API = createAPI(resources, [], 'https://example.com'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     API.user.get(1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     t.true(stubCallAPIMethod.calledWithExactly( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         null, 'https://example.com', {}, 'user-endpoint' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     ), 'correct arguments passed to callAPIMethod'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | test('general behaviour', t => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     const userResource = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         namespace: 'user', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         methods: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             get: ({ id }) => ({ path: ['get', id] }), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             delete: ({ id }) => ({ path: ['delete', id], options: { method: 'DELETE'} }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     const projectResource = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         namespace: 'project', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         methods: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             get: ({ id }) => ({ path: ['get', id] }), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             delete: ({ id }) => ({ path: ['delete', id], options: { method: 'DELETE'} }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     const resources = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         user: userResource, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         project: projectResource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     const middleware = [ _ => _, _ => _]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     const API = createAPI(resources, middleware, 'https://example.com'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     t.deepEqual(Object.keys(API), ['user', 'project'], 'resources'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     Object.keys(API).forEach(resource => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         t.deepEqual(Object.keys(API[resource]), ['get', 'delete'], 'methods'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         Object.keys(API[resource]).forEach(method => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             t.true(API[resource][method] instanceof Function, 'methods are functions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     const methodOptions = { specific: 'option' }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     API.user.delete({ id: 1 }, methodOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     t.true(stubCallAPIMethod.calledOnce); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     t.true(stubApplyMiddleware.calledOnce); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     t.true(stubCallAPIMethod.calledBefore(stubApplyMiddleware)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     t.true(stubCallAPIMethod.calledWithExactly( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         null, 'https://example.com', {}, 'user' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     t.is(stubApplyMiddleware.lastCall.args[0], boundCallAPIMethod, 'stubApplyMiddleware called with boundCallAPIMethod'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     t.is(stubApplyMiddleware.lastCall.args[1], middleware, 'stubApplyMiddleware called with middleware'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     t.is(stubApplyMiddleware.lastCall.args[2], methodOptions, 'stubApplyMiddleware called with methodOptions'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     t.deepEqual(stubApplyMiddleware.lastCall.args[3], { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         path: ['delete', 1], options: { method: 'DELETE'} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     }, 'stubApplyMiddleware called with correct apiParams'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     t.is(stubApplyMiddleware.lastCall.args[4], 'user', 'stubApplyMiddleware called with correct resourceId'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     t.is(stubApplyMiddleware.lastCall.args[5], 'delete', 'stubApplyMiddleware called with correct method'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     API.user.delete({ id: 1 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     t.true(stubCallAPIMethod.calledTwice); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     t.true(stubApplyMiddleware.calledTwice); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |     t.true(stubCallAPIMethod.calledBefore(stubApplyMiddleware)); | 
            
                                                        
            
                                    
            
            
                | 105 |  |  | }); | 
            
                        
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.