Nualiian
11/26/2017 - 6:12 PM

Unit tests (VATSS)

Unit tests (VATSS)

import {
  round,
  secondsToHumanReadable,
  calculateFundedPercentage,
  avgOfArray
} from "utilities/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 avreage number of all the numbers in a givne array", () => {
    expect(avgOfArray([10, 20, 30])).toBe(20);
    expect(avgOfArray(["foo", 20, 30])).toBeFalsy();
  });
});
import {
  getFirstWord,
  parseJwt,
  resolveImage,
  roundToNoDecimals
} from "utilities/parsing";

import { baseWebUrl } from "constants/api";

describe("getFirstWord", () => {
  it("should get the first of the passed string", () => {
    expect(getFirstWord(" ")).toBe("");
    expect(getFirstWord("")).toBe("");
    expect(getFirstWord()).toBeFalsy();
    expect(getFirstWord("Lorem ipsum dolor sit amet")).toBe("Lorem");
    expect(getFirstWord("LoremIpsum")).toBe("LoremIpsum");
  });
});

describe("parseJwt", () => {
  const jwtString =
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
  const jwtDecoded = { admin: true, name: "John Doe", sub: "1234567890" };

  it("should sucessfuly parse a JWT token in a string format into a JSON", () => {
    expect(parseJwt(jwtString)).toEqual(jwtDecoded);
  });
});

describe("resolveImage", () => {
  const imageUrl = "foo.png";

  it("should return a full url to an image on the web", () => {
    expect(resolveImage(imageUrl)).toEqual(`${baseWebUrl}/${imageUrl}`);
  });
});

describe("roundToNoDecimals", () => {
  it("should remove all decimals from a real number", () => {
    expect(roundToNoDecimals(0)).toBe(0);
    expect(roundToNoDecimals(1.4445)).toBe(1);
    expect(roundToNoDecimals(1.00001)).toBe(1);
    expect(roundToNoDecimals(1.9999)).toBe(2);
  });
});