|
| 1 | +const should = require('should'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const { Deprecation } = require('../../src/utils/Deprecation'); |
| 4 | + |
| 5 | +describe('Deprecation', () => { |
| 6 | + let deprecationHandler, response; |
| 7 | + const sandbox = sinon.createSandbox(); |
| 8 | + const NODE_ENV = process.env.NODE_ENV; |
| 9 | + |
| 10 | + beforeEach(()=>{ |
| 11 | + sandbox.stub(console, 'warn'); |
| 12 | + process.env.NODE_ENV = 'development'; |
| 13 | + deprecationHandler = new Deprecation(true); |
| 14 | + |
| 15 | + response = { |
| 16 | + action: 'test', |
| 17 | + collection: 'collection', |
| 18 | + controller: 'controller', |
| 19 | + deprecations: [ |
| 20 | + { |
| 21 | + message: 'Use this route instead: http://kuzzle:7512/test/succeed', |
| 22 | + version: '6.6.6' |
| 23 | + } |
| 24 | + ], |
| 25 | + error: null, |
| 26 | + index: null, |
| 27 | + node: 'nodeJS', |
| 28 | + requestId: 'idididididid', |
| 29 | + result: true, |
| 30 | + status: 200, |
| 31 | + volatile: null |
| 32 | + }; |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(()=>{ |
| 36 | + sandbox.restore(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should warn the developer that he is using a deprecated action', () => { |
| 40 | + deprecationHandler.logDeprecation(response); |
| 41 | + |
| 42 | + should(console.warn) |
| 43 | + .be.calledOnce() |
| 44 | + .be.calledWith(response.deprecations[0].message); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return the same response as it has received', () => { |
| 48 | + should(deprecationHandler.logDeprecation(response)).match(response); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle multiple deprecations', () => { |
| 52 | + response.deprecations.push(response); |
| 53 | + |
| 54 | + deprecationHandler.logDeprecation(response); |
| 55 | + |
| 56 | + should(console.warn).be.calledTwice(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should not warn the developer if he refused to', () => { |
| 60 | + deprecationHandler = new Deprecation(false); |
| 61 | + |
| 62 | + deprecationHandler.logDeprecation(response); |
| 63 | + |
| 64 | + should(console.warn).not.have.been.called(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should not warn the developer if there is no deprecation', () => { |
| 68 | + response.deprecations = []; |
| 69 | + |
| 70 | + deprecationHandler.logDeprecation(response); |
| 71 | + |
| 72 | + should(console.warn).not.have.been.called(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should not warn the developer in production', () => { |
| 76 | + process.env.NODE_ENV = 'production'; |
| 77 | + deprecationHandler = new Deprecation(true); |
| 78 | + |
| 79 | + deprecationHandler.logDeprecation(response); |
| 80 | + |
| 81 | + should(console.warn).not.have.been.called(); |
| 82 | + }); |
| 83 | + |
| 84 | + after(() => { |
| 85 | + process.env.NODE_ENV = NODE_ENV; |
| 86 | + }); |
| 87 | +}); |
0 commit comments