0

I have a test I'm trying to write for a request that uses FormData to submit a multipart form. I'm using Nock to try to mock the request

    const submit_claim_request = nock(gatewayClient.defaults.baseURL)
      .post('/api/v2/claims', (body) => {
        return (
          JSON.stringify(body) ===               //<-------- this does not work
          {
            default_employee_wallet_id: '0000000046',
            amount: 1,
            category: 'Parking',
            reimbursement_vendor: 'Test vendor',
            transaction_date: '2021-10-03T00:00:00.000Z',
            note: 'test description',
            is_multi_month: false,
            'file[]': file,
          }
        );
      })
      .reply(200, {
        data: {
          reimbursement: {
            amount: '1',
            category: 'Parking',
          },
        },
      });

However, I can't figure out how to convert my FormData from my Axios request to a JS object.

This is what my body looks like:

----------------------------792417295264678490134208
Content-Disposition: form-data; name="default_employee_wallet_id"

5cc7f9432e041785dea91a7a
----------------------------792417295264678490134208
Content-Disposition: form-data; name="amount"

1
----------------------------792417295264678490134208
Content-Disposition: form-data; name="reimbursement_vendor"

Test vendor
----------------------------792417295264678490134208
Content-Disposition: form-data; name="category"

gym_and_fitness
----------------------------792417295264678490134208
Content-Disposition: form-data; name="transaction_date"

2021-10-03T00:00:00.000Z
----------------------------792417295264678490134208
Content-Disposition: form-data; name="is_multi_month"

false
----------------------------792417295264678490134208
Content-Disposition: form-data; name="note"

test description
----------------------------792417295264678490134208
Content-Disposition: form-data; name="file[]"; filename="chucknorris1.png"
Content-Type: image/png

(⌐□_□)
----------------------------792417295264678490134208--
bigpotato
  • 24,574
  • 50
  • 160
  • 313

1 Answers1

-1

it seems you are checking that JSON.stringify(body) is equal to an object.

Try to console.log(JSON.stringify(body)) and notice that it would not be equal to the object you are checking it against.

The response received from the axios should already be in JSON format if it is returned as a JSON object.

What you would do is now to check for object equality between body and the object you expect it to be equal to.

Lodash's _.isEqual method can be good for this.

  const submit_claim_request = nock(gatewayClient.defaults.baseURL)
      .post('/api/v2/claims', (body) => {
        return (
          _.isEqual(body, {
            default_employee_wallet_id: '0000000046',
            amount: 1,
            category: 'Parking',
            reimbursement_vendor: 'Test vendor',
            transaction_date: '2021-10-03T00:00:00.000Z',
            note: 'test description',
            is_multi_month: false,
            'file[]': file,
          })    
        );
      })
      .reply(200, {
        data: {
          reimbursement: {
            amount: '1',
            category: 'Parking',
          },
        },
      });

==================

Here is an example of my request:

      const form_data = new FormData();
      form_data.append('default_employee_wallet_id', "foobar");
      form_data.append('amount', 12.34);
      ...
      
      receipts.forEach((file) => {
        form_data.append('file[]', file);
      });

      const axios_instance = axios.create()
      axios_instance.post('/api/v2/claims', form_data);



bigpotato
  • 24,574
  • 50
  • 160
  • 313
rexessilfie
  • 549
  • 2
  • 5