sj-freitas/forofa

View on GitHub
lib/functions/__tests__/last.test.js

Summary

Maintainability
A
2 hrs
Test Coverage
const { last } = require("./../last");

describe("last function", () => {
  it("Gets the unique and last value if no condition is present", () => {
    const value = last([1]);

    expect(value).toEqual(1);
  });

  it("Gets the  last value if no condition is present", () => {
    const value = last([1, 2, 3, 4, 5]);

    expect(value).toEqual(5);
  });

  it("Throws an exception if there are no values", () => {
    const lastOnEmptyIterable = () => last([]);

    expect(lastOnEmptyIterable).toThrow(Error);
    expect(lastOnEmptyIterable).toThrow(
      "Sequence contains no matching elements"
    );
  });

  it("Gets the last and unique item that fills in the condition", () => {
    const value = last([1, 2, 3], t => t % 2 === 0);

    expect(value).toEqual(2);
  });

  it("Gets the last item that fills in the condition", () => {
    const value = last([1, 2, 3, 4], t => t % 2 === 0);

    expect(value).toEqual(4);
  });
});