Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial test cleanup #3395

Merged
merged 2 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/utils/deprecate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const deprecate = function(message, test) {
}
};

/* istanbul ignore next: can't clear console */
deprecate._console = typeof console !== 'undefined' ? console : {};
deprecate._warn = function() {
const warn = deprecate._console.warn || deprecate._console.log || _.noop;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

import Error from '../../src/error';
import Behavior from '../../src/behavior';
import Region from '../../src/region';
import View from '../../src/view';
import CollectionView from '../../src/collection-view';
import behaviorsLookup from '../../src/config/behaviors-lookup';
import Error from '../../../src/error';
import Behavior from '../../../src/behavior';
import Region from '../../../src/region';
import View from '../../../src/view';
import CollectionView from '../../../src/collection-view';
import behaviorsLookup from '../../../src/config/behaviors-lookup';

describe('Behaviors', function() {

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
80 changes: 53 additions & 27 deletions test/unit/utils/deprecate.spec.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,129 @@
describe('Marionette.deprecate', function() {
import deprecate from '../../../src/utils/deprecate';

import Marionette from '../../../src/backbone.marionette';

describe('deprecate', function() {
beforeEach(function() {
Marionette.DEV_MODE = true;
this.sinon.spy(Marionette.deprecate, '_warn');
this.sinon.stub(Marionette.deprecate, '_console', {
warn: this.sinon.stub()
this.sinon.spy(deprecate, '_warn');
this.sinon.stub(deprecate, '_console', {
warn: this.sinon.stub(),
log: this.sinon.stub()
});
Marionette.deprecate._cache = {};
deprecate._cache = {};
});

afterEach(function() {
Marionette.DEV_MODE = false;
});

describe('Marionette.deprecate._warn', function() {
beforeEach(function() {
Marionette.deprecate._warn('foo');
});

describe('#_warn', function() {
it('should `console.warn` the message', function() {
expect(Marionette.deprecate._console.warn)
deprecate._warn('foo');
expect(deprecate._console.warn)
.to.have.been.calledOnce
.and.calledOn(Marionette.deprecate._console)
.and.calledOn(deprecate._console)
.and.calledWith('foo');
});

describe('when `console.warn` does not exist', function() {
beforeEach(function() {
deprecate._console.warn = null;
});

it('should `console.log` the message', function() {
deprecate._warn('foo');
expect(deprecate._console.log)
.to.have.been.calledOnce
.and.calledOn(deprecate._console)
.and.calledWith('foo');
});

describe('when `console.log` does not exist', function() {
it('should call `_.noop`', function() {
deprecate._console.log = null;
this.sinon.spy(_, 'noop');
deprecate._warn('foo');

expect(_.noop).to.have.been.calledOnce;
});
});
});
});

describe('when calling with a message', function() {
beforeEach(function() {
Marionette.deprecate('foo');
deprecate('foo');
});

it('should `console.warn` the message', function() {
expect(Marionette.deprecate._warn)
expect(deprecate._warn)
.to.have.been.calledOnce
.and.calledWith('Deprecation warning: foo');
});
});

describe('when calling with an object', function() {
beforeEach(function() {
Marionette.deprecate({
deprecate({
prev: 'foo',
next: 'bar'
});
});

it('should `console.warn` the message', function() {
expect(Marionette.deprecate._warn)
expect(deprecate._warn)
.to.have.been.calledOnce
.and.calledWith('Deprecation warning: foo is going to be removed in the future. Please use bar instead.');
});
});

describe('when calling with an object with a url', function() {
beforeEach(function() {
Marionette.deprecate({
deprecate({
prev: 'foo',
next: 'bar',
url: 'baz'
});
});

it('should `console.warn` the message', function() {
expect(Marionette.deprecate._warn)
expect(deprecate._warn)
.to.have.been.calledOnce
.and.calledWith('Deprecation warning: foo is going to be removed in the future. Please use bar instead. See: baz');
});
});

describe('when calling with a message and a falsy test', function() {
beforeEach(function() {
Marionette.deprecate('bar', false);
deprecate('bar', false);
});

it('should `console.warn` the message', function() {
expect(Marionette.deprecate._warn)
expect(deprecate._warn)
.to.have.been.calledOnce
.and.calledWith('Deprecation warning: bar');
});
});

describe('when calling with a message and a truthy test', function() {
beforeEach(function() {
Marionette.deprecate('Foo', true);
deprecate('Foo', true);
});

it('should not `console.warn` the message', function() {
expect(Marionette.deprecate._warn).not.to.have.been.called;
expect(deprecate._warn).not.to.have.been.called;
});
});

describe('when calling with the same message twice', function() {
beforeEach(function() {
Marionette.deprecate('baz');
Marionette.deprecate('baz');
deprecate('baz');
deprecate('baz');
});

it('should `console.warn` the message', function() {
expect(Marionette.deprecate._warn)
expect(deprecate._warn)
.to.have.been.calledOnce
.and.calledWith('Deprecation warning: baz');
});
Expand All @@ -106,11 +132,11 @@ describe('Marionette.deprecate', function() {
describe('when calling in production mode', function() {
beforeEach(function() {
Marionette.DEV_MODE = false;
Marionette.deprecate('baz');
deprecate('baz');
});

it('should `console.warn` the message', function() {
expect(Marionette.deprecate._warn).to.not.have.been.called;
expect(deprecate._warn).to.not.have.been.called;
});
});
});
11 changes: 11 additions & 0 deletions test/unit/utils/emulate-collection.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import emulateCollection from '../../../src/utils/emulate-collection';

describe('emulateCollection', function() {
it('should be able to map over list', function() {
const target = { list: [1, 2, 3] };

emulateCollection(target, 'list');

expect(target.map(v => v * 2)).to.eql([2, 4, 6]);
});
});
8 changes: 8 additions & 0 deletions test/unit/utils/get-unique-event-name.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import getUniqueEventName from '../../../src/utils/get-unique-event-name';

describe('getUniqueEventName', function() {
it('should postfix a unique id to the event', function() {
const matcher = /^(click\.evt\d+\sa\.name)$/;
expect(getUniqueEventName('click a.name')).to.match(matcher);
});
});
24 changes: 24 additions & 0 deletions test/unit/utils/proxy.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import proxy from '../../../src/utils/proxy';

describe('proxy', function() {
let method;

beforeEach(function() {
method = this.sinon.stub();
});

it('should return a function', function() {
expect(proxy(method)).to.be.a('function');
});

describe('when calling the returned function', function() {
it('should call the method on context with all arguments', function() {
const context = {};
const proxiedMethod = proxy(method);

proxiedMethod(context, 1, 2, 3);

expect(method).to.be.calledOn(context).and.calledWith(1,2,3);
});
});
});
38 changes: 38 additions & 0 deletions test/unit/utils/set-options.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import setOptions from '../../../src/utils/set-options';

describe('setOptions', function() {
let object;
const options = {
foo: 'baz',
baz: 'baz'
};

beforeEach(function() {
object = {
options() {
return {
foo: 'bar',
bar: 'baz'
};
}
};

setOptions.call(object, options);
});

it('should not mutate the options argument', function() {
expect(options).to.eql({
foo: 'baz',
baz: 'baz'
})
});

// This test covers merge order and options as a function
it('should set options on the context', function() {
expect(object.options).to.eql({
foo: 'baz',
bar: 'baz',
baz: 'baz'
});
});
});