Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Add more tests to telemetry and fix command stub
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcarpanelli committed Sep 17, 2019
1 parent 1456622 commit 1b287d6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
1 change: 0 additions & 1 deletion packages/cli/src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ async function checkOptIn(interactive: boolean): Promise<GlobalTelemetryOptions
if (localOptIn === false) return undefined;

// disable interactivity manually for tests and CI
// TODO: move to options.ts
if (DISABLE_INTERACTIVITY) interactive = false;

if (globalOptIn === undefined && interactive) {
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/test/commands/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import * as Compiler from '../../src/models/compiler/Compiler';
import Dependency from '../../src/models/dependency/Dependency';
import ErrorHandler from '../../src/models/errors/ErrorHandler';
import ConfigManager from '../../src/models/config/ConfigManager';
import * as telemetry from '../../src/telemetry';
import Telemetry from '../../src/telemetry';

program.Command.prototype.parseReset = function() {
var self = this;
Expand Down Expand Up @@ -102,7 +102,8 @@ exports.stubCommands = function() {
.get(function getterFn() {
return projectFile;
});
this.report = sinon.stub(telemetry, 'report').returns(undefined);

this.report = sinon.stub(Telemetry, 'report');
});

afterEach('restore', function() {
Expand Down
36 changes: 32 additions & 4 deletions packages/cli/test/telemetry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'firebase/firestore';
import Telemetry from '../src/telemetry'
import ProjectFile from '../src/models/files/ProjectFile';

describe.only('telemetry', function() {
describe('telemetry', function() {
beforeEach('stub fs-extra functions', function() {
this.readJsonStub = sinon.stub(fs, 'readJson').returns({ catch: () => undefined })
this.ensureDirStub = sinon.stub(fs, 'ensureDir')
Expand All @@ -37,7 +37,7 @@ describe.only('telemetry', function() {

context('when interactive mode is activated', function() {
context('when neither local nor global options are set', function() {
context('when the user answers no to telemetry prompt', function() {
context('when the user answers no to telemetry prompted question', function() {
beforeEach(async function() {
this.inquirerPrompt = sinon.stub(inquirer, 'prompt').returns({ telemetry: false });
});
Expand All @@ -46,9 +46,28 @@ describe.only('telemetry', function() {
await Telemetry.report('create', {}, true);
expect(this.sendToFirebase.getCall(0)).to.be.null;
});

it('writes local file and sets telemetryOptIn option to false', async function() {
this.telemetryOptInSetter = sinon.spy(ProjectFile.prototype, 'telemetryOptIn', ['set']);
this.projectFileWrite = sinon.spy(ProjectFile.prototype, 'write');
await Telemetry.report('create', {}, true);

this.telemetryOptInSetter.set.should.have.been.calledWithExactly(false);
this.projectFileWrite.calledOnce.should.be.true
})

it('writes the global file with salt, uuid, and optIn as false', async function() {
await Telemetry.report('create', {}, true);
const [firstArg, secondArg] = this.writeJsonStub.getCall(0).args;

firstArg.should.match(/telemetry.json/);
secondArg.optIn.should.be.false;
secondArg.uuid.should.match(/^[a-f0-9\-]+$/);
secondArg.salt.should.match(/^[a-f0-9]+$/);
});
});

context('when the user answers yes to telemetry prompt', function() {
context('when the user answers yes to telemetry prompted question', function() {
beforeEach(async function() {
this.inquirerPrompt = sinon.stub(inquirer, 'prompt').returns({ telemetry: true });
});
Expand All @@ -58,7 +77,7 @@ describe.only('telemetry', function() {
this.inquirerPrompt.calledOnce.should.be.true
});

it('writes the global file with salt and uuid', async function() {
it('writes the global file with salt, uuid and optIn as true', async function() {
await Telemetry.report('create', {}, true);
const [firstArg, secondArg] = this.writeJsonStub.getCall(0).args;

Expand All @@ -68,6 +87,15 @@ describe.only('telemetry', function() {
secondArg.salt.should.match(/^[a-f0-9]+$/);
});

it('writes local file', async function() {
this.telemetryOptInSetter = sinon.spy(ProjectFile.prototype, 'telemetryOptIn', ['set']);
this.projectFileWrite = sinon.spy(ProjectFile.prototype, 'write');
await Telemetry.report('create', {}, true);

this.telemetryOptInSetter.set.should.have.been.calledWithExactly(true);
this.projectFileWrite.calledOnce.should.be.true
})

describe('sendToFirebase function call', function() {
before(function() {
this.commandData = {
Expand Down

0 comments on commit 1b287d6

Please sign in to comment.