JS不靠谱系列: 写一个验证过时时间的函数,包含jest单元测试

前言

咱们常常用到的token仍是cookie,都默认有一个过时时间javascript

咱们作鉴权的时候,很依赖这个,因此捣鼓下能不能再严谨点java

由于以前都是之后台固定的格式,直接拿到值作一个简单的判断;git

那,假如后台传过来的日期格式变了呢!!有兴趣的瞧瞧。github

前置基础

  • jest : 这个测试框架很是不错,Facebook 出品
  • ES5&&ES6
  • Typescript

咱们不讲配置,也不讲其余琐碎,只说实现过程typescript

思路分析

重心其实就是围绕传参来执行cookie

  • 判断参数的类型,只考虑两种状况
  • 数字: 验证是否为一个正确的时间戳!!!!
  • 字符串: 验证是不是一个datetime格式,亦或者能够转换成识别的格式(好比 2018/08/01)
  • 类型的转换及比较
  • 最后返回布尔值,来肯定该值是否有效

效果图

代码实现

代码很少,只涵盖了这么几种状况,具体看测试的文字描述框架

js 版本

isDate.js , 暴露isDate函数,接收一个参数函数

function checkDateTime(d) {
  const _date = new Date(d);
  const Now = new Date().getTime();
  const DiffTime = _date.getTime() - Now;

  if (
    _date.getFullYear() === 1970 ||
    _date.getFullYear() < new Date().getFullYear()
  ) {
    // 如果传入的时间转换成1970年...那确定不是咱们后台要传的时间
    // 小于这个年份的也必然不是,谁的后台token过时时间超过一年的...
    return false;
  }

  if (DiffTime > 60000) {
    // 过时结束时间必须大于传入时间
    // 当过时时间还大于一分钟的时候,
    return true;
  } else {
    // 不然返回false,从外部调用这个函数拿到返回值,
    // 作二步处理,续期仍是强制退出什么鬼的
    return false;
  }
}

/** * @description 判断是否为正确的日期 * @param {*} d */
export const isDate = d => {
  // 任何不能给Date识别的参数,子函数调用的返回值为NaN
  return isNaN(new Date(d).getTime()) || new Date(d).getTime() === 0
    ? false
    : checkDateTime(d);
};



复制代码

ts版本

在vscode会有提示错误单元测试

DateConstructor: Argument of type 'string | number' is not assignable to parameter of type 'string'.测试

大致上说日期类型无法赋值字符串类型的值,这个问题彷佛等待修复,我在Github上找了,

github.com/Microsoft/T…,

有人提交了PR,不知道有没有合并进去..

github.com/Microsoft/T…

function checkDateTime(d: number | string): boolean {
  const _date: Date = new Date(d);
  const Now: number = new Date().getTime();
  const DiffTime: number = _date.getTime() - Now;

  if (
    _date.getFullYear() === 1970 ||
    _date.getFullYear() < new Date().getFullYear()
  ) {
    // 如果传入的时间转换成1970年...那确定不是咱们后台要传的时间
    // 小于这个年份的也必然不是,谁的后台token过时时间超过一年的...
    return false;
  }

  if (DiffTime > 60000) {
    // 当过时时间还大于一分钟的时候,
    return true;
  } else {
    // 不然返回false,从外部调用这个函数拿到返回值,
    // 作二步处理,续期仍是强制退出什么鬼的
    return false;
  }
}

/** * @description 判断是否为正确的日期 * @param {*} d */
export const isDate = (d: string | number) => {
  // 任何不能给Date识别的参数,子函数调用的返回值为NaN
  return isNaN(new Date(d).getTime()) || new Date(d).getTime() === 0
    ? false
    : checkDateTime(d);
};


复制代码

测试代码

import { isDate } from "../../src/utils/isDate";

describe("isDate函数测试集合组", () => {
  test("這種非標準的時間戳只會轉成1970這種,已經過期", () => {
    expect(isDate(21312445)).toBe(false);
  });
  test("已經過期", () => {
    expect(isDate(1533097116565)).toBe(false);
  });
  test("已經過期", () => {
    expect(isDate(1514764800000)).toBe(false);
  });
  test("传入undefined为false,不传参就是undefined", () => {
    expect(isDate(undefined)).toBe(false);
  });
  test("传入null虽然返回0,但也是false", () => {
    expect(isDate(null)).toBe(false);
  });
  test("標準格式的返回true", () => {
    expect(isDate("2018-12-01")).toBe(true);
  });
  test("標準格式的返回true", () => {
    expect(isDate("2018/8/09")).toBe(true);
  });
  test("歷史悠久的也是錯的", () => {
    expect(isDate("1988-10-21")).toBe(false);
  });
  test("非標準格式的返回false", () => {
    expect(isDate("1970-13-51")).toBe(false);
  });
  test("非標準的日期也是false", () => {
    expect(isDate("s2018ww-13-51")).toBe(false);
  });
  test("普通字符串會返回fasle", () => {
    expect(isDate("safdaserw")).toBe(false);
  });
});

复制代码

总结

纯函数测试只要声明推断返回值便可, 因此单元测试也很是的直白明了..

纯函数的好处就是能够低耦合,虽然咱们能够在这里高内聚,好比作续期,请求,路由跳转什么的,

那这样就是一个auth的全部功能了,这不是我想要的,

有不对之处请留言,会及时修正,谢谢阅读

相关文章
相关标签/搜索