Skip to content

Commit

Permalink
WIP: Enhance template testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sh19910711 committed Jul 16, 2016
1 parent 7cc76ad commit 2be719d
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ matrix:
- env: TEST_SUITE=test:templates
include:
- env: TEST_SUITE=test:templates
rvm: 2.2
rvm: 2.2.5
24 changes: 15 additions & 9 deletions lib/web_console/tasks/test_templates.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ namespace :test do
task templates: "templates:all"

namespace :templates do
task all: [ :daemonize, :npm, :rackup, :wait, :mocha, :kill, :exit ]
task all: [ :daemonize, :npm, :rackup, :wait, :spec, :test, :kill, :exit ]
task serve: [ :npm, :rackup ]

work_dir = Pathname(EXPANDED_CWD).join("test/templates")
workdir = Pathname(EXPANDED_CWD).join("test/templates")
pid_file = Pathname(Dir.tmpdir).join("web_console.#{SecureRandom.uuid}.pid")
runner_uri = URI.parse("http://localhost:29292/html/spec_runner.html")
rackup_opts = "-p #{runner_uri.port}"
html_uri = URI.parse("http://#{ENV['IP'] || '127.0.0.1'}:#{ENV['PORT'] || 29292}/html/")
spec_runner = 'spec_runner.html'
test_runner = 'test_runner.html'
rackup_opts = "--host #{html_uri.host} --port #{html_uri.port}"
test_result = nil

def need_to_wait?(uri)
Expand All @@ -23,20 +25,24 @@ namespace :test do
end

task :npm do
Dir.chdir(work_dir) { system "npm install --silent" }
Dir.chdir(workdir) { system "npm install --silent" }
end

task :rackup do
Dir.chdir(work_dir) { system "bundle exec rackup #{rackup_opts}" }
Dir.chdir(workdir) { system "bundle exec rackup #{rackup_opts}" }
end

task :wait do
cnt = 0
need_to_wait?(runner_uri) { sleep 1; cnt += 1; cnt < 5 }
need_to_wait?(URI.join(html_uri, spec_runner)) { sleep 1; cnt += 1; cnt < 5 }
end

task :mocha do
Dir.chdir(work_dir) { test_result = system("$(npm bin)/mocha-phantomjs #{runner_uri}") }
task :spec do
Dir.chdir(workdir) { test_result = system("./node_modules/.bin/mocha-phantomjs #{URI.join(html_uri, spec_runner)}") }
end

task :test do
Dir.chdir(workdir) { test_result = system("./node_modules/.bin/mocha-phantomjs #{URI.join(html_uri, test_runner)}") }
end

task :kill do
Expand Down
2 changes: 1 addition & 1 deletion lib/web_console/testing/fake_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call(env)
end

def view
@view ||= View.new(@view_path)
@view = View.new(@view_path)
end

private
Expand Down
7 changes: 7 additions & 0 deletions test/templates/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ map "/spec" do
)
end

map "/test" do
run WebConsole::Testing::FakeMiddleware.new(
req_path_regex: %r{^/test/(.*)},
view_path: TEST_ROOT.join("test"),
)
end

