'Angular Jasmine Unit test Expected spy to have been called

I'm trying to test some methods with angular and i've got some errors like,

Error: Expected spy formatInputFile to have been called.

and

Expected spy FileReader.readAsText to have been called.

Here is the Testcomponent

public readFile(inputValue: any) {
    this.isReset = false;
    let file: File = inputValue;
    let myReader: FileReader = new FileReader();
    clearTimeout(this.errorTimer);
    if (file.type !== "text/plain") {
      this.errorMessage = 'Votre fichier doit être un fichier text (.txt)'
      this.reset();
      console.log("this.errorMessage", this.errorMessage)
      this.errorTimer = setTimeout(() => {
        this.errorMessage = '';
      }, 3000);
    } else {
      myReader.onload = async () => {
        try {
          this.fileString = await myReader.result as string;
          this.formatInputFile(this.fileString)
        } catch (error) {
          this.reset();
          console.log("Invalid file format", error);
        }
      }
      myReader.readAsText(file);
    }
  }

here's a little mock.component:

export const fakeFileListPlainText = [{
  name: 'test.txt', size: 500001, type: "text/plain"
}]

export const fakeFileListJpg = [{
  name: 'test.txt', size: 500001, type: ".jpg"
}]

and the test.spec:

describe('Test', () => {
  let test: TestComponent;
  let inputFileComponent: InputFileComponent;
  let fixture: ComponentFixture<TestComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [TestComponent],
      schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
      providers: [TestComponent, {
        provide: InputFileComponent,
        useValue: inputFileComponent
      }]
    })
      .compileComponents();
  });

  beforeEach(() => {

    fixture = TestBed.createComponent(TestComponent);
    testComponenent = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create the map', () => {

    expect(testComponenent).toBeTruthy();
  });

  it('should call readAsText() if file input type is plain text file', () => {
    const fileReaderSpy: FileReader = jasmine.createSpyObj('FileReader', ['readAsText', 'onload']);
    spyOn(window as any, 'FileReader').and.returnValue(fileReaderSpy);
    spyOn(testComponenent, 'reset');

    test.readFile(fakeFileListPlainText);

    expect(fileReaderSpy.readAsText).toHaveBeenCalled();
    expect(fileReaderSpy.onload).toHaveBeenCalled();

  });

  it('should call formatInputFile() if file input type is plain text file', () => {
    const fileReaderSpy: FileReader = jasmine.createSpyObj('FileReader', ['readAsText', 'onload']);
    const formatInputSpy = spyOn(testComponenent, 'formatInputFile')
    spyOn(window as any, 'FileReader').and.returnValue(fileReaderSpy);

    testComponenent.readFile(fakeFileListPlainText);

    expect(formatInputSpy).toHaveBeenCalled();
  });

I call a spy here const formatInputSpy = spyOn(testComponenent, 'formatInputFile') , i think the error tell the spy is never called, it's mean that methods method thought the readFile() method are never called ? what did i miss in my test ?

Thank you for help :)



Sources

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

Source: Stack Overflow

Solution Source