Skip to content Skip to sidebar Skip to footer

Typeerror: Failed To Execute 'createobjecturl' On 'url': No Function Was Found That Matched The Signature Provided

I have a angular 8 application and I do some unit testing with jasmine karma. So this is the component.ts: export class DossierPersonalDataComponent implements OnInit { dossier:

Solution 1:

It seems that the karma chrome is not supporting the deprecated createObjectURL. Here is how you'll have to do to make it work with karma:

export classDossierPersonalDataComponentimplementsOnInit{
  dossier: DossierDto;
  editDossierForm: FormGroup;
  formBuilder = new FormBuilder();
  globalEditDossierErrors: ValidationErrors;
  dossierItems: DossierItemDto[] = [];
  profileImagefile: File;
  profileImageNeedsUpload = false;
  compWindow: any;

  constructor(
    private dossierService: DossierService,
    private route: ActivatedRoute,
    private sanitizer: DomSanitizer,
    private dossierFileService: DossierFileService,
    private errorProcessor: ErrorProcessor,
    private dialog: MatDialog
  ) {
    // this would help you to mock the "window" object in spec filethis.compWindow = window;
    // please move below code to ngOnInit as standard practice of Angular.this.dossierItems = this.route.snapshot.data.dossierItems;
    this.editDossierForm = this.formBuilder.group({});
    this.editDossierForm.disable();

    this.dossier = this.route.snapshot.data.dossier;
    this.dossierItems = route.snapshot.data.dossierItems;
    this.profileImagefile = this.route.snapshot.data.profileImage;

    this.editDossierForm = this.formBuilder.group({
      firstName: this.formBuilder.control(this.dossier.firstName, [Validators.required, Validators.maxLength(255)]),
      lastName: this.formBuilder.control(this.dossier.lastName, [Validators.required, Validators.maxLength(255)]),
      mobile: this.formBuilder.control(this.dossier.mobile, [Validators.maxLength(255)]),
      company: this.formBuilder.control(this.dossier.company, [Validators.maxLength(255)]),
      buddy: this.formBuilder.control(this.dossier.buddy, [Validators.maxLength(255)]),
      supervisor: this.formBuilder.control(this.dossier.supervisor, [Validators.maxLength(255)]),
      dateOfBirth: this.formBuilder.control(this.dossier.dateOfBirth)
    });
  }
  ngOnInit(): void {
    this.editDossierForm.disable();
  }

  editName() {
    this.editDossierForm.enable();
  }

  get profileImageUrl() {
    returnthis.profileImagefile === null
      ? '/assets/placeholder.jpg'
      : this.sanitizer.bypassSecurityTrustUrl(this.compWindow.URL.createObjectURL(this.profileImagefile));
  }
}

In the spec.ts file:

describe('DossierPersonalDataComponent', () => {
  letcomponent: DossierPersonalDataComponent;
  letfixture: ComponentFixture<DossierPersonalDataComponent>;
  const myWindow = {
    URL : {
      createObjectURL() { return'something'; }
    }
  };

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, DossierModule, BrowserModule],
      declarations: [DossierPersonalDataComponent],
      providers: [
          DossierFileService,
          ErrorProcessor,
          {
            provide: ActivatedRoute,
            useValue: {
              snapshot: {
                data: {
                  dossier: {
                    firstName: 'hello',
                    lastName: 'world',
                    mobile: '111-111-1111',
                    company: 'carapax',
                    buddy: 'bud',
                    supervisor: 'super',
                    dateOfBirth: '1900-01-01',
                  },
                  dossierItems: [], // mockprofileImage: '',
                }
              }
            }
          },
        {
          provide: DomSanitizer,
          useValue: {
            sanitize: () =>'safeString',
            bypassSecurityTrustHtml: () =>'safeString'
          }
        }
      ]
    })
      .compileComponents()
      .then(() => {
        fixture = TestBed.createComponent(DossierPersonalDataComponent);
        component = fixture.componentInstance;
        component.compWindow =  myWindow; // this would override the value we are setting in constructor. 
        fixture.detectChanges(); // once we have overridden it, now call "detectChanges"
      });
  }));


  it('should create', () => {
    expect(component).toBeTruthy();
  }); 
});

Post a Comment for "Typeerror: Failed To Execute 'createobjecturl' On 'url': No Function Was Found That Matched The Signature Provided"