Vue.js单元:测试-如何处理mixin并将其传递给道具

时间:2018-09-20 15:47:52

标签: unit-testing vue.js mixins vue-test-utils

我正在尝试测试我的使用mixin(vue-howler)的AudioPlayer组件

<script>
import VueHowler from "vue-howler";

...

export default {
  mixins: [VueHowler],
  name: "audioPlayer",
  data() {
    return {
      percentage: 0
    };
  },
...
</script>

我需要将道具传递给mixin,我该怎么做?

  sources ( an Array of audio files ), autoplay (Boolean ) , ...

我的AudioPlayer.spec.js

import Vue from "vue";
import { shallowMount } from "@vue/test-utils";
import Vuetify from "vuetify";
import AudioPlayer from "@/components/Home/AudioPlayer.vue";
import VueHowler from "vue-howler";


describe("AudioPlayer.vue", () => {
  beforeEach(() => {
    Vue.use(Vuetify);
    const el = document.createElement("div");
    el.setAttribute("data-app", true);
    document.body.appendChild(el);
  });

  const sourceFiles = [require("@/assets/audio/ultimo_desejo.mp3")];

  it("should emit an event PLAY/PAUSE  from playPauseBtn", () => {
    // given
    const wrapper = shallowMount(AudioPlayer, {
      attachToDocument: true,
      mixins: [VueHowler]
    });
    wrapper.setData({ paused: true, playing: false });
    console.log(wrapper.html());
  });

v叫混合

import { Howl } from 'howler'
import clamp from 'math-clamp'
import values from 'object-values'
import assign from 'object-assign'

export default {
  props: {
    /**
     * An array of audio file urls
     */
    sources: {
      type: Array,
      required: true,
      validator(sources) {
        // Every source must be a non-empty string
        return sources.every(
          source => typeof source === 'string' && source.length > 0
        )
      }
    },
    /**
     * Whether to start the playback
     * when the component is mounted
     */
    autoplay: {
      type: Boolean,
      default: false
    },
    /**
     * Whether to start the playback again
     * automatically after it is done playing
     */
    loop: {
      type: Boolean,
      default: false
      ....

1 个答案:

答案 0 :(得分:1)

解决了...需要在浅层安装混合器之前通过propseDta

  const sourceFiles = ['require("@/assets/audio/ultimo_desejo.mp3"']

  it("should emit an event PLAY/PAUSE  from playPauseBtn", () => {
    // given
    const wrapper = shallowMount(AudioPlayer, {
      // attachToDocument: true,
      propsData: {
        sources: sourceFiles,
        autoplay: false,
        loop: false
      },
      mixins: [VueHowler]
    });
    wrapper.setData({ paused: true, playing: false });
    console.log(wrapper.html());
  });