Unit testing Vue JS component that uses MSAL.js for authentication

Viewed 316

I have a Vue component which uses the created hook to connect to AzureAD and authenticate a user

The code looks like this:

  created () {
    this.loginOnLoad()
  },
  methods: {
    async loginOnLoad () {
      this.authService = new AuthService()
      let user = this.authService.sso()
      if (!user) {
        this.loginFailed = true
        await this.login()
        await this.authService.assignAccessTokenToHeader()
        NotificationsService.successNotification('Logged in successfully')
      } else {
        await this.authService.assignAccessTokenToHeader()
        NotificationsService.successNotification('Welcome Back')
      }
    }
  }

Now, I'm trying to write unit tests using vue-test-utils and Jest, however, when I run the tests the result is saying that user login is required. This is due to me not having mocked the loginOnLoad method, however, before I do that I'd like to know if actually mocking that method is the right way to go i.e. let loginOnLoad = jest.fn() or that will take away the value of the tests?

0 Answers
Related