From 9cd30be25ef90f569228eb5ee74524a3be141114 Mon Sep 17 00:00:00 2001 From: Nelson Pecora Date: Mon, 2 Oct 2017 14:15:10 -0400 Subject: [PATCH] fix tests, clarify api for plugin authors --- lib/component-data/actions.test.js | 4 ++-- lib/component-data/reactive-render.test.js | 2 +- lib/decorators/select.test.js | 2 +- lib/forms/behaviors.test.js | 6 +++--- lib/utils/api.js | 4 ++-- test/setup.js | 19 ++++++++++++++++--- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/component-data/actions.test.js b/lib/component-data/actions.test.js index 5caab5491..7b7335c48 100644 --- a/lib/component-data/actions.test.js +++ b/lib/component-data/actions.test.js @@ -64,7 +64,7 @@ describe('component-data actions', () => { it('reverts components with model.js if model.js errors', () => { model.save.returns(Promise.reject(new Error('nope'))); return fn(store, { uri, data, prevData }).catch(() => { - expect(console.error).to.have.been.calledWith('Error saving component (foo): nope'); + expect(loggerStub.error.called).to.equal(true); }); }); @@ -83,7 +83,7 @@ describe('component-data actions', () => { model.render.returns(Promise.resolve(data)); return fn(store, { uri, data, prevData }).catch(() => { expect(queue.add).to.have.been.calledWith(api.save, [uri, data, false], 'save'); - expect(console.error).to.have.been.calledWith('Error saving component (foo): nope'); + expect(loggerStub.error.called).to.equal(true); }); }); diff --git a/lib/component-data/reactive-render.test.js b/lib/component-data/reactive-render.test.js index c202fb95e..016129d85 100644 --- a/lib/component-data/reactive-render.test.js +++ b/lib/component-data/reactive-render.test.js @@ -22,7 +22,7 @@ describe('reactive render', () => { const weirdNode = document.createComment('hi mom'); fn('foo', weirdNode); - expect(console.error).to.have.been.calledWith('Unknown node type (8) for "foo"'); + expect(loggerStub.error.called).to.equal(true); }); it('updates body components when passed element', () => { diff --git a/lib/decorators/select.test.js b/lib/decorators/select.test.js index 4c9520c5b..a17b039b7 100644 --- a/lib/decorators/select.test.js +++ b/lib/decorators/select.test.js @@ -37,7 +37,7 @@ describe('select', () => { components.getData.returns({ fooProp: { [refProp]: '/components/foo' } }); components.getSchema.returns({}); fn('/components/foo', '/components/bar'); - expect(console.warn).to.have.been.calledWith('bar has no field for fooProp in its schema, but has foo in its data'); + expect(loggerStub.warn.called).to.equal(true); }); it('returns path for component in list', () => { diff --git a/lib/forms/behaviors.test.js b/lib/forms/behaviors.test.js index 4d186b430..7cbd72618 100644 --- a/lib/forms/behaviors.test.js +++ b/lib/forms/behaviors.test.js @@ -69,7 +69,7 @@ describe('behaviors', () => { it('warns if behavior slot not found in definition', () => { fn('foo'); - expect(console.warn).to.have.been.calledWith('Behavior "foo" has no slot specified. Make sure you add it!'); + expect(loggerStub.warn.called).to.equal(true); }); it('adds behavior slot from definition', () => { @@ -78,7 +78,7 @@ describe('behaviors', () => { it('omits missing behaviors', () => { expect(fn('baz')).to.eql([]); - expect(console.warn).to.have.been.calledWith('Behavior "baz" not found. Make sure you add it!'); + expect(loggerStub.warn.called).to.equal(true); }); it('converts behavior names that match native tags', () => { @@ -87,7 +87,7 @@ describe('behaviors', () => { it('converts behaviors name BEFORE omitting', () => { expect(fn('textarea')).to.eql([]); - expect(console.warn).to.have.been.calledWith('Behavior "input-textarea" not found. Make sure you add it!'); + expect(loggerStub.warn.called).to.equal(true); }); }); }); diff --git a/lib/utils/api.js b/lib/utils/api.js index 9ce315325..f59a8a481 100644 --- a/lib/utils/api.js +++ b/lib/utils/api.js @@ -11,7 +11,7 @@ import * as urls from './urls'; import getAvailableComponents from './available-components'; import * as local from './local'; import * as validationHelpers from '../validators/helpers'; -import * as log from './log'; +import logger from './log'; // these utilities are exported so 3rd party plugins/validators/behaviors/panes can access them. // note: the store is passed into those things automatically, so don't export it here @@ -29,7 +29,7 @@ const api = { getAvailableComponents, local, validationHelpers, - log, + logger, // plugin authors, please instantiate a logger from this (passing in the filename / plugin used) version: process.env.KILN_VERSION }; diff --git a/test/setup.js b/test/setup.js index c123d18b4..5819cb9e9 100644 --- a/test/setup.js +++ b/test/setup.js @@ -2,6 +2,7 @@ import _ from 'lodash'; import Vue from 'vue'; import Vuex from 'vuex'; import { beforeEachHooks, afterEachHooks, mount } from 'vue-unit/src'; +import * as logger from '../lib/utils/log'; // allow us to stub the default const testsContext = require.context('../', true, /^\.\/(lib|behaviors)\/.*?\.test\.js$/); @@ -16,11 +17,23 @@ window.renderWithArgs = (Component, props, state) => { return mount(Component, { props, store: _.assign({}, defaultStore, { state }) }); }; +// stub logger +window.loggerStub = { + info: sinon.spy(), + trace: sinon.spy(), + debug: sinon.spy(), + warn: sinon.spy(), + error: sinon.spy() +}; + +sinon.stub(logger, 'default', () => { + // return the same instances of our logging spies every time + // we create a new logger + return window.loggerStub; +}); + window.beforeEachHooks = beforeEachHooks; window.afterEachHooks = afterEachHooks; -// don't write to console -sinon.stub(console); - // run all tests testsContext.keys().forEach(testsContext);