HeinrichApfelmus / threepenny-gui

GUI framework that uses the web browser as a display.
https://heinrichapfelmus.github.io/threepenny-gui/
Other
437 stars 77 forks source link

Is it possible to write unit testing code for functions that use Threepenny? #142

Closed Wizek closed 7 years ago

Wizek commented 7 years ago

I was thinking that maybe I can run an arbitrary UI action in IO and assert on its results, e.g.

runUI mockWindow (UI.div # set UI.text "result") >>= (`shouldSatisfy` (("result" `isInfixOf`) . show))

But it seems Window is defined within a hidden module. Would it make sense to expose it perhaps, making it possible to construct transient mock instances? Or is there an alternative, preferred way to test code building on top of UI?

HeinrichApfelmus commented 7 years ago

I don't think it's possible to test code from UI — besides running it and connecting a browser, of course (There are test frameworks for the latter, I think). One reason is that you can run arbitrary JavaScript code from within UI, so any mock test would have to be able to interpret arbitrary JavaScript code. I'm afraid that costs much more than a threepenny. :smile:

If the code you want to test does not depend on UI a lot, then you can try to lift from a lower monad, e.g. IO or a pure computation, and test the latter instead, e.g.

myCode = liftIO myOtherCode    -- --> test myOtherCode instead

Other than that, I'm afraid you're out of luck.

Wizek commented 7 years ago

I see, that's sad to hear.

Thanks for the quick response though.

I still have a bit of hope that maybe if I stack unpack threepanny-gui locally and modify Graphics.UI.Threepenny.Internal to be exported, maybe I can construct a Window that is sufficient for some limited use cases -- e.g. constructing Elements, maybe those don't require roundtrip connections with a browser.

HeinrichApfelmus commented 7 years ago

Closing. Feel free to reopen if more questions remain!