Тестирование рендеринга с помощью Jest - не удается прочитать свойство 'createDocumentFragment' из неопределенного - PullRequest
0 голосов
/ 04 июля 2018

Я пытаюсь написать очень простой модульный тест с Jest для вида Backbone / Marionette.

код теста:

import MyView from './MyView';

describe('test of my view', function() {
  const myView = new MyView().render();
});

Но это возвращает следующую ошибку TypeError: Cannot read property 'createDocumentFragment' of undefined.

at buildFragment (node_modules/jquery/dist/jquery.js:4311:22)
  at domManip (node_modules/jquery/dist/jquery.js:5238:14)
  at jQuery.fn.init.append (node_modules/jquery/dist/jquery.js:5431:10)
  at jQuery.fn.init.<anonymous> (node_modules/jquery/dist/jquery.js:5525:18)
  at access (node_modules/jquery/dist/jquery.js:3642:7)
  at jQuery.fn.init.html (node_modules/jquery/dist/jquery.js:5492:10)
  at Marionette.TemplateCache.loadTemplate (node_modules/backbone.marionette/lib/core/backbone.marionette.js:1169:24)
  at Marionette.TemplateCache.load (node_modules/backbone.marionette/lib/core/backbone.marionette.js:1149:27)
  at Function.get (node_modules/backbone.marionette/lib/core/backbone.marionette.js:1111:29)
  at Object.render (node_modules/backbone.marionette/lib/core/backbone.marionette.js:1200:87)
  at child._renderTemplate (node_modules/backbone.marionette/lib/core/backbone.marionette.js:1664:38)
  at child.render (node_modules/backbone.marionette/lib/core/backbone.marionette.js:1632:12)
  at child.render (node_modules/backbone.marionette/lib/core/backbone.marionette.js:2628:51)
  at Object.<anonymous> (src/js/my.view.spec.js:22:27)

Я не уверен, как я могу это исправить, так как мой взгляд довольно тупой (вывод html с жестким кодом), и проблема, похоже, больше связана с магистралью / марионеткой / jquery, чем с кодом моего представления.

Код MyView (упрощен до одной кнопки, но для многих кнопок это просто дублирование):

import Marionette from 'backbone.marionette';

import { LeftMenuView as LeftMenuViewTemplate } from './templates.index';

const LeftMenuView = Marionette.LayoutView.extend({
    template: LeftMenuViewTemplate,

    ui: {
        generalButton: '.menu-general',
        generalLink: '.menu-general-link',
    },
    buttonsList: [
        'generalButton',
    ],
    events: {
    '    click @ui.generalLink': 'switchToGeneral',
    },

    initialize() {},

    onShow() {
        this.ui.navLine.height(this.ui.generalButton.innerHeight());
        this.ui.navLine.css('top', this.ui.generalButton.position().top);
    },

    _animate(target) {
        const topPos = target.position().top;
        const newHeight = target.innerHeight();
        this.ui.navLine.stop().animate({
            top: topPos,
            height: newHeight,
        });
    },

    setInactiveButtons() {
        for (let i = 0; i < this.buttonsList.length; i += 1) {
            this.ui[this.buttonsList[i]].toggleClass('active', false);
        }
    },

    switchToGeneral() {
        this.setInactiveButtons();
        this.ui.generalButton.toggleClass('active', true);
        this.trigger('changePage:toGeneral');
        this._animate(this.ui.generalButton);
    },

});

export default LeftMenuView;

И соответствующий код шаблона (файл handlebar .hbs):

<div class="nav left-menu">
    <div class="vertical-nav-line"> </div>
        <ul class="settings-menu">
            <li class="menu-general active"><a href="#" class="menu-general-link">{{i18n 'NavMenuGeneral'}}</a></li>
        </ul>
</div>
...