0

I'm making a POST with AJAX and trying to test the data that gets sent, but I'm getting the error "paramString.split is not a function" when that test runs. I've looked for other posts about this but they all seem to be about getting FormData to work with AJAX, which I'm not having trouble with. The data sends, but I can't write a successful test around it.

AJAX:

upload: (file, progressCallback) => {
  let data = new FormData();
  data.append('image', file);

  return $.ajax({
    xhr: function() {
      let xhr = new window.XMLHttpRequest();

      xhr.upload.addEventListener('progress', progressCallback);
      return xhr;
    },
    method: 'POST',
    url: apiUrl(),
    cache: false,
    processData: false,
    contentType: false,
    data: data
  });
}

Test:

describe('my test', () => {

  beforeEach(function() {
    jasmine.Ajax.install()
  });

  afterEach(function() {
    jasmine.Ajax.uninstall();
  });

  it('sends a POST request to the right endpoint with data', function() {
    const image = { 
        size: 10000,
        type: 'image/jpeg'
    };

    let data = new FormData();
      data.append('image', image);

    myService.upload(image); // POST happens here

    const request = jasmine.Ajax.requests.mostRecent();

    expect(request.method).toBe('POST');              // passes
    expect(request.url).toBe('/dashapi/dam-assets/'); // passes
    expect(request.data()).toEqual(data);             // error
  });

Error

TypeError: paramString.split is not a function

The error is occurring at this line: https://github.com/jasmine/jasmine-ajax/blob/master/src/paramParser.js#L18. I put a breakpoint there and paramString is actually a FormData object at that point. I'm assuming that mocking out the request with jasmine.Ajax.install is overwriting the processData: false I have in the original request, but I'm not sure how to fix that.

js225
  • 3
  • 4

1 Answers1

0

I had to change the test to be

expect(request.params).toEqual(data);
js225
  • 3
  • 4