Matcher or methodology for FormData?

299 views
Skip to first unread message

Travis Briggs

unread,
Apr 3, 2015, 7:13:34 PM4/3/15
to jasmi...@googlegroups.com
Hello,

I'm writing Jasmine tests for a bunch of methods that upload data using XHRs directly. Many of the methods use FormData to pass the data to the server. I'm trying to use matchers with these FormData objects without luck.

I am using mock-ajax.js. I have set up a custom "parser" that simply returns the FormData directly:

  beforeEach(function() {
    jasmine.Ajax.install();
    jasmine.Ajax.addCustomParamParser({
      // If the params are a FormData, simply echo them back.
      test: function(xhr) {
        return xhr.params instanceof FormData;
      },
      parse: function(formData) {
        return formData;
      }
    });
  });

Now in my spec I am getting the FormData objects alright, but they always seem toEqual each other no matter if they actually contain the same fields. See this codepen:


I'm not sure there's a way to write a custom equality matcher for this one, because the FormData interface features few methods for returning the data inside (https://developer.mozilla.org/en-US/docs/Web/API/FormData). Furthermore, in Chrome it seems the FormData object only has an "append" method and doesn't even have a "get" method (which would either way require you to know all of the keys in the object).

Any ideas or suggestions would be appreciated.

Thanks,
-Travis

Gregg Van Hove

unread,
Apr 6, 2015, 4:06:54 PM4/6/15
to jasmi...@googlegroups.com
When I've had to test with `FormData` objects before I've basically had to replace the constructor with a spy and return something that behaves like a real `FormData` but allows for the kinds of tests you want to write.

-Gregg

--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jasmine-js+...@googlegroups.com.
To post to this group, send email to jasmi...@googlegroups.com.
Visit this group at http://groups.google.com/group/jasmine-js.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages