Home > Software engineering >  Angular test for loop
Angular test for loop

Time:08-16

I have a method that check and replaces with empty strings those deltas in which the insert is a string and in which the length is zero when trimmed.

trimDelta(ops = []): void {
    for (const delta of ops) {
      if (
        typeof delta?.insert === 'string' &&
        delta?.insert?.trim().length === 0
      ) {
        delta.insert = '';
      } else {
        break;
      }
    }
  }

I have to check if when there is no string, when the string does not stretch to zero length, when there are only empty strings, etc. How to implement this case?

CodePudding user response:

Every test follows 3 basic steps:

  1. Setup teststate
  2. Execute
  3. Verify

Step 1 is very simple in your case, you define an ops object array. Step 2 is executing trimDelta function with the array from step 1. You save the result in another object array. In Step 3 you check the result and compare it's values. How would you expect the 2nd array to have been altered?

  • Related