1

This is a duplicate question asked 2 years before but still i am taking this out from Pandora Box for no answer:

How to mock Blob and Click() in jasmine Specs in angular js?

Similar kind of a function exists here:

function saveFile(data, fileName) {
  let url = URL.createObjectURL(new Blob([data]));
  let a = document.createElement('a');
  a.href = url;
  a.download = fileName;
  a.target = '_blank';
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
}

My attempt of unit test code is here below:

describe('UtilityService', function () {
    let UtilityService;
    let URL;
    let Blob;

    beforeEach(inject(function (_UtilityService_) {
        UtilityService = _UtilityService_;
        Blob = function (dataArray) {
            return dataArray;
        };
        URL = {
            createObjectURL: function (blobData) {
                return Blob(blobData);
            }
        };
        spyOn(URL.createObjectURL).and.returnValue([201]);
        spyOn(document, 'createElement').and.callFake(function () {
            return {
                href: '',
                target: '_blank',
                download: ''
            };
        });
        spyOn(document, 'body').and.returnValue('');
    }));

Caller function:

fdescribe('saveFile', function () {
  it('should save the file', function () {
    UtilityService.saveFile(null, null);
  });
});

No luck, ChromeHeadless is crashing. What is the actual cause to this and how to get rid off this? Do we need change anything in karma configuration file?

Mithun Shreevatsa
  • 3,588
  • 9
  • 50
  • 95

0 Answers0