From 4b10001b66c92edd8515abab2b6bd278c017de4c Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Sat, 19 Mar 2016 00:08:20 +0200 Subject: [PATCH 1/2] test(helpers): fix error message generation for `toHaveBeenCalledOnce[With]` matchers Jasmine 2.4 (maybe earlier) does not support returning an array containing both the "normal" and the negative error messages. It will always concatenate them. --- test/helpers/matchers.js | 92 +++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/test/helpers/matchers.js b/test/helpers/matchers.js index f345b15e5890..5d12f4f7de4f 100644 --- a/test/helpers/matchers.js +++ b/test/helpers/matchers.js @@ -131,25 +131,38 @@ beforeEach(function() { return { compare: function(actual) { if (arguments.length > 1) { - throw new Error('toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith'); + throw new Error('`toHaveBeenCalledOnce` does not take arguments, ' + + 'use `toHaveBeenCalledOnceWith`'); } if (!jasmine.isSpy(actual)) { throw new Error('Expected a spy, but got ' + jasmine.pp(actual) + '.'); } + var count = actual.calls.count(); + var pass = count === 1; + var message = function() { - var msg = 'Expected spy ' + actual.identity() + ' to have been called once, but was ', - count = this.actual.calls.count(); - return [ - count === 0 ? msg + 'never called.' : - msg + 'called ' + count + ' times.', - msg.replace('to have', 'not to have') + 'called once.' - ]; + var msg = 'Expected spy ' + actual.and.identity() + (pass ? ' not ' : ' ') + + 'to have been called once, but '; + + switch (count) { + case 0: + msg += 'it was never called.'; + break; + case 1: + msg += 'it was called once.'; + break; + default: + msg += 'it was called ' + count + ' times.'; + break; + } + + return msg; }; return { - pass: actual.calls.count() == 1, + pass: pass, message: message }; } @@ -158,43 +171,52 @@ beforeEach(function() { toHaveBeenCalledOnceWith: function(util, customEqualityTesters) { return { - compare: function(actual) { - var expectedArgs = Array.prototype.slice.call(arguments, 1); + compare: generateCompare(false), + negativeCompare: generateCompare(true) + }; + + function generateCompare(isNot) { + return function(actual) { if (!jasmine.isSpy(actual)) { throw new Error('Expected a spy, but got ' + jasmine.pp(actual) + '.'); } + + var expectedArgs = Array.prototype.slice.call(arguments, 1); + var actualCount = actual.calls.count(); + var actualArgs = actualCount && actual.calls.argsFor(0); + + var pass = (actualCount === 1) && util.equals(actualArgs, expectedArgs); + if (isNot) pass = !pass; + var message = function() { - if (actual.calls.count() != 1) { - if (actual.calls.count() === 0) { - return [ - 'Expected spy ' + actual.identity() + ' to have been called once with ' + - jasmine.pp(expectedArgs) + ' but it was never called.', - 'Expected spy ' + actual.identity() + ' not to have been called with ' + - jasmine.pp(expectedArgs) + ' but it was.' - ]; - } + var msg = 'Expected spy ' + actual.and.identity() + (isNot ? ' not ' : ' ') + + 'to have been called once with ' + jasmine.pp(expectedArgs) + ', but '; - return [ - 'Expected spy ' + actual.identity() + ' to have been called once with ' + - jasmine.pp(expectedArgs) + ' but it was called ' + actual.calls.count() + ' times.', - 'Expected spy ' + actual.identity() + ' not to have been called once with ' + - jasmine.pp(expectedArgs) + ' but it was.' - ]; + if (isNot) { + msg += 'it was.'; } else { - return [ - 'Expected spy ' + actual.identity() + ' to have been called once with ' + - jasmine.pp(expectedArgs) + ' but was called with ' + jasmine.pp(actual.calls.argsFor(0)), - 'Expected spy ' + actual.identity() + ' not to have been called once with ' + - jasmine.pp(expectedArgs) + ' but was called with ' + jasmine.pp(actual.calls.argsFor(0)) - ]; + switch (actualCount) { + case 0: + msg += 'it was never called.'; + break; + case 1: + msg += 'it was called with ' + jasmine.pp(actualArgs) + '.'; + break; + default: + msg += 'it was called ' + actualCount + ' times.'; + break; + } } + + return msg; }; + return { - pass: actual.calls.count() === 1 && util.equals(actual.calls.argsFor(0), expectedArgs), + pass: pass, message: message }; - } - }; + }; + } }, toBeOneOf: function() { From 70ea7146b7f0405b3be58846cc39c6f8c9851e38 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Sat, 19 Mar 2016 00:10:30 +0200 Subject: [PATCH 2/2] test: add TODOs and fix typos and testcase descriptions --- test/jqLiteSpec.js | 4 ++-- test/modules/no_bootstrap.js | 2 +- test/ng/anchorScrollSpec.js | 6 ++++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index d67218ae8d95..e0502073d96d 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -972,7 +972,7 @@ describe('jqLite', function() { describe('text', function() { - it('should return null on empty', function() { + it('should return `""` on empty', function() { expect(jqLite().length).toEqual(0); expect(jqLite().text()).toEqual(''); }); @@ -1040,7 +1040,7 @@ describe('jqLite', function() { describe('html', function() { - it('should return null on empty', function() { + it('should return `undefined` on empty', function() { expect(jqLite().length).toEqual(0); expect(jqLite().html()).toEqual(undefined); }); diff --git a/test/modules/no_bootstrap.js b/test/modules/no_bootstrap.js index 64a7ed0e26ed..8248e367eb70 100644 --- a/test/modules/no_bootstrap.js +++ b/test/modules/no_bootstrap.js @@ -1,3 +1,3 @@ -// When runnint the modules test, then the page should not be bootstrapped. +// When running the modules test, then the page should not be bootstrapped. window.name = "NG_DEFER_BOOTSTRAP!"; diff --git a/test/ng/anchorScrollSpec.js b/test/ng/anchorScrollSpec.js index cbe2abdfc221..1a5832c93fd7 100644 --- a/test/ng/anchorScrollSpec.js +++ b/test/ng/anchorScrollSpec.js @@ -88,6 +88,9 @@ describe('$anchorScroll', function() { return function($window) { forEach(elmSpy, function(spy, id) { var count = map[id] || 0; + // TODO(gkalpak): `toHaveBeenCalledTimes()` works correctly with 0 since + // https://github.com/jasmine/jasmine/commit/342f0eb9a38194ecb8559e7df872c72afc0fe52e + // Fix when we upgrade to a version that contains the fix. if (count > 0) { expect(spy).toHaveBeenCalledTimes(count); } else { @@ -386,6 +389,9 @@ describe('$anchorScroll', function() { return function($rootScope, $window) { inject(expectScrollingTo(identifierCountMap)); + // TODO(gkalpak): `toHaveBeenCalledTimes()` works correctly with 0 since + // https://github.com/jasmine/jasmine/commit/342f0eb9a38194ecb8559e7df872c72afc0fe52e + // Fix when we upgrade to a version that contains the fix. if (list.length > 0) { expect($window.scrollBy).toHaveBeenCalledTimes(list.length); } else {