Skip to content

Test methods (Composition API) #818

@standbyoneself

Description

@standbyoneself

I am trying to test my method logout() that should be called by clicking on the img element. In order to do that and also because we cannot access to this method directly by wrapper.vm.* I want to assert that inner methods inside the logout() are also called. But these methods are not called.

User.vue:

<template>
  <div class="user">
    <img
      class="user__avatar"
      :src="user.avatar_url"
      data-testid="user-avatar"
    />
    <animated-anchor
      :text="user.login"
      :href="user.html_url"
      class="user__name"
    />
    <img
      :src="logoutIcon"
      class="user__logout-icon"
      @click="logout()"
      data-testid="user-logout-icon"
    />
  </div>
</template>
<script lang="ts">
import { defineComponent, PropType } from '@vue/runtime-core';
import { GithubUser } from '@/types';
import AnimatedAnchor from './AnimatedAnchor.vue';
import logoutIcon from '@/assets/icons/logout.svg';
import { useRouter } from 'vue-router';
import { useStore } from '@/store';

export default defineComponent({
  components: { AnimatedAnchor },
  name: 'User',
  props: {
    user: {
      type: Object as PropType<GithubUser>,
      required: true,
    },
  },
  setup() {
    const router = useRouter();
    const store = useStore();

    const logout = async () => {
      await store.dispatch('logout');
      router.push({ name: 'Auth' });
    };

    return { logoutIcon, logout };
  },
});
</script>

User.spec.ts:

jest.mock('vue-router', () => ({
  ...jest.requireActual('vue-router'),
  useRouter: () => ({
    push: jest.fn(),
  }),
}));

jest.mock('@/store');

import User from '@/components/User.vue';
import { shallowMount } from '@vue/test-utils';
import userStub from '../../../__stubs__/user.stub';
import { useRouter } from 'vue-router';
import { useStore } from '@/store';

describe('User.vue', () => {
  describe('logout icon', () => {
    it.only('should call `logout()` on click', async () => {
      const wrapper = shallowMount(User, {
        props: {
          user: userStub,
        },
      });

      const router = useRouter();
      const store = useStore();

      const logoutIcon = wrapper.get('[data-testid="user-logout-icon"]');

      await logoutIcon.trigger('click');

      expect(store.dispatch).toHaveBeenCalled();
      expect(router.push).toHaveBeenCalled();
    });
  });
});

/src/store/__mocks__/index.ts:

export const useStore = () => ({
  dispatch: jest.fn(),
});

Running tests causes this:
● User.vue › logout icon › should call logout() on click

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

  70 |       await logoutIcon.trigger('click');
  71 |
> 72 |       expect(store.dispatch).toHaveBeenCalled();
     |                              ^
  73 |       expect(router.push).toHaveBeenCalled();
  74 |     });
  75 |   });

  at Object.<anonymous> (__tests__/unit/components/User.spec.ts:72:30)

I have the reproduction repository: https://github.com/standbyoneself/ws-test-client

What am I do wrong? How to fix that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions