0

I am trying to write a unit test case where it calls a method onclick of a dropdown in my component. While running my testcase it gives me the below error.

 error TS2345: Argument of type 'string' is not assignable to parameter of type 'Expected<void>'.

Generator.component.ts

 templateChanged(selectedTemplate: any) : void{
    let profileName: string = '';

    switch (selectedTemplate) {
      case '1': { // generic app
        profileName = 'generic';
        break;
      }
      case '2': { // spring boot
        profileName = 'springboot';
        break;
      }
      default: {
        alert('selection not handled');
      }
    }
    this.generatorModel = this.generatorService.getDataModel(profileName);
    this.formDisabled = false;

    for (var k = 0; k < this.generatorModel.ImageDetails.imageList.length; k++) {
      if (this.generatorModel.ImageDetails.imageRepo == this.generatorModel.ImageDetails.imageList[k].value) {
        this.imageName = this.generatorModel.ImageDetails.imageList[k].name
      }
    }
    this.templateProfile = profileName;
    return (this.templateProfile);
  }

Generator.component.spec.ts

import { GeneratorComponent } from "./fm-generator.component";

let templateChanged : GeneratorComponent["templateChanged"];

describe('GeneratorComponent', () => {
  let applicationTemplate = "";
  
  beforeEach(() => {
    applicationTemplate = "springboot";    
  });

  it('should call templateChanged',() => {
    const ChosenApplicationTemplate: void = templateChanged(2);
    expect (ChosenApplicationTemplate)
      .toBe("springboot");
  });
});

I understood I am making some mistake in passing the argument for the templateChanged() Method, but not getting the correct syntax to apss teh argument. Could anyone please help me solve this issue.

Priyanka
  • 3
  • 1
  • 4
  • It looks like your return typing for the function doesn't match your return. `templateChanged(selectedTemplate: any) : void` doesn't match `return (this.templateProfile);` – tswei Jul 26 '22 at 09:15
  • Even if i remove `return (this.templateProfile);` , I am getting the same error @tswei – Priyanka Jul 26 '22 at 09:25
  • You are correct, it's not specifically that. Check the portion where you're performing your expect().toBe() in your test. Similar Q&A: https://stackoverflow.com/a/44583559/4771663 – tswei Jul 26 '22 at 10:13

0 Answers0