import "mocha"; import { expect } from "chai"; import { is } from "./helpers"; describe('Thunk factory "is.keyOf"', () => { const obj: any = { foo: 42, bar: { baz: 17, 'complex.key': "text" } }; const thunk = is.keyOf(obj); const keys = Object.keys(obj); const paths = { valid: [ ...keys.map(v => [v]), ["bar", "baz"] ], invalid: [ ["barr", "whatever", "text", 42], ["a", "b", "c"] ], }; it("tells whether the key exists in the object", () => { const invalid = [ "baz", "whatever", "text", 42 ]; for (const value of keys) expect(thunk(value)).to.be.true; for (const value of invalid) expect(thunk(value)).to.be.false; }); it("tells whether the path exists in the object", () => { for (const value of paths.valid) expect(thunk(value)).to.be.true; for (const value of paths.invalid) expect(thunk(value)).to.be.false; }); it("accepts shorthand syntax", () => { for (const path of paths.valid) expect(thunk(path.join("."))).to.be.true; expect(thunk("bar:complex.key", ":")).to.be.true; for (const path of paths.invalid) expect(thunk(path.join("."))).to.be.false; }); it("works with primitive values", () => { const thunk = is.keyOf(42); const valid = [ "toString", "toFixed", "toExponential", "toPrecision", "valueOf", "toLocaleString" ]; const invalid: any[] = [ 42, "text", /whatever/ ]; for (const value of valid) expect(thunk(value)).to.be.true; for (const value of invalid) expect(thunk(value)).to.be.false; }); });