Unit testing Jovo 4 Components

v4
unit-testing

#1

Hi all

Is there any way to force a component inside a unit test? I’m looking for a way to do the same as described here Unit Testing Hacks and Best Practices
This is for version 3 though.

I want to abstract my unit tests as much as possible and not re-test existing components that redirect to the component I want to test.

Thanks!


#2

Got it to work!

In this example I have 3 components, which are structured like this;

  • FeedbackComponent.ts
    • START()
  • WelcomeComponent.ts
    • START()
    • UNHANDLED()
    • giveFeedback will return this.$redirect(FeedbackComponent);
  • GlobalComponent.ts
    –> LAUNCH will return this.$redirect(WelcomeComponent)

Thing I wanted was to only test the FeedbackComponent’s methods, so I would not need to test all the application logic/flow for Global -> Welcome -> Feedback time and time again.

So my code looks somewhat like this.

# FeedbackComponent.ts

@Component()
export class FeedbackComponent extends BaseComponent {
  START(): Promise<void> {
    return this.$send('What is your feedback on my community question and answer?');
  }

In my unit test, I set the session state.

# FeedbackComponent.test.ts

describe('Components > FeedbackComponent', () => {
  const testSuite = new TestSuite();

  describe('START', () => {
    test('it can trigger the START() method', async () => {
      testSuite.$session.state = [{ component: 'FeedbackComponent' }];
      const expectedResult = [
        { message: 'What is your feedback on my community question and answer?' },
      ];
      const { output: actualResult } = await testSuite.run({
        type: InputType.Intent,
        intent: 'START',
      });

      expect(actualResult).toEqual(expectedResult);
    });
  });
});