'How to write unit test case using jest for local file download?

  const saveContainerData = useCallback(() => {
    if (binder.current) {
      const root = binder.current.getPropertyTree()!.root as any;
      const element = document.createElement('a');
      const file = new Blob([JSON.stringify(root._toJson(), undefined, 2)], {
        type: 'text/plain'
      });

      element.href = window.URL.createObjectURL(file);
      console.log('href', element.href)
      element.download = 'sdc_' + new Date().getTime() + '.json';
      document.body.appendChild(element); // Required for this to work in FireFox
      element.click();
      element.remove();
    }
  }, []);

I have to write Jest unit test case for this code.

TypeError: window.URL.createObjectURL is not a function

I am getting the above error, can anyome help me on this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source