TestItem object calling function() as parameter

This demonstrate an issue for binding this

by Stephane de Luca

JavaScript

/**
 * The test item
 */


/*
class TestItem {

	constructor(name, isSuccess) {
    this.isSuccess = isSuccess
    this.name= name
  }
  
  
  async runTest() {
  
       console.log('~>Test running')
       return await new Promise((resolve, reject) => {
       let wait = setTimeout(() => {
          console.log('<~Test completed')
          resolve('Promise resolved!');
        }, 1000)
		})
 	}
  
  
  test() {
  	return this.runTest()
    .finally(() => {
    
    	this.isSuccess()
    })
  }
}
*/

class TestItem {
/*
  name = ''
  type = null


  tested = false
  resultJson = null
  responseStatusCode = null




  isSuccess = () => {
    console.log('isSuccess() not implemented')
    return this.responseStatusCode === 200
  }

  runTest = () => { return null }
*/
  /**
   * @summary Creates a test item.
   * @param {*} name The name of the test.
   * @param {*} type The type of the test.
   * @param {*} isSuccess The function that tells whenter the test passed or failed.
   * @param {*} runTest The function to test.
   */
  constructor(name, type, isSuccess, runTest) {
    this.name = name
    this.type = type
    this.isSuccess = isSuccess
    this.runTest = runTest

    this.tested = false
    this.resultJson = null
    this.responseStatusCode = null

    //console.log(`TestItem: `, this)
  }

  /**
   * @summary Prepares the test prior to run.
   */
  prepareBeforeRunning() {
    this.running = true
    this.tested = false
    this.success = null
    this.error = null
  }


  /**
   * @summary Runs the test with parameters from this.props
   * @returns The runTest promise
   */
  test( /*updateTestItem*/ ) {


    // Prepare the test by reseting runtime information (and render display)
    console.log(`~~⇝ Test ${this.name} running`)

    this.prepareBeforeRunning()
    //updateTestItem(this.id, this)

    // Run the test…
    return this.runTest()
      .then(json => {
        // The test has already been tested (ran).
        this.tested = true
       ...