diff --git a/.travis.yml b/.travis.yml index edfff988..6eff9fde 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,4 +23,4 @@ matrix: - env: TEST_SUITE=test:templates include: - env: TEST_SUITE=test:templates - rvm: 2.2 + rvm: 2.2.5 diff --git a/lib/web_console/tasks/test_templates.rake b/lib/web_console/tasks/test_templates.rake index a5b6d5c4..f00e5636 100644 --- a/lib/web_console/tasks/test_templates.rake +++ b/lib/web_console/tasks/test_templates.rake @@ -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) @@ -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 diff --git a/lib/web_console/testing/fake_middleware.rb b/lib/web_console/testing/fake_middleware.rb index 95a75cc1..b58731a6 100644 --- a/lib/web_console/testing/fake_middleware.rb +++ b/lib/web_console/testing/fake_middleware.rb @@ -21,7 +21,7 @@ def call(env) end def view - @view ||= View.new(@view_path) + @view = View.new(@view_path) end private diff --git a/test/templates/config.ru b/test/templates/config.ru index 474169c0..e8bd6cb8 100644 --- a/test/templates/config.ru +++ b/test/templates/config.ru @@ -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/(.*)}, diff --git a/test/templates/html/spec_runner.html.erb b/test/templates/html/spec_runner.html.erb index b577d20f..436291be 100644 --- a/test/templates/html/spec_runner.html.erb +++ b/test/templates/html/spec_runner.html.erb @@ -20,8 +20,8 @@ - <% Pathname.glob(TEST_ROOT.join "spec/**/*_spec.js") do |spec| %> - + <% Pathname.glob(TEST_ROOT.join "spec/**/*_spec.js") do |t| %> + <% end %> diff --git a/test/templates/html/test_runner.html.erb b/test/templates/html/test_runner.html.erb new file mode 100644 index 00000000..c3d4beb7 --- /dev/null +++ b/test/templates/html/test_runner.html.erb @@ -0,0 +1,36 @@ + + + + + Templates Test + + + + +
+ + + + + + + + + + <% Pathname.glob(TEST_ROOT.join "test/**/*_test.js") do |t| %> + + <% end %> + + + + + diff --git a/test/templates/spec/auto_complete_spec.js b/test/templates/spec/auto_complete_spec.js new file mode 100644 index 00000000..fccf259f --- /dev/null +++ b/test/templates/spec/auto_complete_spec.js @@ -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); }); + }); + }); + }); + }); +}); diff --git a/test/templates/spec/repl_console_spec.js b/test/templates/spec/repl_console_spec.js index 61587930..860b3e6f 100644 --- a/test/templates/spec/repl_console_spec.js +++ b/test/templates/spec/repl_console_spec.js @@ -1,6 +1,48 @@ describe("REPLConsole", function() { SpecHelper.prepareStageElement(); + describe("#swapWord", function() { + beforeEach(function() { + var elm = document.createElement('div'); + elm.innerHTML = '
'; + 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 = '
'; + 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); diff --git a/test/web_console/middleware_test.rb b/test/web_console/middleware_test.rb index 0f7cd779..65909481 100644 --- a/test/web_console/middleware_test.rb +++ b/test/web_console/middleware_test.rb @@ -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 @@ -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