user1999541
user1999541

Reputation: 29

Javascript - mock FormData.entries() in unit test

I am trying to unit test submitting of form in javascript. This is how I mock the FormData:

function FormDataMock() {
  this.append = jest.fn();
}
global.FormData = FormDataMock

But I have a problem to mock later in the function I'm testing I'm iterating over entries in form by FormData.entries(), the problem is that I don't know how to mock .entries() function. I have tried to assign the function to entries but it did not help me. I'm still getting entries is not a function. Do you have any idea, how to mock .entries() function in FormData? Thanks in advance.

Upvotes: 1

Views: 3477

Answers (1)

Andreas Köberle
Andreas Köberle

Reputation: 110972

What about

const entries = jest.fn()
const append = jest.fn()
global.FormData = () => ({ entries, append })

Upvotes: 1

Related Questions