Unit Test - Mocking Dependencies

134 views
Skip to first unread message

santiag...@gmail.com

unread,
Sep 28, 2014, 11:25:29 PM9/28/14
to polym...@googlegroups.com
Hi everyone, Im just starting on polymer. Im trying to unit test a custom element that has dependencies and I would like to fake/mock these out.
I've found Scott Miles recommendation on how to mock the core-ajax implementation. I thought I could follow that pattern easily but this only works as long as my element does not import the about to be mocked (core-ajax in this case) element. 
If it does import it, then when the test tries to run I get 

'Uncaught NotSupportedError: Failed to execute 'registerElement' on 'Document': Registration failed for type 'core-ajax'. A type with that name is already registered.' 

If I could do something like document.unregister the core-ajax element and import it again in my test, Id be a much happier dev!!! 
Polymer is awesome but if I can not unit test it, then it presents major risks (at least when building an app that will need to be maintained/changed)

How are you guys working around this? I've been digging into Polymer and PolymerLab elements repo and most of them lack tests. So far I;ve not found much reference on how to do it.

Thanks for the help!

Santiago

Scotts' recommendation was:

Instead of importing core-ajax/core-ajax.html, create your own core-ajax element.

<polymer-element name="core-ajax" attributes="response">
<script>
  Polymer('core-ajax', {
    attached: function() {
      this.response = ['a', 'b', 'c'];
    }
  });
</script>
</polymer-element>

Obviously, this is just an example, the actual implementation depends on the desired mocking behavior.

This is just one way to solve it, there are many others. I'm interested to hear what you find (in)convenient.

Rob Dodson

unread,
Sep 29, 2014, 3:21:49 PM9/29/14
to santiag...@gmail.com, polymer-dev
If you're just trying to mock the http response, then a library like Sinon might be a better approach.
There is another option, which is to override core-ajax's prototype and change what happens inside of urlChanged. Take a look at this discussion on twitter and in particular the jsbin at the end.

Follow Polymer on Google+: plus.google.com/107187849809354688692
---
You received this message because you are subscribed to the Google Groups "Polymer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to polymer-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/polymer-dev/650f51fd-290f-458a-abb0-2a5af04e3916%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages