javascript math.test.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript math.test.js相关的知识,希望对你有一定的参考价值。

import {
  round,
  secondsToHumanReadable,
  calculateFundedPercentage,
  avgOfArray,
  maxOfArray,
  metersToHumanReadable
} from "utils/math";

describe("round", () => {
  it("should round the number to the given precision", () => {
    expect(round(1.234, 2)).toBe(1.23);
    expect(round(5.2222222223)).toBe(5.22);
  });
});

describe("secondsToHumanReadable", () => {
  it("should display an integer in seconds as hh:mm:ss", () => {
    expect(secondsToHumanReadable(0)).toBe("0:00");
    expect(secondsToHumanReadable(1)).toBe("0:01");
    expect(secondsToHumanReadable(59)).toBe("0:59");
    expect(secondsToHumanReadable(60)).toBe("1:00");
    expect(secondsToHumanReadable(61)).toBe("1:01");
    expect(secondsToHumanReadable(21355)).toBe("5:55:55");
  });
});

describe("calculateFundedPercentage", () => {
  it("should calculate the percentage[string] of donated / goal ratio", () => {
    expect(calculateFundedPercentage(20, 200)).toBe("10");
  });
});

describe("avgOfArray", () => {
  it("should calculate the average number of all the numbers in a given array", () => {
    expect(avgOfArray([10, 20, 30])).toBe(20);
    expect(avgOfArray(["foo", 20, 30])).toBeFalsy();
  });
});

describe("maxOfArray", () => {
  it("should return the highest number in an array of numbers", () => {
    expect(maxOfArray([10, 20, 30])).toBe(30);
    expect(maxOfArray([10, 30, "foo", 99])).toBe(99);
  });
});

describe("metersToHumanReadable", () => {
  it("should consume meter and return a prettier version of the distance in meters", () => {
    expect(metersToHumanReadable(1000)).toBe("1 km");
    expect(metersToHumanReadable(1100)).toBe("1.1 km");
    expect(metersToHumanReadable(1950)).toBe("1.95 km");
  });
});

以上是关于javascript math.test.js的主要内容,如果未能解决你的问题,请参考以下文章

javascript的题。

javascript JavaScript isset()等效: - JavaScript

JavaScript 使用JavaScript更改CSS(JavaScript)

JavaScript之基础-1 JavaScript(概述基础语法)

前端基础-JavaScript的基本概述和语法

JavaScript