piskel/test/js/service/HistoryServiceTest.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-08-12 09:11:23 +04:00
var callFactory = function (method) {
return {
times : function (times) {
var results = [];
for (var i = 0 ; i < times ; i++) {
results.push(method());
}
return results;
},
once : function () {
return method();
}
};
};
2014-07-23 21:42:08 +04:00
describe("History Service suite", function() {
2014-08-12 09:11:23 +04:00
var SERIALIZED_PISKEL = 'serialized-piskel';
var historyService = null;
2014-08-12 02:30:57 +04:00
2014-08-12 09:11:23 +04:00
var getLastState = function () {
return historyService.stateQueue[historyService.currentIndex];
};
var createMockHistoryService = function () {
2014-08-12 02:30:57 +04:00
var mockPiskelController = {
serialize : function () {
2014-08-12 09:11:23 +04:00
return SERIALIZED_PISKEL;
2014-08-12 02:30:57 +04:00
}
};
var mockShortcutService = {
addShortcut : function () {}
};
2014-08-12 09:11:23 +04:00
return new pskl.service.HistoryService(mockPiskelController, mockShortcutService);
};
it("starts at -1", function() {
historyService = createMockHistoryService();
expect(historyService.currentIndex).toBe(-1);
});
it("is at 0 after init", function() {
historyService = createMockHistoryService();
2014-08-12 02:30:57 +04:00
historyService.init();
2014-07-23 21:42:08 +04:00
expect(historyService.currentIndex).toBe(0);
});
2014-08-12 09:11:23 +04:00
var sendSaveEvents = function (type) {
return callFactory (function () {
$.publish(Events.PISKEL_SAVE_STATE, {
type : type,
scope : {},
replay : {}
});
});
};
it("stores a piskel snapshot after 5 SAVE", function () {
// BEFORE
var SNAPSHOT_PERIOD_BACKUP = pskl.service.HistoryService.SNAPSHOT_PERIOD;
pskl.service.HistoryService.SNAPSHOT_PERIOD = 5;
historyService = createMockHistoryService();
historyService.init();
sendSaveEvents(pskl.service.HistoryService.REPLAY).times(5);
expect(historyService.currentIndex).toBe(5);
expect(getLastState().piskel).toBe(SERIALIZED_PISKEL);
sendSaveEvents(pskl.service.HistoryService.REPLAY).times(4);
sendSaveEvents(pskl.service.HistoryService.REPLAY_NO_SNAPSHOT).once();
expect(getLastState().piskel).toBeUndefined();
sendSaveEvents(pskl.service.HistoryService.REPLAY_NO_SNAPSHOT).once();
expect(getLastState().piskel).toBeUndefined();
sendSaveEvents(pskl.service.HistoryService.REPLAY).once();
expect(getLastState().piskel).toBe(SERIALIZED_PISKEL);
// AFTER
pskl.service.HistoryService.SNAPSHOT_PERIOD = SNAPSHOT_PERIOD_BACKUP;
})
2014-07-23 21:42:08 +04:00
});