Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/util/fill/fill.test.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { fill } from "./fill";6import { expectType } from "tsd";78test("Supplied default should be merged in to target even if marked undefined", () => {9const opts: { name: string; height?: number } = {10name: "jack",11height: undefined,12};13const actual = fill(opts, { height: 20 });14expect(actual).toStrictEqual({ name: "jack", height: 20 });15});1617test("Defaults should not overwrite already defined optional params", () => {18const opts: { name: string; height?: number; weight?: number } = {19name: "jack",20height: 20,21};22const actual = fill(opts, { height: 30 });23expect(actual).toStrictEqual({ name: "jack", height: 20 });24});2526test("Missing optional params should not appear if not given defaults", () => {27const opts: { name: string; height?: number; weight?: number } = {28name: "jack",29};30const actual = fill(opts, { height: 20 });31expect(actual).toStrictEqual({ name: "jack", height: 20 });32});3334test("Supplied default should guarantee type existance", () => {35type Expected = {36name: string;37direction: "up" | "down" | "left" | "right";38highlight: boolean;39animate?: boolean;40};4142const opts: {43name: string;44direction: "up" | "down" | "left" | "right";45highlight?: boolean;46animate?: boolean;47} = { name: "foo", direction: "up" };4849const actual = fill(opts, { highlight: false });5051expectType<Expected>(actual);52});5354test("strings", () => {55function filled(props: {56name: string;57direction: "up" | "down" | "left" | "right";58highlight?: string;59animate?: boolean;60}) {61// This should not end up narrowing to the fixed value62return fill(props, { highlight: "fixed_string" });63}64const a = filled({ name: "foo", direction: "up" });65expectType<string>(a.name);66expectType<"up" | "down" | "left" | "right">(a.direction);67expectType<string>(a.highlight);68expectType<boolean | undefined>(a.animate);69});7071// tsd expectError doesn't integrate into Jest72test("Errors", () => {73/*74function prop_typed_errors(props: {75name: string;76direction: "up" | "down" | "left" | "right";77highlight?: boolean;78animate?: boolean;79}) {80// Don't allow requireds to even be listed81return fill(props, { name: "undefined", highlight: false });82}83*/84});858687