简体   繁体   中英

Vue JS Component template rendering issue when testing

I am having issues at the moment with testing a vue component, the code below actually works but testing does not play ball. I have done some searching and I am aware there is an issue where Vue 2.0 only uses runtime-only build when importing vue and that I need to use vue/esm or use the render function. The problem I have with that is that i am not using webpack or any build system (using tsconfig) to create an alias.

I have tried just using template:'' and it still errors out. When i use the render/createElement function I can't seem to add in my own html file/template as it seems to only want to create a new element rather than inject a template.

ERROR: '[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

COMPONENT:

import {
    debounce
}
from 'npmPackage/debounce';
import Vue from 'vue';
import Component from 'vue-class-component';
import './navigation-bar.styles.less';
import {
    menuItemList,
    secondaryButton
}
from './types';

 @ Component({
    props: ['panels', 'secondaryButton'],
    // render: (createElement) => { return createElement('div', require('./navigation-bar.html')); }
    template: require('./navigation-bar.html')
})

export class NavigationBar extends Vue {
    public menuActive: boolean = false;
    public menuClass: string = '';
    public panels: menuItemList;
    public secondaryButton: secondaryButton;

    private animateMenu: () => void = debounce(this.toggleMenuClass, 300, true);

    public toggleMenu(): void {
        this.animateMenu();
    }

    private toggleMenuClass(): void {
        this.menuActive = !this.menuActive;
        this.menuClass = this.menuActive ? 'show' : 'hide';
    }
}

Vue.component('navigation-bar', NavigationBar);

UNIT TEST:

import {
    expect
}
from 'chai';
import {
    spy,
    stub
}
from 'sinon';
import Vue from 'vue';
import {
    NavigationBar
}
from '../src/navigation-bar.component'
import {
    IMenuItem
}
from "../src/types";

describe('Navigation bar component', () => {
    let panels: Array < IMenuItem > ;
    let navigationBar: NavigationBar;

    beforeEach(() => {
        panels = [{
                label: 'my-account',
                action: function () {}
            }, {
                label: 'home',
                action: stub
            }
        ];
        navigationBar = new NavigationBar();
        navigationBar.$mount();
    });

    describe('Checks that only one action is being called per panel', () => {
        it('checks actions are called', () => {
            console.log(navigationBar.$el);
            navigationBar.panels = panels;
            const menuItem = navigationBar.$el.querySelectorAll('.menu-item');
            menuItem.length.should.equal(panels.length);

            const menuItem1 = menuItem[0];
            menuItem1.should.be.instanceOf(HTMLElement);
            const html = menuItem1 as HTMLElement;
            html.click();
            panels[0].action.should.have.been.calledOnce;
            panels[1].action.should.not.have.been.called;
        });
    });
});

Get a copy of the CDN version of Vue and import that instead of the npm Vue.

Or alternatively you could import Vue from vue/dist/vue.js

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM