首页 > 解决方案 > 用于范围外变量的 Flux Store 的 JEST 规范

问题描述

我正在尝试为基于 Flux 的商店编写规范。组件外部定义了一些变量,这些变量在组件内部使用。当我尝试为它创建模拟时,它并没有捡起它。

员工商店

let employee_year = null;
let all_employees = [];

const dropdown = document.getElementById('employee_projects'); //This is the not the react component.

if (dropdown) { employee = dropdown.options[dropdown.selectedIndex].getAttribute('employee_year_id'); }

if (window.VIEW_DATA.all_employees) { all_employees= JSON.parse(window.VIEW_DATA.all_employees); }

const viewData = JSON.parseJSON({
  employee_details: all_employees.filter(e => e[employee_year])[0],
  employee_year: employee_year
});

class EmployeeStore extends Store { //Flux Store
{
  const employeeDetails = all_employees[employee_year] || [];
  const relatedDetails = employeeDetails.filter(e => e.type === 'Permanent');
}

JEST 规格

describe('EmployeeStore', () => {
    
    beforeAll(() => {
    
        global.employee_year  = 2019; //Not sure of this syntax

        window.VIEW_DATA.all_employees = [
                                 'employee_1': {}
                                 'employee_2': {}
        ];          

        dd = window.document.createElement('select');
        
        const option = window.document.createElement('option');

        option.setAttribute('employee_year_id', 2019);
        dd.appendChild(option);
        
        spyOn(window.document, 'getElementById').and.returnValue(dd);
        
    });
});

我得到的一切都是undefined我设定的beforeAll。有任何想法吗?

标签: reactjsjestjs

解决方案


推荐阅读