map "/templates" do
run WebConsole::Testing::FakeMiddleware.new(
req_path_regex: %r{^/templates/(.*)},
Expand Down
4 changes: 2 additions & 2 deletions test/templates/html/spec_runner.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

<!-- find and load test cases -->
<script src="/spec/spec_helper.js"></script>
<% Pathname.glob(TEST_ROOT.join "spec/**/*_spec.js") do |spec| %>
<script src="/<%= spec.relative_path_from TEST_ROOT %>"></script>
<% Pathname.glob(TEST_ROOT.join "spec/**/*_spec.js") do |t| %>
<script src="/<%= t.relative_path_from TEST_ROOT %>"></script>
<% end %>

<!-- run tests -->
Expand Down
36 changes: 36 additions & 0 deletions test/templates/html/test_runner.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Templates Test</title>
<link rel="stylesheet" href="/node_modules/mocha/mocha.css" />
</head>
<body>
<!-- setup for mocha -->
<div id="mocha"></div>
<script src="/node_modules/mocha/mocha.js"></script>
<script src="/node_modules/chai/chai.js"></script>
<script>
mocha.setup('tdd');
mocha.reporter('html');
</script>

<!-- load templates -->
<script src="/templates/console.js"></script>

<!-- find and load test cases -->
<script src="/spec/spec_helper.js"></script>
<% Pathname.glob(TEST_ROOT.join "test/**/*_test.js") do |t| %>
<script src="/<%= t.relative_path_from TEST_ROOT %>"></script>
<% end %>

<!-- run tests -->
<script>
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}
</script>
</body>
</html>
37 changes: 37 additions & 0 deletions test/templates/spec/auto_complete_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
describe("Auto Complete", function() {
beforeEach(function() {
var self = this.autoComplete = new Autocomplete(["something", "somewhat", "somewhere"], 'some');
this.moveNext = function(times) {
for (var i = 0; i < times; ++i) self.next();
};
this.assertSelect = function(pos) {
assert.equal(self.current, pos);
};
});
describe("move functions", function() {
context("set up with three elements", function() {
it("should have three elements", function() {
assert.ok(this.autoComplete.view.children.length === 3)
});
it("should have no selected element", function() {
assert.ok(this.autoComplete.view.getElementsByClassName('selected').length === 0);
});
context("move next two times", function() {
beforeEach(function() { this.moveNext(2) });
it("should point the 1-th element", function() { this.assertSelect(1); });
context("back once", function() {
beforeEach(function() { this.autoComplete.back(); });
it("should point the 0-th element", function() { this.assertSelect(0); });
context("back once again", function() {
beforeEach(function() { this.autoComplete.back(); });
it("should point the last element", function() { this.assertSelect(2); });
});
});
context("move next two times again", function() {
beforeEach(function() { this.moveNext(2) });
it("should back to the first of list", function() { this.assertSelect(0); });
});
});
});
});
});
42 changes: 42 additions & 0 deletions test/templates/spec/repl_console_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
describe("REPLConsole", function() {
SpecHelper.prepareStageElement();

describe("#swapWord", function() {
beforeEach(function() {
var elm = document.createElement('div');
elm.innerHTML = '<div id="console"></div>';
this.stageElement.appendChild(elm);
var consoleOptions = { mountPoint: '/mock', sessionId: 'result' };
this.console = REPLConsole.installInto('console', consoleOptions);
});
context("caret points to last item", function() {
beforeEach(function() {
this.console.setInput('hello world');
this.console.swapCurrentWord('swapped');
});
it('should be last word', function() { assert.equal(this.console._input, 'hello swapped'); });
});
context("points to first item", function() {
beforeEach(function() {
this.console.setInput('hello world', 3);
this.console.swapCurrentWord('swapped');
});
it('should be first word', function() { assert.equal(this.console._input, 'swapped world'); });
});
});

describe("#getCurrentWord", function() {
beforeEach(function() {
var elm = document.createElement('div');
elm.innerHTML = '<div id="console"></div>';
this.stageElement.appendChild(elm);
var consoleOptions = { mountPoint: '/mock', sessionId: 'result' };
this.console = REPLConsole.installInto('console', consoleOptions);
});
context("caret points to last item", function() {
beforeEach(function() { this.console.setInput('hello world'); });
it('should be last word', function() { assert.equal(this.console.getCurrentWord(), 'world'); });
});
context("points to first item", function() {
beforeEach(function() { this.console.setInput('hello world', 0); });
it('should be first word', function() { assert.equal(this.console.getCurrentWord(), 'hello'); });
});
});

describe("#commandHandle", function() {
function runCommandHandle(self, consoleOptions, callback) {
self.console = REPLConsole.installInto('console', consoleOptions);
Expand Down
4 changes: 2 additions & 2 deletions test/web_console/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def body
get '/', params: nil
put "/repl_sessions/#{session.id}", xhr: true, params: { input: '__LINE__' }

assert_equal({ output: "=> #{line}\n" }.to_json, response.body)
assert_equal("=> #{line}\n", JSON.parse(response.body)["output"])
end

test 'can switch bindings on error pages' do
Expand All @@ -151,7 +151,7 @@ def body
session, line = Session.new([binding]), __LINE__
put "/customized/path/repl_sessions/#{session.id}", params: { input: '__LINE__' }, xhr: true

assert_equal({ output: "=> #{line}\n" }.to_json, response.body)
assert_equal("=> #{line}\n", JSON.parse(response.body)["output"])
end

test 'unavailable sessions respond to the user with a message' do
Expand Down

0 comments on commit 2be719d

Please sign in to comment.