Home > Net >  Handle void method as object's property in React, redux, typescript
Handle void method as object's property in React, redux, typescript

Time:06-28

Below are the details. I am a little confuse on how to test my file with an object with function that return void.

type Pros={
 studentid: StudentId
 pageId?: PageID
closeForm: () =>void }

for unit test how should I do

const testPros={
studentid:"123"
padeId:"123"
closeForm:null //I am confuse here }

CodePudding user response:

Use a function that doesn't have implicit return nor has the return keyword anywhere. (Don't use return; or return undefined;.) You should also use pageId, not padeId, and each key-value pair should be separated with a comma.

const testPros = {
  studentid:"123",
  pageId:"123",
  closeForm() {
  }
};

CodePudding user response:

You can do something like this.

const testPros= {
  studentid: "123",
  pageId: "123",
  closeForm: () => {},
}

the type () => void means the closeForm accepts a function that has no parameters and does not have a return value.

You could also have a function like:

const closeFormHandler = () => {
  // Do something here without a return statement
}

const testPros= {
  studentid: "123",
  pageId: "123",
  closeForm: closeFormHandler,
}

  • Related