Skip to content

Commit

Permalink
added more test cases to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ferronrsmith committed Oct 7, 2013
1 parent 244d672 commit cc2df2f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
7 changes: 6 additions & 1 deletion js/src/datespy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
41 changes: 34 additions & 7 deletions test/spec/test-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
36 changes: 34 additions & 2 deletions test/spec/test.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down Expand Up @@ -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();

});
});

0 comments on commit cc2df2f

Please sign in to comment.