[Vuejs]-Unit testing vue component, using vue-cli, can't test parent methods?

0👍

You should (nearly) never call something from a parent component.

You have 2 possibilities:
1. Use a vuex store and save your data there. Then every component in need can access it by a store getter.
2. Provide the needed data as props to your child components.

Because the props thing gets very quick messy a suggest to use the store.

If you do so your tests will become much clearer and easier to read.

Simple test with a mocked store (just to show how things could be):

describe('MyComponent', () => {
  let wrapper

  beforeEach(() => {
    const localVue = createLocalVue()
    wrapper = mount(MyComponent, {
      localVue,
      mocks: {
        $store: {
          getters: {
            'copyright': 'MyCopyright text'
          }
        }
      }
    })
  })

  it('should display copyright text', () => {
    expect(wrapper.text('MyCopyright text')).toBe(true) 
  })
})

Leave a comment