From 77d6f67f002c2106186b692d4a34482a15ce4d0b Mon Sep 17 00:00:00 2001 From: Hiroyuki Sano Date: Wed, 20 Jul 2016 08:13:14 +0000 Subject: [PATCH 1/5] Rename test_templates.rake => templates.rake --- lib/web_console/tasks/{test_templates.rake => templates.rake} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/web_console/tasks/{test_templates.rake => templates.rake} (100%) diff --git a/lib/web_console/tasks/test_templates.rake b/lib/web_console/tasks/templates.rake similarity index 100% rename from lib/web_console/tasks/test_templates.rake rename to lib/web_console/tasks/templates.rake From f5fdfc436c577af53eee2ab09ce8b2811cfa5a16 Mon Sep 17 00:00:00 2001 From: Hiroyuki Sano Date: Wed, 20 Jul 2016 08:14:11 +0000 Subject: [PATCH 2/5] Use non-nested tasks for templates --- .travis.yml | 4 +- lib/web_console/tasks/templates.rake | 102 +++++++++++++-------------- 2 files changed, 51 insertions(+), 55 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6eff9fde..a75bf12a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ matrix: - rvm: ruby-head - rvm: rbx-2 - rvm: jruby - - env: TEST_SUITE=test:templates + - env: TEST_SUITE=templates:test include: - - env: TEST_SUITE=test:templates + - env: TEST_SUITE=templates:test rvm: 2.2.5 diff --git a/lib/web_console/tasks/templates.rake b/lib/web_console/tasks/templates.rake index f00e5636..edce62c4 100644 --- a/lib/web_console/tasks/templates.rake +++ b/lib/web_console/tasks/templates.rake @@ -1,56 +1,52 @@ -namespace :test do +namespace :templates do desc "Run tests for templates" - task templates: "templates:all" - - namespace :templates do - task all: [ :daemonize, :npm, :rackup, :wait, :spec, :test, :kill, :exit ] - task serve: [ :npm, :rackup ] - - workdir = Pathname(EXPANDED_CWD).join("test/templates") - pid_file = Pathname(Dir.tmpdir).join("web_console.#{SecureRandom.uuid}.pid") - 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) - Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path) } - rescue Errno::ECONNREFUSED - retry if yield - end - - task :daemonize do - rackup_opts += " -D -P #{pid_file}" - end - - task :npm do - Dir.chdir(workdir) { system "npm install --silent" } - end - - task :rackup do - Dir.chdir(workdir) { system "bundle exec rackup #{rackup_opts}" } - end - - task :wait do - cnt = 0 - need_to_wait?(URI.join(html_uri, spec_runner)) { sleep 1; cnt += 1; cnt < 5 } - end - - 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 - system "kill #{File.read pid_file}" - end - - task :exit do - exit test_result - end + task test: [ :daemonize, :npm, :rackup, :wait, :spec, :test, :kill, :exit ] + task serve: [ :npm, :rackup ] + + workdir = Pathname(EXPANDED_CWD).join("test/templates") + pid_file = Pathname(Dir.tmpdir).join("web_console.#{SecureRandom.uuid}.pid") + 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) + Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path) } + rescue Errno::ECONNREFUSED + retry if yield + end + + task :daemonize do + rackup_opts += " -D -P #{pid_file}" + end + + task :npm do + Dir.chdir(workdir) { system "npm install --silent" } + end + + task :rackup do + Dir.chdir(workdir) { system "bundle exec rackup #{rackup_opts}" } + end + + task :wait do + cnt = 0 + need_to_wait?(URI.join(html_uri, spec_runner)) { sleep 1; cnt += 1; cnt < 5 } + end + + 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 + system "kill #{File.read pid_file}" + end + + task :exit do + exit test_result end end From 12e9228ce6315b727d3b119f0ae49947792ecdd8 Mon Sep 17 00:00:00 2001 From: Hiroyuki Sano Date: Wed, 20 Jul 2016 08:16:14 +0000 Subject: [PATCH 3/5] Enhance rake tasks for templates --- lib/web_console/tasks/templates.rake | 46 +++++++++++++++------------- test/templates/package.json | 2 +- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/web_console/tasks/templates.rake b/lib/web_console/tasks/templates.rake index edce62c4..bf0fe14e 100644 --- a/lib/web_console/tasks/templates.rake +++ b/lib/web_console/tasks/templates.rake @@ -1,15 +1,14 @@ namespace :templates do - desc "Run tests for templates" - task test: [ :daemonize, :npm, :rackup, :wait, :spec, :test, :kill, :exit ] + desc 'Run tests for templates' + task test: [ :daemonize, :npm, :rackup, :wait, :mocha, :kill, :exit ] task serve: [ :npm, :rackup ] - workdir = Pathname(EXPANDED_CWD).join("test/templates") - pid_file = Pathname(Dir.tmpdir).join("web_console.#{SecureRandom.uuid}.pid") - 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 + workdir = Pathname(EXPANDED_CWD).join('test/templates') + pid = Pathname(Dir.tmpdir).join("web_console_test.pid") + runner = URI.parse("http://#{ENV['IP'] || '127.0.0.1'}:#{ENV['PORT'] || 29292}/html/test_runner.html") + rackup = "rackup --host #{runner.host} --port #{runner.port}" + result = nil + browser = 'phantomjs' def need_to_wait?(uri) Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path) } @@ -18,35 +17,38 @@ namespace :templates do end task :daemonize do - rackup_opts += " -D -P #{pid_file}" + rackup += " -D --pid #{pid}" end - task :npm do - Dir.chdir(workdir) { system "npm install --silent" } + task :npm => [ :phantomjs ] do + Dir.chdir(workdir) { system 'npm install --silent' } + end + + task :phantomjs do + unless system("which #{browser} >/dev/null") + browser = './node_modules/.bin/phantomjs' + Dir.chdir(workdir) { system("test -f #{browser} || npm install --silent phantomjs-prebuilt") } + end end task :rackup do - Dir.chdir(workdir) { system "bundle exec rackup #{rackup_opts}" } + Dir.chdir(workdir) { system rackup } end task :wait do cnt = 0 - need_to_wait?(URI.join(html_uri, spec_runner)) { sleep 1; cnt += 1; cnt < 5 } - end - - task :spec do - Dir.chdir(workdir) { test_result = system("./node_modules/.bin/mocha-phantomjs #{URI.join(html_uri, spec_runner)}") } + need_to_wait?(runner) { sleep 1; cnt += 1; cnt < 5 } end - task :test do - Dir.chdir(workdir) { test_result = system("./node_modules/.bin/mocha-phantomjs #{URI.join(html_uri, test_runner)}") } + task :mocha do + Dir.chdir(workdir) { result = system("#{browser} ./node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js #{runner} dot") } end task :kill do - system "kill #{File.read pid_file}" + system "kill #{File.read pid}" end task :exit do - exit test_result + exit result end end diff --git a/test/templates/package.json b/test/templates/package.json index c933abb6..e6994e9b 100644 --- a/test/templates/package.json +++ b/test/templates/package.json @@ -8,6 +8,6 @@ "devDependencies": { "chai": "^3.0.0", "mocha": "^2.2.5", - "mocha-phantomjs": "^3.5.3" + "mocha-phantomjs-core": "^1.3.1" } } From ce38d74336dccf0343d5b87a1d97787220025bf3 Mon Sep 17 00:00:00 2001 From: Hiroyuki Sano Date: Wed, 20 Jul 2016 05:24:15 +0000 Subject: [PATCH 4/5] Unify template tests into one style --- test/templates/html/spec_runner.html.erb | 36 ---- test/templates/html/test_runner.html.erb | 2 +- test/templates/spec/auto_complete_spec.js | 37 ---- test/templates/spec/dom_helpers_spec.js | 105 ---------- test/templates/spec/repl_console_spec.js | 188 ------------------ test/templates/test/dom_helper_test.js | 24 +++ test/templates/test/repl_console_test.js | 133 +++++++++++-- .../spec_helper.js => test/test_helper.js} | 6 +- 8 files changed, 144 insertions(+), 387 deletions(-) delete mode 100644 test/templates/html/spec_runner.html.erb delete mode 100644 test/templates/spec/auto_complete_spec.js delete mode 100644 test/templates/spec/dom_helpers_spec.js delete mode 100644 test/templates/spec/repl_console_spec.js create mode 100644 test/templates/test/dom_helper_test.js rename test/templates/{spec/spec_helper.js => test/test_helper.js} (90%) diff --git a/test/templates/html/spec_runner.html.erb b/test/templates/html/spec_runner.html.erb deleted file mode 100644 index 436291be..00000000 --- a/test/templates/html/spec_runner.html.erb +++ /dev/null @@ -1,36 +0,0 @@ - - - - - Templates Test - - - - -
- - - - - - - - - - <% 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 index c3d4beb7..f82809a1 100644 --- a/test/templates/html/test_runner.html.erb +++ b/test/templates/html/test_runner.html.erb @@ -19,7 +19,7 @@ - + <% 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 deleted file mode 100644 index fccf259f..00000000 --- a/test/templates/spec/auto_complete_spec.js +++ /dev/null @@ -1,37 +0,0 @@ -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/dom_helpers_spec.js b/test/templates/spec/dom_helpers_spec.js deleted file mode 100644 index 377180f8..00000000 --- a/test/templates/spec/dom_helpers_spec.js +++ /dev/null @@ -1,105 +0,0 @@ -describe("DOM helpers", function() { - describe("hasClass()", function() { - context("create a
", function() { - beforeEach(function() { - this.div = document.createElement("div"); - }); - - context("class=hello", function() { - beforeEach(function() { - this.div.className = "hello"; - }); - it("should have .hello", function() { - assert.ok(hasClass(this.div, "hello")); - }); - it("should not have .world", function() { - assert.notOk(hasClass(this.div, "world")); - }); - }); - - context("class=hello world", function() { - beforeEach(function() { - this.div.className = "hello world"; - }); - it("should have .hello", function() { - assert.ok(hasClass(this.div, "hello")); - }); - it("should have .world", function() { - assert.ok(hasClass(this.div, "world")); - }); - it("should not have .not-world", function() { - assert.notOk(hasClass(this.div, "not-world")); - }); - }); - }); - }); - - describe("addClass()", function() { - context("create a
", function() { - beforeEach(function() { - this.div = document.createElement("div"); - }); - - context("addClass(div, hello)", function() { - beforeEach(function() { - addClass(this.div, "hello"); - }); - it("should have .hello", function() { - assert.ok(hasClass(this.div, "hello")); - }); - }); - }); - - context("create a
", function() { - beforeEach(function() { - this.div = document.createElement("div"); - this.div.className = "hello"; - }); - - context("addClass(div, world)", function() { - beforeEach(function() { - addClass(this.div, "world"); - }); - it("should have .hello", function() { - assert.ok(hasClass(this.div, "hello")); - }); - it("should also have .world", function() { - assert.ok(hasClass(this.div, "world")); - }); - }); - }); - }); - - describe("removeClass()", function() { - context("create a
", function() { - beforeEach(function() { - this.div = document.createElement("div"); - this.div.className = "hello world"; - }); - - context("removeClass(hello)", function() { - beforeEach(function() { - removeClass(this.div, "hello"); - }); - it("should not have .hello", function() { - assert.notOk(hasClass(this.div, "hello")); - }); - it("should have .world", function() { - assert.ok(hasClass(this.div, "world")); - }); - }); - - context("removeClass(world)", function() { - beforeEach(function() { - removeClass(this.div, "world"); - }); - it("should have .hello", function() { - assert.ok(hasClass(this.div, "hello")); - }); - it("should not have .world", function() { - assert.notOk(hasClass(this.div, "world")); - }); - }); - }); - }); -}); diff --git a/test/templates/spec/repl_console_spec.js b/test/templates/spec/repl_console_spec.js deleted file mode 100644 index 860b3e6f..00000000 --- a/test/templates/spec/repl_console_spec.js +++ /dev/null @@ -1,188 +0,0 @@ -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); - self.console.commandHandle('fake-input', function(result, response) { - self.result = result; - self.response = response; - self.message = self.elm.getElementsByClassName('console-message')[0]; - callback(); - }); - } - - beforeEach(function() { - this.elm = document.createElement('div'); - this.elm.innerHTML = '
'; - this.stageElement.appendChild(this.elm); - }); - - context("sessionId=result", function() { - beforeEach(function(done) { - runCommandHandle(this, { mountPoint: '/mock', sessionId: 'result' }, 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("sessionId=error", function() { - beforeEach(function(done) { - runCommandHandle(this, { mountPoint: '/mock', sessionId: 'error' }, 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')); - }); - }); - - context("sessionId=error.txt", function() { - beforeEach(function(done) { - runCommandHandle(this, { mountPoint: '/mock', sessionId: 'error.txt' }, done); - }); - it("should output HTTP status code", function() { - assert.match(this.message.innerHTML, /400 Bad Request/); - }); - }); - }); - - describe(".installInto", function() { - beforeEach(function() { - this.elm = document.createElement('div'); - this.elm.innerHTML = '
'; - this.stageElement.appendChild(this.elm); - }); - - context("install console without options", function() { - beforeEach(function() { - this.console = REPLConsole.installInto('console'); - }); - it("should have attr-prompt-label", function() { - assert.equal(this.console.prompt, 'attr-prompt-label'); - }); - it("should have attr-mount-point", function() { - assert.equal(this.console.mountPoint, 'attr-mount-point'); - }); - it("should have attr-session-id", function() { - assert.equal(this.console.sessionId, 'attr-session-id'); - }); - }); - - context("install console with options", function() { - beforeEach(function() { - var options = { mountPoint: 'opt-mount-point', sessionId: 'opt-session-id' }; - this.console = REPLConsole.installInto('console', options); - }); - it("should have opt-mount-point", function() { - assert.equal(this.console.mountPoint, 'opt-mount-point'); - }); - it("should have opt-session-id", function() { - assert.equal(this.console.sessionId, 'opt-session-id'); - }); - }); - }); - - describe("#install", function() { - beforeEach(function() { - this.elm = document.createElement("div"); - this.stageElement.appendChild(this.elm); - }); - - context("install console", function() { - beforeEach(function() { - var replConsole = new REPLConsole; - replConsole.install(this.elm); - }); - it("should have .console", function() { - assert.ok(hasClass(this.elm, "console")); - }); - it("should have a inner", function() { - assert.equal(this.elm.getElementsByClassName("console-inner").length, 1); - }); - it("should have a resizer", function() { - assert.equal(this.elm.getElementsByClassName("resizer").length, 1); - }); - it("should have a close button", function() { - assert.equal(this.elm.getElementsByClassName("close-button").length, 1); - }); - }); - }); - - describe("console actions", function() { - describe("close action", function() { - beforeEach(function() { - this.elm = document.createElement("div"); - this.elmId = this.elm.id = SpecHelper.randomString(); - this.stageElement.appendChild(this.elm); - var replConsole = new REPLConsole; - replConsole.install(this.elm); - }); - - context("click close button", function() { - beforeEach(function() { - var closeButton = this.elm.getElementsByClassName("close-button")[0]; - SpecHelper.triggerEvent(closeButton, "click"); - }); - it("should be removed from the parent node", function() { - assert.isNull(document.getElementById(this.elmId)); - }); - }); - }); - }); -}); diff --git a/test/templates/test/dom_helper_test.js b/test/templates/test/dom_helper_test.js new file mode 100644 index 00000000..e4156a5e --- /dev/null +++ b/test/templates/test/dom_helper_test.js @@ -0,0 +1,24 @@ +suite('DOM Helpers', function() { + test('hasClass() checks if an element contains a class name', function() { + var div = document.createElement('div'); + assert.notOk(hasClass(div, 'hello')); + div.className = 'hello'; + assert.ok(hasClass(div, 'hello')); + div.className = 'hello world'; + assert.ok(hasClass(div, 'hello')); + }); + + test('addClass() adds a class name to an element', function() { + var div = document.createElement('div'); + assert.notOk(hasClass(div, 'hello')); + addClass(div, 'hello'); + assert.ok(hasClass(div, 'hello')); + }); + + test('removeClass() removes a class name from an element', function() { + var div = document.createElement('div'); + div.className = 'hello world'; + removeClass(div, 'hello'); + assert.notOk(hasClass(div, 'hello')); + }); +}); diff --git a/test/templates/test/repl_console_test.js b/test/templates/test/repl_console_test.js index 015dcbf9..31b65d28 100644 --- a/test/templates/test/repl_console_test.js +++ b/test/templates/test/repl_console_test.js @@ -14,29 +14,126 @@ suite('REPLCosnole', function() { teardown(function() { removeAllChildren(this.stage); }); - test('swap last word', function() { - this.console.setInput('hello world'); - this.console.swapCurrentWord('word'); + suite('REPLConsole.installInto()', function() { + setup(function() { + var elm = document.createElement('div'); + elm.innerHTML = '
'; + this.stage.appendChild(elm); + }); - assert.equal(this.console._input, 'hello word'); + test('console can be installed with dataset', function() { + var console = REPLConsole.installInto('my-console'); + assert.equal('my-prompt-label', console.prompt); + assert.equal('my-mount-point', console.mountPoint); + assert.equal('my-session-id', console.sessionId); + }); + + test('console can be installed with argument options', function() { + var options = { mountPoint: 'other-mount-point', sessionId: 'other-session-id' }; + var console = REPLConsole.installInto('my-console', options); + assert.equal('other-mount-point', console.mountPoint); + assert.equal('other-session-id', console.sessionId); + assert.equal('my-prompt-label', console.prompt); + }); + }); + + suite('Installing', function() { + test('#install renders console into an element', function() { + var div = document.createElement('div'); + var console = new REPLConsole; + console.install(div); + assert.equal(1, div.getElementsByClassName('console-inner').length); + assert.equal(1, div.getElementsByClassName('resizer').length); + assert.equal(1, div.getElementsByClassName('close-button').length); + }); }); - test('swap first word', function() { - this.console.setInput('hello world'); - this.console._caretPos = 0; - this.console.swapCurrentWord('my'); + suite('Actions', function() { + test('close button removes console on clicked', function() { + var div = document.createElement('div'); + div.id = 'my-console'; + this.stage.appendChild(div); + REPLConsole.installInto('my-console'); + assert.isNotNull(document.getElementById('my-console')); + + var btn = div.getElementsByClassName('close-button')[0]; + TestHelper.triggerEvent(btn, 'click'); + + assert.isNull(document.getElementById('my-console')); + }); + }); + + suite('Command Handling', function() { + test('#commandHandle renders output to console', function(done) { + var inner = this.console.inner; + this.console.commandHandle('fake-input', function(result, response) { + var msg = inner.getElementsByClassName('console-message')[0]; + + assert.ok(result); + assert.match(msg.innerText, /"fake-result"/); + assert.notOk(hasClass(msg, 'error-message')); + + done(); + }); + }); - assert.equal(this.console._input, 'my world'); + test('#commandHandle shows error message if something is wrong', function(done) { + var inner = this.console.inner; + this.console.sessionId = 'error'; + this.console.commandHandle('fake-input', function(result, response) { + var msg = inner.getElementsByClassName('console-message')[0]; + + assert.notOk(result); + assert.match(msg.innerText, /fake-error-message/); + assert.ok(hasClass(msg, 'error-message')); + + done(); + }); + }); + + test('#commandHandle shows HTTP status code if something is wrong', function(done) { + var inner = this.console.inner; + this.console.sessionId = 'error.txt'; + this.console.commandHandle('fake-input', function(result, response) { + var msg = inner.getElementsByClassName('console-message')[0]; + + assert.notOk(result); + assert.match(msg.innerText, /400 Bad Request/); + + done(); + }); + }); }); - test('current word', function() { - this.console.setInput('hello world'); - assert.equal(this.console.getCurrentWord(), 'world'); - this.console._caretPos = 0; - assert.equal(this.console.getCurrentWord(), 'hello'); - this.console._caretPos = 5; - assert.equal(this.console.getCurrentWord(), ''); - this.console._caretPos = 6; - assert.equal(this.console.getCurrentWord(), 'world'); + suite('Auto Completion', function() { + test('swap last word', function() { + this.console.setInput('hello world'); + this.console.swapCurrentWord('word'); + + assert.equal(this.console._input, 'hello word'); + }); + + test('swap first word', function() { + this.console.setInput('hello world'); + this.console._caretPos = 0; + this.console.swapCurrentWord('my'); + + assert.equal(this.console._input, 'my world'); + }); + + test('current word', function() { + this.console.setInput('hello world'); + assert.equal(this.console.getCurrentWord(), 'world'); + this.console._caretPos = 0; + assert.equal(this.console.getCurrentWord(), 'hello'); + this.console._caretPos = 5; + assert.equal(this.console.getCurrentWord(), ''); + this.console._caretPos = 6; + assert.equal(this.console.getCurrentWord(), 'world'); + }); }); }); diff --git a/test/templates/spec/spec_helper.js b/test/templates/test/test_helper.js similarity index 90% rename from test/templates/spec/spec_helper.js rename to test/templates/test/test_helper.js index 58451ed5..c0e32c35 100644 --- a/test/templates/spec/spec_helper.js +++ b/test/templates/test/test_helper.js @@ -1,5 +1,7 @@ (function() { - var SpecHelper = { + 'use strict'; + + var TestHelper = { triggerEvent: function(el, eventName) { var event = document.createEvent("MouseEvents"); event.initEvent(eventName, true, true); // type, bubbles, cancelable @@ -20,6 +22,6 @@ } }; - window.SpecHelper = SpecHelper; + window.TestHelper = TestHelper; window.assert = chai.assert; })(); From 8fb1ee41f1997d87da32f6172d4093a1fe92cf4f Mon Sep 17 00:00:00 2001 From: Hiroyuki Sano Date: Wed, 20 Jul 2016 07:35:44 +0000 Subject: [PATCH 5/5] Test key events for auto-completion --- test/templates/config.ru | 2 +- test/templates/test/repl_console_test.js | 17 +++++++++++++++++ test/templates/test/test_helper.js | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/test/templates/config.ru b/test/templates/config.ru index e8bd6cb8..3a0589c4 100644 --- a/test/templates/config.ru +++ b/test/templates/config.ru @@ -43,7 +43,7 @@ end map "/mock/repl_sessions/result" do headers = { 'Content-Type' => 'application/json' } - body = [ { output: '=> "fake-result"\n' }.to_json ] + body = [ { output: '=> "fake-result"\n', context: [ :something, :somewhat, :somewhere ] }.to_json ] run lambda { |env| [ 200, headers, body ] } end diff --git a/test/templates/test/repl_console_test.js b/test/templates/test/repl_console_test.js index 31b65d28..eb59ff14 100644 --- a/test/templates/test/repl_console_test.js +++ b/test/templates/test/repl_console_test.js @@ -135,5 +135,22 @@ suite('REPLCosnole', function() { this.console._caretPos = 6; assert.equal(this.console.getCurrentWord(), 'world'); }); + + test('console can start auto completion by typing tab key', function(done) { + this.console.setInput('some'); + assert.notOk(this.console.autocomplete); + this.console.onKeyDown(TestHelper.keyDown(9)); // tab + assert.ok(this.console.autocomplete); + + var self = this; + setTimeout(function() { + self.console.autocomplete.onFinished(function(word) { + assert.equal('something', word); + done(); + }); + self.console.onKeyDown(TestHelper.keyDown(9)); // tab + self.console.onKeyDown(TestHelper.keyDown(13)); // enter + }, 100); + }); }); }); diff --git a/test/templates/test/test_helper.js b/test/templates/test/test_helper.js index c0e32c35..44a2c5b9 100644 --- a/test/templates/test/test_helper.js +++ b/test/templates/test/test_helper.js @@ -1,12 +1,21 @@ (function() { 'use strict'; + function FakeKeyEvent(key) { + this.keyCode = key; + this.preventDefault = function() {}; + this.stopPropagation = function() {}; + } + var TestHelper = { triggerEvent: function(el, eventName) { var event = document.createEvent("MouseEvents"); event.initEvent(eventName, true, true); // type, bubbles, cancelable el.dispatchEvent(event); }, + keyDown: function(keyCode) { + return new FakeKeyEvent(keyCode); + }, randomString: function() { Math.random().toString(36).substring(2); },