Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

Add test support for TypeScript-Classes that extend from Vue #115

Open
h2xd opened this issue Oct 29, 2020 · 2 comments
Open

Add test support for TypeScript-Classes that extend from Vue #115

h2xd opened this issue Oct 29, 2020 · 2 comments

Comments

@h2xd
Copy link

h2xd commented Oct 29, 2020

Hello there,

we would like to test our models with the vue-test-utils library.
But somehow the test-utils are not resolving the model, when it's mounted inside @vue/test-utils.createLocalVue.

Implementation

Here is a short example of our model and the test implementation:

import Vue from 'vue'
import { Component } from 'vue-property-decorator'

@Component({
  name: 'Authentication',
})
export class AuthenticationModel extends Vue {
  private _authenticated = false

  get authenticated() {
     return this._authenticated
  }

  public async login(email: string, password: string) {
    // ... authentication logic agains a mocked api
    this._authenticated = await this.api.login(email, password)
  }
}
import { createLocalVue } from '@vue/test-utils'
import { AuthenticationModel } from './auth'

async function generateAuthenticationModel(): Promise<AuthenticationModel> {
  const LocalVue = createLocalVue({
    models: { Auth: AuthenticationModel },
  })

  const { Auth } = LocalVue

  return Auth
}

it('should be able to login user', async () => {
  const AuthenticationTestModel = await generateAuthenticationModel()

  expect(AuthenticationTestModel.authenticated).toBeFalsy()

  await AuthenticationTestModel.login({
    email: '[email protected]',
    password: 'some-password-that-matches',
  })

  expect(AuthenticationTestModel.authenticated).toBeTruthy()
})

Expected

LocalVue should be filled with the imported and mounted model


Hit me up, if you need more examples or further explanation. :)

@Djaler
Copy link

Djaler commented Mar 22, 2021

Hello, @h2xd. Have you found a way to test your typescript models?

@Djaler
Copy link

Djaler commented Mar 23, 2021

Hmm, looks like you don't need to use vue-test-utils to test a model. Just create its instance, like new AuthenticationModel() and test it.

But I have a new question now. How can I mock model in host component? @JohannesLamberts can you help?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants