This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-01-19
Channels
- # announcements (1)
- # architecture (24)
- # beginners (7)
- # boot (7)
- # cider (1)
- # cljdoc (4)
- # clojure (20)
- # clojure-austin (1)
- # clojure-brasil (2)
- # clojure-dev (1)
- # clojure-italy (1)
- # clojure-spec (36)
- # clojure-uk (11)
- # clojurescript (44)
- # data-science (3)
- # datascript (1)
- # datomic (8)
- # figwheel-main (2)
- # fulcro (29)
- # graphql (9)
- # kaocha (2)
- # keyboards (1)
- # leiningen (3)
- # lumo (5)
- # nrepl (4)
- # off-topic (39)
- # onyx (5)
- # re-frame (2)
- # shadow-cljs (42)
- # tools-deps (18)
- # yada (65)
What is your approach for mocking the inner call of http/get in function some-example assuming that http/get should return a channel with some string?
I did try something like this:
(t/deftest get-products-test
(t/testing "should output mocked status"
(t/async done
(with-redefs [http/get (fn [_ __] (async/go {:status "great"}))]
(async/go
(t/is (= (async/<! (fablo/get-products {})) "okkkk"))
(done))))))
But it seems that the go block does not play nicely with with-redefs
. Could you please recommend me some other approach?in the past i've used defprotocol to override clj-http / cljs-http functions. it's been a while, but maybe i can dig up some code.
To override you mean that the first variable in your functions was a http which implements a Http protocol. Is that so?
I think I will do the same
(defn get-products
[^Http http
{:keys [results search-string]
:or {results 5
search-string "water"}}]
yeah, something like this (but not entirely)
(defprotocol Http
(GET [this url params])
(POST [this url params])
(PUT [this url params])
(DELETE [this url params]))
(defn do-http [http method url params]
(case method
:get (GET http url params)
:post (POST http url params)
:delete (DELETE http url params)
:put (PUT http url params)
(GET http url params)))
Looks perfect!
Thank you!
@U0GC1C09L Btw can I implement only a part of a protocol in Clojure/script? Or do I have to specify implementation for all of methods in Protocol?
i guess neither? i remember only implementing functions that i wanted to override depending on the context (dev / test).
(deftype ActualHttp []
Http
(GET [this url params]
(http/get url params))
(POST [this url params]
(http/post url params))
(DELETE [this url params]
:delete-not-implemented)
(PUT [this url params]
:put-not-implemented))
@U0GC1C09L I tried to reify Http Protocol in test namespace but I’m unable to do it. 😮 I received an error about calling the function of undefined. But when I try to reify in the same namespace then everythin works like a charm
(defprotocol Http
"Abstraction over http requests"
(get [this url opts] "GET http request"))
(get
(reify Http
(get [this url opts] (println "Hello")))
"Hello" {})
Well I don’t know which one is used by figwheel main. I quess that it’s done by JVM
but the results are indeed in the browser
that's beyond my knowledge. 🙂 last time i checked though, clj-http was not browser compatible, and cljs-http was not JVM compatible. that's probably just a tangent but you never know.
in a previous project i differentiated between clj and cljs: https://github.com/intermine/imcljs/blob/dev/src/clj/imcljs/internal/io.clj#L2 https://github.com/intermine/imcljs/blob/dev/src/cljs/imcljs/internal/io.cljs#L2
Hmm. Now I’m talking about regular protocols in Clojurescript. I defined a simple protocol in one namespace
(defprotocol Http
"Abstraction over http requests"
(get [this url opts] "GET http request"))
Then I just defined a implementation of a protocol in test namespace like so:
(deftype http []
Http
(get [this url opts]
(println "Hello")))
(get (http.) "" {})
What is very bizzare is that I’m receiving an error:
#object[TypeError TypeError: (intermediate value).get is not a function]
(<NO_SOURCE_FILE>)
Completely don’t know why it’s happeninghow can i keep the integrity of a CLJS function name when using :advanced
compilation?
(defn ^:export myfunction []
"abcdefg")
output:
function P(){return"abcdefg"}
is this a job for externs? but they're for third party libraries, right?@U0GC1C09L it should work for you https://stackoverflow.com/questions/31641030/call-clojurescript-from-javascript
Btw how do you try to require your function?
Happy that I could help
FWIW, ^:export
doesn't really preserve the function name. All it does is make a non-minified name that points to the original function.
I completely don’t get it. Why calling a method of instance of deftype in namespace where defprotocol is defined works like a charm while in different namespace calling a method from a instance of deftype throws an error 😮?
how are you referring to the function?
protocol functions are functions in the namespace where they are defined, so you need to require the protocol namespace and either refer or qualify the function from the protocol namespace to invoke
Let me try
:man-facepalming:
Sorry for the mess
Thank you @alexmiller