Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmsogn committed Dec 19, 2014
2 parents e91f51a + 4282dff commit 7ebea48
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 8 deletions.
233 changes: 232 additions & 1 deletion test/StageSpec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe("Stage", function () {
describe("jsCanvasNinja.Stage", function () {
var stage;

beforeEach(function () {
Expand All @@ -14,4 +14,235 @@ describe("Stage", function () {
expect(stage.getMode()).toEqual('insert');
});

describe("toDataURL()", function () {

it("should not trigger beforeToDataURL when it's not function.", function () {
pending();
});

it("should trigger beforeToDataURL before Stage_toDataURL().", function () {
var count = 0;

stage.beforeToDataURL = function () {
count = 1;
}
spyOn(stage, 'Stage_toDataURL').and.callFake(function () {
count = 2;
});

spyOn(stage, 'beforeToDataURL').and.callThrough();

stage.toDataURL();

expect(stage.beforeToDataURL).toHaveBeenCalled();
expect(count).toBe(2);
});

it("should not trigger afterToDataURL when it's not function.", function () {
pending();
});

it("should trigger afterToDataURL after Stage_toDataUR().", function () {
var count = 0;

stage.afterToDataURL = function () {
count = 1;
}
spyOn(stage, 'Stage_toDataURL').and.callFake(function () {
count = 2;
});

spyOn(stage, 'afterToDataURL').and.callThrough();

stage.toDataURL();

expect(stage.afterToDataURL).toHaveBeenCalled();
expect(count).toBe(1);
});

});

describe("update()", function () {

it("should not trigger _createHistory() when argument is not truth.", function () {
spyOn(stage, '_createHistory');
stage.update();
expect(stage._createHistory).not.toHaveBeenCalled();
});

it("should trigger _createHistory() when argument is truth.", function () {
spyOn(stage, '_createHistory');
stage.update(true);
expect(stage._createHistory).toHaveBeenCalled();
});

it("should trigger _updateFrame(), Stage_update() and _createHistory() with right order.", function () {
var count = 0;

spyOn(stage, '_updateFrame').and.callFake(function () {
count = 0;
});
spyOn(stage, 'Stage_update').and.callFake(function () {
count++;
});
spyOn(stage, '_createHistory').and.callFake(function () {
count *= 2;
});

stage.update(true);

expect(stage._updateFrame.calls.count()).toEqual(1);
expect(stage.Stage_update.calls.count()).toEqual(1);
expect(stage._createHistory.calls.count()).toEqual(1);
expect(count).toEqual(2);
});

});

describe("canUndo()", function () {

it("should return false when there are not anything to undo.", function () {
expect(stage.canUndo()).toBeFalsy();

stage.update(true);
stage.undo();
expect(stage.canUndo()).toBeFalsy();
});

it("should return true when there are something to undo.", function () {
stage.update(true);
expect(stage.canUndo()).toBeTruthy();
});

});

describe("canRedo()", function () {

it("should return false when there are not anything to redo.", function () {
expect(stage.canRedo()).toBeFalsy();

stage.update(true);
stage.undo();
stage.redo();
expect(stage.canRedo()).toBeFalsy();
});

it("should return true when there are something to redo.", function () {
stage.update(true);
stage.undo();
expect(stage.canRedo()).toBeTruthy();
});

});

describe("undo()", function () {

it("should not trigger onUndo when it's not function.", function () {
pending();
});

it("should trigger onUndo.", function () {
stage.onUndo = function () {
};
spyOn(stage, 'onUndo');

stage.update(true);
stage.undo();

expect(stage.onUndo).toHaveBeenCalledWith({canRedo: true, canUndo: false});
});

it("should pop command from 'didCommands', then trigger it's unexecute() and push to 'undidCommands'.", function () {
stage.update(true);
stage.update(true);

expect(stage.didCommands.length).toEqual(2);
expect(stage.undidCommands.length).toEqual(0);

var command1 = stage.didCommands[0];
var command2 = stage.didCommands[1];

spyOn(command2, 'unexecute');
stage.undo();

expect(stage.didCommands.length).toEqual(1);
expect(stage.undidCommands.length).toEqual(1);
expect(stage.didCommands[0]).toBe(command1);
expect(stage.undidCommands[0]).toBe(command2);

expect(command2.unexecute).toHaveBeenCalled();
});

});

describe("redo()", function () {

it("should not trigger onRedo when it's not function.", function () {
pending();
});

it("should trigger onRedo.", function () {
stage.onRedo = function () {
};
spyOn(stage, 'onRedo');

stage.update(true);
stage.undo();
stage.redo();

expect(stage.onRedo).toHaveBeenCalledWith({canRedo: false, canUndo: true});
});

it("should pop command from 'undidCommands', then trigger it's execute() and push to 'didCommands'.", function () {
stage.update(true);
stage.update(true);
stage.undo();

expect(stage.didCommands.length).toEqual(1);
expect(stage.undidCommands.length).toEqual(1);

var command1 = stage.didCommands[0];
var command2 = stage.undidCommands[0];

spyOn(command2, 'execute');
stage.redo();

expect(stage.didCommands.length).toEqual(2);
expect(stage.undidCommands.length).toEqual(0);
expect(stage.didCommands[0]).toBe(command1);
expect(stage.didCommands[1]).toBe(command2);

expect(command2.execute).toHaveBeenCalled();
});

});

describe("undo() and redo()", function () {

it("should store undidCommand and didCommand appropriately.", function () {
expect(stage.didCommands.length).toEqual(0);
expect(stage.undidCommands.length).toEqual(0);

stage.update(true);
stage.update(true);
expect(stage.didCommands.length).toEqual(2);
expect(stage.undidCommands.length).toEqual(0);

stage.undo();
expect(stage.didCommands.length).toEqual(1);
expect(stage.undidCommands.length).toEqual(1);

stage.redo();
expect(stage.didCommands.length).toEqual(2);
expect(stage.undidCommands.length).toEqual(0);

stage.undo();
stage.undo();
stage.update(true);
expect(stage.didCommands.length).toEqual(1);
expect(stage.undidCommands.length).toEqual(0);
});

});

});
4 changes: 2 additions & 2 deletions test/TransformSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("jsCanvasNinja.Transform", function () {
}).toThrow();
});

describe("::rotate()", function () {
describe("rotate()", function () {

it("should rotate central coordinate object base on it's center point.", function () {
var scaleX = 2;
Expand Down Expand Up @@ -67,7 +67,7 @@ describe("jsCanvasNinja.Transform", function () {

});

describe("::scale()", function () {
describe("scale()", function () {

it("should scale with object's scale when arguments are null.", function () {
var scaleX = 2;
Expand Down
10 changes: 5 additions & 5 deletions test/UtilitySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("jsCanvasNinja.Utility", function () {
}).toThrow();
});

describe("::isText()", function () {
describe("isText()", function () {

it("should return true with object having text property.", function () {
expect(jsCanvasNinja.Utility.isText({text: "foo"})).toBeTruthy();
Expand All @@ -22,7 +22,7 @@ describe("jsCanvasNinja.Utility", function () {

});

describe("::hasColor()", function () {
describe("hasColor()", function () {

it("should return true with object having color property.", function () {
expect(jsCanvasNinja.Utility.hasColor({color: "foo"})).toBeTruthy();
Expand All @@ -38,7 +38,7 @@ describe("jsCanvasNinja.Utility", function () {

});

describe("::hasHeight()", function () {
describe("hasHeight()", function () {

it("should return true with object having valid height.", function () {
expect(jsCanvasNinja.Utility.hasHeight({height: 1})).toBeTruthy();
Expand All @@ -58,7 +58,7 @@ describe("jsCanvasNinja.Utility", function () {

});

describe("::hasWidth()", function () {
describe("hasWidth()", function () {

it("should return true with object having valid width.", function () {
expect(jsCanvasNinja.Utility.hasWidth({width: 1})).toBeTruthy();
Expand All @@ -78,7 +78,7 @@ describe("jsCanvasNinja.Utility", function () {

});

describe("::isCentralCoordinate()", function () {
describe("isCentralCoordinate()", function () {

it("should return true with object having center coordinate type.", function () {
expect(jsCanvasNinja.Utility.isCentralCoordinate({_type: 1})).toBeTruthy();
Expand Down

0 comments on commit 7ebea48

Please sign in to comment.