Skip to content

Commit

Permalink
Add tests for REPLConsole#commandHandle()
Browse files Browse the repository at this point in the history
And add a callback to REPLConsole#commandHandle() for testing
  • Loading branch information
sh19910711 committed Jul 29, 2015
1 parent 0dc2aa3 commit 5d66fa3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/web_console/templates/console.js.erb
Expand Up @@ -71,21 +71,24 @@ REPLConsole.prototype.getUrl = function(path) {
}
};

REPLConsole.prototype.commandHandle = function(line) {
REPLConsole.prototype.commandHandle = function(line, callback) {
var self = this;
var params = 'input=' + encodeURIComponent(line);
callback = callback || function() {};

function isSuccess(status) {
return status >= 200 && status < 300 || status === 304;
}

putRequest(self.getUrl(), params, function(xhr) {
var response = JSON.parse(xhr.responseText);
if (isSuccess(xhr.status)) {
var result = isSuccess(xhr.status);
if (result) {
self.writeOutput(response.output);
} else {
self.writeError(response.output);
}
callback(result, response);
});
};

Expand Down
12 changes: 12 additions & 0 deletions test/templates/config.ru
Expand Up @@ -81,3 +81,15 @@ map "/templates" do
view_path: TEST_ROOT.join("../../lib/web_console/templates"),
)
end

map "/mock/repl/result" do
headers = { 'Content-Type' => 'application/json' }
body = [ { output: '=> "fake-result"\n' }.to_json ]
run lambda { |env| [200, headers, body] }
end

map "/mock/repl/error" do
headers = { 'Content-Type' => 'application/json' }
body = [ { output: 'fake-error-message' }.to_json ]
run lambda { |env| [400, headers, body] }
end
52 changes: 52 additions & 0 deletions test/templates/spec/repl_console_spec.js
@@ -1,6 +1,58 @@
describe("REPLConsole", function() {
SpecHelper.prepareStageElement();

describe("#commandHandle()", function() {
beforeEach(function() {
this.elm = document.createElement('div');
this.elm.innerHTML = '<div id="console"></div>';
this.stageElement.appendChild(this.elm);
});

context("remotePath: /mock/repl/result", function() {
beforeEach(function(done) {
var self = this;
self.console = REPLConsole.installInto('console', { remotePath: '/mock/repl/result' });
self.console.commandHandle('fake-input', function(result, response) {
self.result = result;
self.response = response;
self.message = self.elm.getElementsByClassName('console-message')[0];
done();
});
});
it("should be a successful request", function() {
assert.ok(this.result);
})
it("should have fake-result in output", function() {
assert.match(this.response.output, /"fake-result"/);
});
it("should not have .error-message", function() {
assert.notOk(hasClass(this.message, 'error-message'));
});
});

context("remotePath: /mock/repl/error", function() {
beforeEach(function(done) {
var self = this;
self.console = REPLConsole.installInto('console', { remotePath: '/mock/repl/error' });
self.console.commandHandle('fake-input', function(result, response) {
self.result = result;
self.response = response;
self.message = self.elm.getElementsByClassName('console-message')[0];
done();
});
});
it("should not be a successful request", function() {
assert.notOk(this.result);
});
it("should have fake-error-message in output", function() {
assert.match(this.response.output, /fake-error-message/);
});
it("should have .error-message", function() {
assert.ok(hasClass(this.message, 'error-message'));
});
});
});

describe(".installInto()", function() {
beforeEach(function() {
this.elm = document.createElement('div');
Expand Down

0 comments on commit 5d66fa3

Please sign in to comment.