|
| 1 | +const Sinon = require('sinon'); |
| 2 | +const program = require('commander'); |
| 3 | +const { assert } = require('chai'); |
| 4 | +const Config = require('../../src/include/file-config'); |
| 5 | +const moment = require('moment'); |
| 6 | +const report = require('../../src/models/report'); |
| 7 | + |
| 8 | +describe('Command Report. Date shortcuts', () => { |
| 9 | + it('Shortcut last_month registered', () => { |
| 10 | + /** @type {Sinon.SinonMock} */ |
| 11 | + const stub = Sinon.mock(program); |
| 12 | + |
| 13 | + /** @type {Sinon.SinonSpy} */ |
| 14 | + const spy = Sinon.spy(program, 'option'); |
| 15 | + |
| 16 | + stub.expects('parse').throws('Test exception to prevent further execution of gtt-report') |
| 17 | + try { |
| 18 | + require('../../src/gtt-report'); |
| 19 | + } catch (e) {} |
| 20 | + |
| 21 | + try { |
| 22 | + assert(spy.calledWith('--last_month'), 'last_month not registered'); |
| 23 | + } finally { |
| 24 | + spy.restore(); |
| 25 | + stub.restore(); |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + it('Shortcut last_month. Sets From and To to Config', () => { |
| 30 | + program.last_month = true; |
| 31 | + program.args = []; |
| 32 | + let configSpy = Sinon.spy(Config.prototype, 'set'); |
| 33 | + |
| 34 | + const reportOriginal = Object.getPrototypeOf(report); |
| 35 | + let reportStub = Sinon.stub().callsFake(() => { |
| 36 | + throw new Error('Test exception to prevent further execution of gtt-report') |
| 37 | + }); |
| 38 | + Object.setPrototypeOf(report, reportStub); |
| 39 | + |
| 40 | + stub = Sinon.mock(program); |
| 41 | + stub.expects('parse').returnsThis(); |
| 42 | + |
| 43 | + try { |
| 44 | + require('../../src/gtt-report'); |
| 45 | + } catch (e) {} |
| 46 | + |
| 47 | + try { |
| 48 | + assert( |
| 49 | + configSpy.calledWith('from', moment().subtract(1, 'months').startOf('month')), |
| 50 | + 'From is not set to Config' |
| 51 | + ); |
| 52 | + assert( |
| 53 | + configSpy.calledWith('to', moment().subtract(1, 'months').endOf('month')), |
| 54 | + 'To is not set to Config' |
| 55 | + ); |
| 56 | + } finally { |
| 57 | + configSpy.restore(); |
| 58 | + Object.setPrototypeOf(report, reportOriginal); |
| 59 | + delete require.cache[require.resolve('../../src/models/report')]; |
| 60 | + } |
| 61 | + }) |
| 62 | +}) |
0 commit comments