jeudi 11 avril 2019

Return a double in Jest JS

I have a simple function like this that I want to test:

const saveAs = (fileName, content, contentType) => {
 const a = createDownloadLink(fileName, content, contentType)
 a.click()
}

export const createDownloadLink = (fileName, content, contentType) => {
 ...
 const a = document.createElement('a')
 ...
 return a
}

export default saveAs

I want to test that when I call saveAs, createDownloadLink is called, and click is called on the result.

I have tried mocking createDownloadLink and creating a spy on it. However, I can't find how to test click on the result:

Jest JS test:

  const createDownloadLink = jest.fn()
  saveAs('file.html', '<h1>hello</h1>', 'test/plain;UTF-8')
  expect(createDownloadLink).toBeCalled() // And return a double
  // expect(double.click).toBeCalled
})

Aucun commentaire:

Enregistrer un commentaire