Skip to content

Commit

Permalink
Make the history unit test date independent by adding a date object m…
Browse files Browse the repository at this point in the history
…ock.
  • Loading branch information
caedesvvv committed Oct 26, 2014
1 parent ec81fd5 commit a6205bc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
62 changes: 62 additions & 0 deletions test/mock/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Date mock object that works as if current time is fixed at
* Sun Oct 26 2014 23:12:54 GMT+0100 (CET)
*/

'use strict';

define([], function() {

var realDate;
function mockDate(val) {
if (!val) {
this.date = new realDate(1414361574805);
} else {
this.date = new realDate(val);
}
}
mockDate.hook = function() {
if (realDate) {
throw Error('Hook date called two times!');
}
realDate = window.Date;
window.Date = mockDate;
}
mockDate.restore = function() {
window.Date = realDate;
realDate = undefined;
}
mockDate.prototype.setDate = function(val) {
return this.date.setDate(val);
}
mockDate.prototype.toLocaleDateString = function() {
return this.date.toLocaleDateString();
}
mockDate.prototype.valueOf = function() {
return this.date.valueOf();
}
mockDate.prototype.getDate = function() {
return this.date.getDate();
}
mockDate.prototype.getDay = function() {
return this.date.getDay();
}
mockDate.prototype.getMonth = function() {
return this.date.getMonth();
}
mockDate.prototype.getFullYear = function() {
return this.date.getFullYear();
}
mockDate.prototype.getHours = function() {
return this.date.getHours();
}
mockDate.prototype.getMinutes = function() {
return this.date.getMinutes();
}
mockDate.prototype.getTime = function() {
return this.date.getTime();
}

return mockDate;

});
1 change: 1 addition & 0 deletions test/test-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ requirejs.config({

paths: {
'angular-mocks': '../vendors/angular-mocks/angular-mocks',
'date-mock': '../test/mock/date',
'chrome': '../test/mock/chrome_mock',
'testUtils': '../test/mock/testUtils',
'frontend/app': '../test/mock/frontend_app',
Expand Down
6 changes: 4 additions & 2 deletions test/unit/frontend/providers/historySpec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';

define(['angular-mocks', 'testUtils'], function(mocks, testUtils) {
define(['angular-mocks', 'testUtils', 'date-mock'], function(mocks, testUtils, dateMock) {
describe('History provider', function() {

var history, walletProvider, scope;

var pocket, identity;
beforeEach(function(done) {
dateMock.hook();
pocket = {
destroy: function() { this.destroyed=true;return [0, 1]; },
getWalletAddresses: function() {return [];},
Expand Down Expand Up @@ -84,6 +85,7 @@ define(['angular-mocks', 'testUtils'], function(mocks, testUtils) {
});

afterEach(function() {
dateMock.restore();
testUtils.reset();
})

Expand Down Expand Up @@ -170,7 +172,7 @@ define(['angular-mocks', 'testUtils'], function(mocks, testUtils) {
history.setHistoryFilter('monthly');
identity.fillHistoryStub();
var rows = history.chooseRows();
expect(rows.length).toEqual(70);
expect(rows.length).toEqual(76);
});

it('chooses rows weekly', function() {
Expand Down

0 comments on commit a6205bc

Please sign in to comment.