From 21450dfd7fed113b1b183ea4e5f4b6bcf29e1210 Mon Sep 17 00:00:00 2001 From: Alessio Coser Date: Wed, 4 Oct 2017 15:19:22 +0200 Subject: [PATCH] injects usersStat dependency to improve code coverage of Logs class --- lib/plugins/logs/logs.js | 7 +++--- lib/plugins/logs/logs.test.js | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/plugins/logs/logs.js b/lib/plugins/logs/logs.js index 1888f7227..139e59394 100644 --- a/lib/plugins/logs/logs.js +++ b/lib/plugins/logs/logs.js @@ -1,11 +1,12 @@ 'use strict'; const BbPromise = require('bluebird'); -const userStats = require('../../utils/userStats'); +const defaultUserStats = require('../../utils/userStats'); class Logs { - constructor(serverless) { + constructor(serverless, userStats) { this.serverless = serverless; + this.userStats = userStats || defaultUserStats; this.commands = { logs: { @@ -55,7 +56,7 @@ class Logs { if (sls && sls.processedInput && sls.processedInput.options) { const opts = sls.processedInput.options; const type = (opts.tail) ? 'service_logsTailed' : 'service_logsViewed'; - userStats.track(type); + this.userStats.track(type); } return BbPromise.resolve(); } diff --git a/lib/plugins/logs/logs.test.js b/lib/plugins/logs/logs.test.js index bc720ab1f..a8034ef3c 100644 --- a/lib/plugins/logs/logs.test.js +++ b/lib/plugins/logs/logs.test.js @@ -1,6 +1,7 @@ 'use strict'; const expect = require('chai').expect; +const sinon = require('sinon'); const Logs = require('./logs'); const Serverless = require('../../Serverless'); @@ -16,4 +17,45 @@ describe('Logs', () => { describe('#constructor()', () => { it('should have commands', () => expect(logs.commands).to.be.not.empty); }); + + describe('#track()', () => { + let userStats; + + beforeEach(() => { + userStats = { track: sinon.spy() }; + }); + + describe('Without cli processed input', () => { + it('do not track user stats', () => { + const newLogs = new Logs(serverless, userStats); + + return newLogs.track() + .then(() => { + expect(userStats.track.called).to.be.equal(false); + }); + }); + }); + + describe('With cli processed input', () => { + it('tracks user stats with viewed option', () => { + serverless.processedInput = { commands: [], options: {} }; + + return new Logs(serverless, userStats).track() + .then(() => { + expect(userStats.track.calledWithExactly('service_logsViewed')) + .to.be.equal(true); + }); + }); + + it('tracks user stats with tailed option', () => { + serverless.processedInput = { commands: [], options: { tail: true } }; + + return new Logs(serverless, userStats).track() + .then(() => { + expect(userStats.track.calledWithExactly('service_logsTailed')) + .to.be.equal(true); + }); + }); + }); + }); });