How to use dommy for testing against a CodeMirror instance?

88 views
Skip to first unread message

franko

unread,
Feb 11, 2017, 2:44:56 PM2/11/17
to ClojureScript
Hi everyone,

I am trying to write some ClojureScript to use with CodeMirror, like a new keymap. For testing, I have PhantomJS installed. I'm trying to programmatically create a test page with a CodeMirror instance on it.

It would be good if all of my test cases can re-use the same CodeMirror instance.

So my thought is at the beginning of my test cljs file, I'll create the page

(dommy/append! js/document.body ...

Is this the right starting point? I'd like to get a textarea with some known id, then do the equivalent of calling CodeMirror.fromTextArea with it plus a property map that assigns the keyMap to mine. So the equivalent of javascript:

var textArea = document.getElementById("knownId");
var editor = CodeMirror.fromTextArea(textArea,
{
keyMap: "myMap"
});
textArea.myCodeMirrorInstance = editor;

What would the above look like in ClojureScript with dommy?

And I'll save the CM instance ...

(def cm-instance (-> "#knownId" sel1 .-myCodeMirrorInstance))

And use `cm-instance` in my tests.

I had an earlier setup (with lein) that seemed to work, but then I realized the unit tests were grabbing the actual index.html for my project. Which meant I could just have the plain javascript above as is in index.html, plus the cljs (-> "#knownId" sel1 .-myCodeMirrorInstance) and I was all set.

I think it's better that the unit tests use some other virtual page it creates. Now I'm trying to use boot and I'm not even sure how to get it working either way (either using index.html or having a separate dommy-created page headlessly).

Any help would be appreciated!

franko

unread,
Feb 12, 2017, 1:36:54 AM2/12/17
to ClojureScript
Ah. I probably don't need a text editor. I can create a CodeMirror instance by calling the constructor. But how do I do that from ClojureScript?

Javascript: CodeMirror(yourFnThatAddsTheInstanceToThePage);

ClojureScript: (js/CodeMirror. fn-that-resets-my-atom)

I get a "ReferenceError, can't find variable: CodeMirror."

If I try (CodeMirror/Codemirror. my-fn) it says it cannot find CodeMirror.cljs or cljc or namespace...

Is there some place where I get it to know about a CodeMirror namespace like it knows about js/ ?

franko

unread,
Feb 12, 2017, 2:01:33 AM2/12/17
to ClojureScript
Seems like I ought to be able to extern CodeMirror so it's available... Something like putting this ...

:externs ["resources/cm/lib/codemirror.js"]

... somewhere. But where? The boot task I want to affect is auto-test ...

(deftask auto-test []
(comp (testing)
(watch)
(test-cljs :js-env :phantom)))

Reply all
Reply to author
Forward
0 new messages