From cc2df2fc1b7b95c67e1ac3a911e40443074bb927 Mon Sep 17 00:00:00 2001 From: ferron Date: Mon, 7 Oct 2013 16:52:16 -0400 Subject: [PATCH] added more test cases to increase coverage --- js/src/datespy.js | 7 ++++++- test/spec/test-angular.js | 41 ++++++++++++++++++++++++++++++++------- test/spec/test.js | 36 ++++++++++++++++++++++++++++++++-- 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/js/src/datespy.js b/js/src/datespy.js index c745901..b291d64 100644 --- a/js/src/datespy.js +++ b/js/src/datespy.js @@ -353,13 +353,18 @@ datespy.timers = { Date: Date }; +function detectTestFramework() { + return undefined !== window.jasmine && + window.angular !== undefined && angular.mock !== undefined; +} + /** * Helps IE run the fake timers. By defining global functions, IE allows * them to be overwritten at a later point. If these are not defined like * this, overwriting them will result in anything from an exception to browser * crash. */ -if (/MSIE ([0-9]{1,}[\.0-9]{0,})/.test(navigator.userAgent)) { +if (/MSIE ([0-9]{1,}[\.0-9]{0,})/.test(navigator.userAgent) || detectTestFramework()) { (function () { "use strict"; function setTimeout() {} diff --git a/test/spec/test-angular.js b/test/spec/test-angular.js index a07895e..2a92499 100644 --- a/test/spec/test-angular.js +++ b/test/spec/test-angular.js @@ -170,18 +170,45 @@ describe('Test angular support', function () { expect(dCallback.callCount).toBe(0); }); - it('expect time to be zero', function () { + it("throws for invalid minutes", function () { + var dCallback = jasmine.createSpy('dateCallback'); + setInterval(dCallback, 10000); + + expect(function () { + clock.tick("67:10"); + }).toThrow(); + + expect(dCallback.callCount).toBe(0); + }); + + it("throws for invalid minutes", function () { + var dCallback = jasmine.createSpy('dateCallback'); + setInterval(dCallback, 10000); + + expect(function () { + clock.tick("67:10"); + }).toThrow(); + + expect(dCallback.callCount).toBe(0); + }); + + it('Expect time to be zero', function () { datespy.clock.create(0); // reset clock to the beginning of time expect(new Date().getTime()).toBe(0); }); - it("Expects date2 to be 3 ticks ahead of date1", function () { - var date1, date2; - date1 = new datespy.clock.Date(); - clock.tick(3); - date2 = new datespy.clock.Date(); + it("Expects date epoch time to be 0 if string is empty", function () { + clock.tick(""); + expect(new Date().getTime()).toBe(0); + }); - expect(date2.getTime() - date1.getTime()).toBe(3); + it('Expect', function () { + var dCallback = jasmine.createSpy('dateCallback'), + tId; + tId = setTimeout(dCallback, 5000); + clock.tick(4999); + clearTimeout(tId); + expect(dCallback.callCount).toBe(0); }); it("creates regular date when passing timestamp", function () { diff --git a/test/spec/test.js b/test/spec/test.js index 57270f9..34b4386 100644 --- a/test/spec/test.js +++ b/test/spec/test.js @@ -1,5 +1,5 @@ -/*jslint browser : true */ -/*global describe, expect, it, beforeEach, afterEach, datespy, spyOn, jasmine */ +/*jslint sub : true, newcap : true */ +/*global describe, expect, it, beforeEach, afterEach, datespy, spyOn, jasmine, setTimeout, setInterval, clearTimeout, clearInterval, navigator:true */ var originalDate = new Date(); describe('Testing date spy library', function () { @@ -53,4 +53,36 @@ describe('Test to ensure that date spy functionality restore clock', function () expect(new Date().getDate()).toBe(originalDate.getDate()); expect(new Date().getDay()).toBe(originalDate.getDay()); }); +}); + + +describe('Test IE Support', function () { + "use strict"; + var clock; + + beforeEach(function () { + clock = datespy.useFakeTimers(); + }); + + afterEach(function () { + clock.restore(); + }); + + it('Testing IE', function () { + // Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0) + var originalNavigator = null; + if (!(/MSIE ([0-9]{1,}[\.0-9]{0,})/.test(navigator.userAgent))) { + originalNavigator = navigator; + navigator = {}; + navigator['__proto__'] = originalNavigator; + navigator['__defineGetter__']('userAgent', function () { return 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'; }); + } + expect(navigator.userAgent).toBe('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)'); + expect(setTimeout).toBeDefined(); + expect(clearTimeout).toBeDefined(); + expect(setInterval).toBeDefined(); + expect(clearInterval).toBeDefined(); + expect(Date).toBeDefined(); + + }); }); \ No newline at end of file