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

前言

我们经常用到的token还是cookie,都默认有一个过期时间

我们做鉴权的时候,很依赖这个,所以捣鼓下能不能再严谨点

因为之前都是以后台固定的格式,直接拿到值做一个简单的判断;

那,假如后台传过来的日期格式变了呢!!

有兴趣的瞧瞧,没兴趣的请勿往下走,节省您的时间!


前置基础

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

我们不讲配置,也不讲其他琐碎,只说实现过程


思路分析

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

  • 判断参数的类型,只考虑两种情况
  • 数字: 验证是否为一个正确的时间戳!!!!
  • 字符串: 验证是否是一个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上找了,

https://github.com/Microsoft/TypeScript/issues/21758,

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

https://github.com/Microsoft/TypeScript/commit/7b9ceb85fa4e19ade740faa2af2e00e62e16f7c9


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的所有功能了,这不是我想要的,

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

猜你喜欢

转载自juejin.im/post/5b615865f265da0faa368103
今日推荐