Skip to content

Commit

Permalink
Merge pull request #121 from NicolasPetton/chrome-tests
Browse files Browse the repository at this point in the history
[WIP] Add tests for indium-chrome.el
  • Loading branch information
NicolasPetton committed Oct 18, 2017
2 parents 0119deb + 524d45b commit db3b727
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
11 changes: 6 additions & 5 deletions indium-chrome.el
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ Try a maximum of NUM-TRIES."
(message "Trying to connect to the Chrome instance...")
(sleep-for 1)
(indium-chrome--get-tabs-data host
indium-chrome-port
(lambda (tabs)
(if tabs
(indium-chrome--connect-to-tab tabs)
(indium-chrome--try-connect host (1- num-tries))))))
indium-chrome-port
(lambda (tabs)
(if tabs
(indium-chrome--connect-to-tab tabs)
(when (> num-tries 0)
(indium-chrome--try-connect host (1- num-tries)))))))

(defun indium-connect-to-chrome ()
"Open a connection to a v8 tab."
Expand Down
88 changes: 88 additions & 0 deletions test/unit/indium-chrome-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,94 @@
(indium-chrome--connect-to-tab-with-url "file:///home/foo" '())
(expect #'indium-workspace-read :not :to-have-been-called)))

(describe "Reading tab data"
(it "should be able to parse tab data"
(let ((data "HTTP/1.1 200 OK
{\"foo\": 1, \"bar\": 2}"))
(with-temp-buffer
(insert data)
(goto-char (point-min))
(expect (indium-chrome--read-tab-data)
:to-equal '((foo . 1)
(bar . 2))))))

(it "should return nil when there is no data"
(with-temp-buffer
(goto-char (point-min))
(expect (indium-chrome--read-tab-data)
:to-equal nil)))

(it "should return nil when not getting a 200 response"
(let ((data "HTTP/1.1 404 NOT-FOUND
{\"foo\": 1, \"bar\": 2}"))
(with-temp-buffer
(insert data)
(goto-char (point-min))
(expect (indium-chrome--read-tab-data)
:to-equal nil)))))

(describe "Connecting to a tab"
(it "should signal an error when the list of tabs is empty"
(expect (indium-chrome--connect-to-tab nil)
:to-throw))

(it "should automatically connect to the first tab if there is only one"
(spy-on 'indium-chrome--connect-to-tab-with-url)
(let ((tabs '(((url . foo)))))
(indium-chrome--connect-to-tab tabs)
(expect #'indium-chrome--connect-to-tab-with-url
:to-have-been-called-with 'foo tabs))))

(describe "Running Chrome"
(it "Should start the Chrome process"
(spy-on 'indium-chrome--find-executable :and-return-value "chrome")
(spy-on 'make-process)
(spy-on 'indium-chrome--try-connect)
(let ((indium-chrome-port 9999))
(indium-run-chrome "foo.html"))
(expect #'make-process
:to-have-been-called-with
:name "indium-chrome-process"
:command '("chrome" "--remote-debugging-port=9999" "foo.html")))

(it "Should try to open connections"
(spy-on 'indium-chrome--find-executable :and-return-value "chrome")
(spy-on 'make-process)
(spy-on 'indium-chrome--try-connect)
(let ((indium-chrome-port 9999))
(indium-run-chrome "foo.html"))
(expect #'indium-chrome--try-connect :to-have-been-called-with "127.0.0.1" 10)))

(describe "Connecting to a Chrome process"
(it "Should wait between connection retries"
(spy-on 'sleep-for)
(spy-on 'indium-chrome--get-tabs-data)
(indium-chrome--try-connect "foo" 1)
(expect #'sleep-for :to-have-been-called-with 1))

(it "Should retry if the connection attempt fails"
(spy-on 'sleep-for)
(spy-on 'indium-chrome--try-connect :and-call-through)
(spy-on 'indium-chrome--get-tabs-data
:and-call-fake
(lambda (host port callback)
(funcall callback nil)))
(indium-chrome--try-connect "foo" 1)
(expect #'indium-chrome--try-connect :to-have-been-called-times 2)
(expect #'indium-chrome--try-connect :to-have-been-called-with "foo" 0))

(it "Should connect to a tab when found"
(spy-on 'sleep-for)
(spy-on 'indium-chrome--connect-to-tab)
(spy-on 'indium-chrome--get-tabs-data
:and-call-fake
(lambda (host port callback)
(funcall callback 'tabs)))
(indium-chrome--try-connect "foo" 1)
(expect #'indium-chrome--connect-to-tab :to-have-been-called-with 'tabs)))

(describe "Regression test for GH issue #97"
(it "should not create multiple REPL buffers"
(let ((buf (indium-repl-get-buffer-create)))
Expand Down

0 comments on commit db3b727

Please sign in to comment.