Skip to content

Commit

Permalink
Code style fixes and a couple of bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
rhyslbw committed May 3, 2016
1 parent a3beb30 commit 41eba01
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 75 deletions.
4 changes: 2 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Package.onTest(function(api) {

api.add_files([
'tests/unit/winston-adapter.unit.js',
'tests/integration/module.integration.js',
'tests/integration/module.integration.js'
], 'server');

});
});
4 changes: 2 additions & 2 deletions source/server/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Space.Module.define('Space.logging.Winston', {
const log = this.injector.get('log');
let transports = lodash.get(this.configuration, 'log.winston.transports', []);
if (lodash.isEmpty(transports)) {
transports = [this._setupWinstonConsoleTransport()]
transports = [this._setupWinstonConsoleTransport()];
}

const adapter = new Space.Logger.WinstonAdapter(winston, transports);
Expand All @@ -27,4 +27,4 @@ Space.Module.define('Space.logging.Winston', {
};
return new winston.transports.Console(options);
}
});
});
2 changes: 1 addition & 1 deletion source/server/winston-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const WinstonAdapter = Space.Logger.Adapter.extend('Space.Logger.WinstonAdapter'

Constructor(winston, transports) {
if (!winston) {
throw new Error(this.ERRORS.winstonMissing)
throw new Error(this.ERRORS.winstonMissing);
}
const lib = new winston.Logger({
transports: transports || []
Expand Down
31 changes: 15 additions & 16 deletions tests/integration/module.integration.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
describe("Space.logging.WinstonAdapter", function() {
describe("Space.logging.WinstonAdapter", () => {


beforeEach(function () {
beforeEach(() => {
this.winston = Npm.require('winston');
testApp = Space.Application.extend('Test.App', {
const TestApp = Space.Application.extend('Test.App', {
configuration: {},
requiredModules: ['Space.logging.Winston'],
requiredModules: ['Space.logging.Winston']
});
this.app = new testApp();
this.app = new TestApp();
this.app.start();
});

it('adds instance of Space.Logger.WinstonAdapter to instance of Space.Logger', function() {
it('adds instance of Space.Logger.WinstonAdapter to instance of Space.Logger', () => {
const log = this.app.injector.get('log');
const adapter = log.adapter('winston');

expect(adapter).to.be.instanceof(Space.Logger.WinstonAdapter);
expect(adapter.lib()).to.be.instanceof(this.winston.Logger);
});

it('maps instance of Space.Logger.WinstonAdapter to Injector', function() {
it('maps instance of Space.Logger.WinstonAdapter to Injector', () => {
expect(
this.app.injector.get('Space.Logger.WinstonAdapter')
).to.be.instanceof(Space.Logger.WinstonAdapter);
});

describe('configuration', function () {
describe('transports', function () {
it('uses default winston.transports.Console when transports are not configured', function() {
describe('configuration', () => {
describe('transports', () => {
it('uses default winston.transports.Console when transports are not configured', () => {
const logger = this.app.injector.get('log');
const adapter = logger.adapter('winston');

expect(adapter.hasTransport('console')).to.be.true;
});

it('overrides transports configuration', function() {
it('overrides transports configuration', () => {
const options = {
colorize: false,
json: true,
level: 'error'
}
const testApp = Space.Application.extend('Test.App', {
};
const TestApp = Space.Application.extend('Test.App', {
configuration: {
log: {
enabled: true,
Expand All @@ -51,7 +50,7 @@ describe("Space.logging.WinstonAdapter", function() {
},
requiredModules: ['Space.logging.Winston'],
});
const app = new testApp();
const app = new TestApp();
app.start();

const log = app.injector.get('log');
Expand All @@ -63,4 +62,4 @@ describe("Space.logging.WinstonAdapter", function() {
});
});

});
});
99 changes: 45 additions & 54 deletions tests/unit/winston-adapter.unit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
describe("Space.Logger.WinstonAdapter", function() {
describe("Space.Logger.WinstonAdapter", () => {

beforeEach(function () {
beforeEach(() => {
this.winston = {
config: {syslog: {levels: {} }}
config: { syslog: { levels: {} } }
};

class Logger {};
class Logger {}
Logger.prototype.setLevels = sinon.stub();
Logger.prototype.add = sinon.stub();
Logger.prototype.remove = sinon.stub();
Expand All @@ -18,63 +17,58 @@ describe("Space.Logger.WinstonAdapter", function() {
this.winston.Logger = sinon.spy(Logger);
});

it('extends Space.Logger.Adapter', function() {
it('extends Space.Logger.Adapter', () => {
expect(Space.Logger.WinstonAdapter).to.extend(Space.Logger.Adapter);
});

describe('construction', function () {
it("takes winston as dependency", function() {
describe('construction', () => {
it("takes winston as dependency", () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
expect(this.winston.Logger.calledWithNew()).to.be.true;
expect(adapter.lib()).to.be.instanceOf(this.winston.Logger);
});

it("throws an error if winston is not provided", function() {
const instantiate = function() {
new Space.Logger.WinstonAdapter()
};
it("throws an error if winston is not provided", () => {
const instantiate = () => new Space.Logger.WinstonAdapter();
expect(instantiate).to.throw(
Space.Logger.WinstonAdapter.prototype.ERRORS.winstonMissing()
);
});

describe('transports', function () {
it("can be constructed without transports argument", function() {
instantiate = () => {
new Space.Logger.WinstonAdapter(this.winston);
};
describe('transports', () => {
it("can be constructed without transports argument", () => {
const instantiate = () => new Space.Logger.WinstonAdapter(this.winston);
expect(instantiate).to.not.throw(Error);
});

describe("takes transports", function () {
it("as empty array", function() {
describe("takes transports", () => {
it("as empty array", () => {
const transports = [];
const adapter = new Space.Logger.WinstonAdapter(
this.winston, transports
);
expect(
this.winston.Logger.calledWith({transports: transports})
this.winston.Logger.calledWith({ transports: transports })
).to.be.true;
});

it("as filled array", function() {
const transports = [{name: 'console'}, {name: 'file'}];
it("as filled array", () => {
const transports = [{ name: 'console' }, { name: 'file' }];
const adapter = new Space.Logger.WinstonAdapter(
this.winston, transports
);
expect(
this.winston.Logger.calledWith({transports: transports})
this.winston.Logger.calledWith({ transports: transports })
).to.be.true;
});
});

});
});

describe('transports', function () {
it('adds transport to winston', function() {
describe('transports', () => {
it('adds transport to winston', () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);

const transport = sinon.spy();
const options = {};
adapter.addTransport(transport, options);
Expand All @@ -83,56 +77,53 @@ describe("Space.Logger.WinstonAdapter", function() {
expect(adapter.lib().add).to.be.calledWith(transport, options);
});

it('removes transport on winston', function() {
it('removes transport on winston', () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);

const transport = sinon.spy();
adapter.removeTransport(transport);

expect(adapter.lib().remove).to.be.calledOnce;
expect(adapter.lib().remove).to.be.calledWith(transport);
});

it('checks if transport is added on winston', function() {
it('checks if transport is added on winston', () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
adapter.lib().transports = {console: sinon.spy()}
adapter.lib().transports = { console: sinon.spy() };

expect(adapter.hasTransport('console')).to.be.true;
});

it('returns transport from winston', function() {
it('returns transport from winston', () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
const consoleTransport = sinon.spy()
adapter.lib().transports = {console: consoleTransport};
const consoleTransport = sinon.spy();
adapter.lib().transports = { console: consoleTransport };

expect(adapter.transport('console')).to.be.equal(consoleTransport);
expect(adapter.transport('file')).to.be.equal(null);
});

it('returns transports from winston', function() {
it('returns transports from winston', () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
const transports = {console: sinon.spy}
const transports = { console: sinon.spy };
adapter.lib().transports = transports;

expect(adapter.transports()).to.be.equal(transports);
});

describe('setting level', function () {
it('sets level on transport', function () {
describe('setting level', () => {
it('sets level on transport', () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
adapter.lib().transports = {console: sinon.spy()}
adapter.lib().transports = { console: sinon.spy() };

adapter.setLevel('console', 5)
adapter.setLevel('console', 5);
expect(adapter.transport('console').level).to.be.equal(5);
});

it('throws error if transport does not exist', function () {
it('throws error if transport does not exist', () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
adapter.lib().transports = {}
adapter.lib().transports = {};

setLevel = function() {
adapter.setLevel('console', 5)
};
const setLevel = () => adapter.setLevel('console', 5);
expect(setLevel).to.throw(
Space.Logger.WinstonAdapter.prototype.ERRORS.transportNotFound(
'console'
Expand All @@ -142,32 +133,32 @@ describe("Space.Logger.WinstonAdapter", function() {
});
});

describe('logging', function () {
describe('logs message as', function () {
it("debug", function() {
describe('logging', () => {
describe('logs message as', () => {
it("debug", () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
let message = 'My log message';
const message = 'My log message';
adapter.debug(message);
expect(adapter.lib().debug).to.be.calledWithExactly(message);
});

it("info", function() {
it("info", () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
let message = 'My log message';
const message = 'My log message';
adapter.info(message);
expect(adapter.lib().info).to.be.calledWithExactly(message);
});

it("warning", function() {
it("warning", () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
let message = 'My log message';
const message = 'My log message';
adapter.warning(message);
expect(adapter.lib().warning).to.be.calledWithExactly(message);
});

it("error", function() {
it("error", () => {
const adapter = new Space.Logger.WinstonAdapter(this.winston);
let message = 'My log message';
const message = 'My log message';
adapter.error(message);
expect(adapter.lib().error).to.be.calledWithExactly(message);
});
Expand Down

0 comments on commit 41eba01

Please sign in to comment.