-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshellspec.js
97 lines (97 loc) · 4.28 KB
/
shellspec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
describe("TurtleShell and TurtleShellLite", function () {
var input, event, shell;
beforeEach(function () {
input = {
className: "turtle-shell",
value: '',
focus: function () {},
form: {addEventListener: function () {}},
addEventListener: function () {}
};
spyOn(input.form, "addEventListener");
spyOn(input, "focus");
spyOn(input, "addEventListener");
event = {
target: input,
stopPropagation: function () {},
preventDefault: function () {},
};
spyOn(event, "stopPropagation");
spyOn(event, "preventDefault");
});
describe("TurtleShell.handleEvent", function () {
beforeEach(function () {
shell = new Turtle.Shell(input);
});
it("should add event listeners in the constructor", function () {
expect(input.form.addEventListener).toHaveBeenCalled();
expect(input.addEventListener).toHaveBeenCalled();
});
it("disable regular event handling", function () {
var result = shell.handleEvent(event);
expect(result).toEqual(false);
expect(event.stopPropagation).toHaveBeenCalled();
expect(event.preventDefault).toHaveBeenCalled();
});
it("should evaluate the input.value", function () {
window.evalWasCalled = 0;
input.value = 'window.evalWasCalled = 1;'
shell.handleEvent(event);
expect(window.evalWasCalled).toEqual(1);
});
it("should echo errors to the console", function () {
spyOn(window.console, "log");
input.value = 'force(anError)';
shell.handleEvent(event);
expect(window.console.log).toHaveBeenCalled();
var args = window.console.log.mostRecentCall.args[0];
expect(args.name).toEqual("ReferenceError");
});
});
describe("TurtleShellLite.handleEvent", function () {
var scope;
beforeEach(function () {
scope = jasmine.createSpyObj("scope", ["methoda", "methodA", "methodc"]);
shell = new Turtle.ShellLite(input, scope);
});
it("should add event listeners in the constructor", function () {
expect(input.form.addEventListener).toHaveBeenCalled();
expect(input.addEventListener).toHaveBeenCalled();
});
it("should split input.value & call scope[the first word]([the rest])", function () {
input.value = 'methodA argument1 argument2';
shell.handleEvent(event);
expect(scope.methoda).toHaveBeenCalled();
var args = scope.methoda.mostRecentCall.args;
expect(args[0]).toEqual("argument1");
expect(args[1]).toEqual("argument2");
expect(scope.methodA).not.toHaveBeenCalled();
});
it("should report errors to the console", function () {
spyOn(window.console, "log"); //.andCallThrough(); //handy for debugging this spec
input.value = 'force anError';
shell.handleEvent(event);
expect(window.console.log).toHaveBeenCalled();
var args = window.console.log.mostRecentCall.args;
var err = args.pop(), msg = args.shift();
expect(msg).toMatch(/cannot call force\(\) with/);
expect(err.name).toEqual("TypeError");
});
it("should recognize multiple commands delimited by period (.)", function () {
input.value = "methodA argument1 argument2. methodc argument3.";
shell.handleEvent(event);
expect(scope.methoda).toHaveBeenCalled();
var args = scope.methoda.mostRecentCall.args;
expect(args[0]).toEqual("argument1");
expect(args[1]).toEqual("argument2");
expect(scope.methodc).toHaveBeenCalled();
expect(scope.methodc.mostRecentCall.args[0]).toEqual("argument3");
});
it("should repeat a command N times if it's prefixed with Nx:", function () {
input.value = "3x: methodA argument3 argument2. methodc argument1";
shell.handleEvent(event);
expect(scope.methoda.callCount).toEqual(3);
expect(scope.methodc.callCount).toEqual(3);
});
});
});