使用笑话酶测试功能-无道具/无状态

时间:2019-04-15 19:50:37

标签: javascript jestjs

我正在尝试测试一个功能。它不调用任何道具或设置状态,所以我不确定如何测试 这是功能

sanitizeEmail = (email) => {
 let result = email;
 if(result.indexOf('@') !== -1 && result.substring(0,result.indexOf('@')).length > 3 ) {
   let end = result.indexOf('@');
   let temp = '';
   for(let i in result.substring(4,end)) {
    temp = temp + '*';
  }
   result = result.substring(0,3) + temp + result.substring(end, result.length);
 }
 return result
}

测试

 beforeEach(() => wrapper = mount(<MemoryRouter keyLength={0}><ProfileMenuComponent {...baseProps} /></MemoryRouter>));   

测试方法/功能

这是我通常调用函数或方法以进行测试的方式:

expect(wrapper.find('ProfileMenuComponent').instance().sanitizeEmail('test')).toEqual();

1 个答案:

答案 0 :(得分:0)

使用不同的参数集简单地调用方法(以便它涵盖所有流)。并比较返回值。

一个例子

方法

SELECT DATE_FORMAT(curr.FirstMonth, "%Y-%m") AS `Month`,
       curr.SaleID,
       curr.Total_Sales,
       CONCAT(
              ROUND((`curr`.Total_Sales - `prev`.Total_Sales) / `prev`.Total_Sales
                    , 2)*100  
             , '%') AS `Growth`
FROM
   (SELECT DATE_ADD(LAST_DAY(DATE_SUB(SaleDate, INTERVAL 1 MONTH))
                    , INTERVAL 1 DAY) As FirstMonth,
           SaleID,
           SUM(`Total`) As `Total_Sales`
    FROM Sales
    GROUP BY 
           DATE_ADD(LAST_DAY(DATE_SUB(SaleDate, INTERVAL 1 MONTH))
                    , INTERVAL 1 DAY),
           SaleID
   ) AS `curr`
LEFT JOIN
   (SELECT DATE_ADD(LAST_DAY(DATE_SUB(SaleDate, INTERVAL 1 MONTH))
                    , INTERVAL 1 DAY) As FirstMonth,
           SaleID,
           SUM(`Total`) As `Total_Sales`
    FROM Sales
    GROUP BY 
           DATE_ADD(LAST_DAY(DATE_SUB(SaleDate, 
                                      INTERVAL 1 MONTH)) 
                    , INTERVAL 1 DAY),
           SaleID
   ) AS `prev`
ON `curr`.SaleID = `prev`.SaleID   
AND `curr`.FirstMonth - INTERVAL 1 MONTH = `prev`.FirstMonth
AND `curr`.FirstMonth BETWEEN '2018-04-00' AND '2040-00-00'     

测试

export const sortByField = (field, isRevered = false, primerFn) => {

        if (field) {
            var key = primerFn ? (x) => primerFn(x[field]) : (x) => x[field];

            isRevered = !isRevered ? 1 : -1;

            return (a, b) => {
                /*eslint-disable */
                return a = key(a), b = key(b), isRevered * ((a > b) - (b > a));
                /*eslint-enable */
            }
        }
        else {
            return (a, b) => {
                return isRevered ? a < b : a > b;
            }
        }
    }