rc-js-util
Version:
A collection of TS and C++ utilities to help writing performant and correct applications, achieved through strict typing and (removable) invariant checking.
46 lines (41 loc) • 1.34 kB
text/typescript
import { arrayIndex } from "./array-index.js";
import { _Production } from "../../production/_production.js";
import { Test_setDefaultFlags } from "../../test-util/test_set-default-flags.js";
describe("=> arrayIndex", () =>
{
beforeEach(() =>
{
Test_setDefaultFlags();
});
const values = ["a", "b", "c"] as const;
it("| calls the callback with the correct parameters", () =>
{
const spy = jasmine.createSpy();
spy.and.returnValue(null);
arrayIndex(values, spy);
expect(spy.calls.count()).toBe(3);
expect(spy.calls.argsFor(0)).toEqual(["a", 0]);
expect(spy.calls.argsFor(1)).toEqual(["b", 1]);
expect(spy.calls.argsFor(2)).toEqual(["c", 2]);
});
it("| returns the indexed result", () =>
{
const result = arrayIndex(values, (value) =>
{
switch (value)
{
case "a":
return null;
case "b":
return undefined;
case "c":
return "d";
default:
return _Production.assertValueIsNever(value);
}
});
expect(result.size).toEqual(2);
expect(result.get(undefined)).toEqual("b");
expect(result.get("d")).toEqual("c");
});
});