Skip to content

Commit

Permalink
Clean up with correct prettier file
Browse files Browse the repository at this point in the history
  • Loading branch information
willhuang1997 committed Feb 7, 2023
1 parent c67493a commit 32e5b93
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 58 deletions.
16 changes: 14 additions & 2 deletions packages/cells/src/cells/common/support.ts
@@ -1,7 +1,19 @@
/**
* This is all copied from packages/core/src/common/support.ts for now.
*/

function panic(message: string = "This should not happen"): never {
throw new Error(message);
throw new Error(message);
}

export function assertNever(_never: never): never {
return panic("Hell froze over");
return panic("Hell froze over");
}

export function assert(
fact: boolean,
message: string = "Assertion failed"
): asserts fact {
if (fact) return;
return panic(message);
}
2 changes: 1 addition & 1 deletion packages/cells/src/cells/date-picker-cell.tsx
Expand Up @@ -111,7 +111,7 @@ const renderer: CustomRenderer<DatePickerCell> = {
onPaste: (v, d) => {
let newDate: Date | undefined;
if (d.format === "time") {
v = `1970-01-01T${v}Z`
v = `1970-01-01T${v}Z`;
}
try {
newDate = new Date(v);
Expand Down
101 changes: 46 additions & 55 deletions packages/cells/test/date-picker-cell.test.tsx
@@ -1,9 +1,6 @@
import {
GridCellKind,
isObjectEditorCallbackResult,
} from "@glideapps/glide-data-grid";
import { GridCellKind } from "@glideapps/glide-data-grid";
import * as React from "react";
import { assert } from "console";
import { assert } from "../src/cells/common/support";
import { fireEvent, render } from "@testing-library/react";
import renderer, {
DateKind,
Expand Down Expand Up @@ -58,7 +55,6 @@ describe("editor", () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(getMockDateCell()).editor;
assert(Editor !== undefined);
assert(!isObjectEditorCallbackResult(Editor));

const result = render(
<Editor isHighlighted={false} value={getMockDateCell()} />
Expand All @@ -71,37 +67,34 @@ describe("editor", () => {
expect(result.value === "04:47:44.584");
});

it.each([
['date'],
['time'],
['datetime-local']
])("renders with correct format", (format: string) => {
// @ts-ignore
const Editor = renderer.provideEditor?.(
it.each([["date"], ["time"], ["datetime-local"]])(
"renders with correct format",
(format: string) => {
// @ts-ignore
const Editor = renderer.provideEditor?.(
getMockDateCell({ data: { format: format } } as DatePickerCell)
// @ts-ignore
).editor;
).editor;
assert(Editor !== undefined);
assert(!isObjectEditorCallbackResult(Editor));


const result = render(
<Editor isHighlighted={false} value={getMockDateCell()} />
);
const input = result.getByTestId("test-id");
expect(input).not.toBeUndefined();

// @ts-ignore
expect(input.format === format);
})
const input = result.getByTestId("test-id");
expect(input).not.toBeUndefined();

// @ts-ignore
expect(input.format === format);
}
);

it("renders textarea when readonly is true", () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(
getMockDateCell({ data: { readonly: true } } as DatePickerCell)
// @ts-ignore
// @ts-ignore
).editor;
assert(Editor !== undefined);
assert(!isObjectEditorCallbackResult(Editor));

const result = render(
<Editor isHighlighted={false} value={getMockDateCell()} />
Expand All @@ -113,9 +106,9 @@ describe("editor", () => {
});

it("contains max, min, step when passed in", () => {
const min = "2018-01-01"
const max = "2018-12-31"
const step = ".001"
const min = "2018-01-01";
const max = "2018-12-31";
const step = ".001";
const extraProps = {
data: {
min,
Expand All @@ -127,7 +120,6 @@ describe("editor", () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(getMockDateCell(extraProps)).editor;
assert(Editor !== undefined);
assert(!isObjectEditorCallbackResult(Editor));

const result = render(
<Editor isHighlighted={false} value={getMockDateCell()} />
Expand All @@ -149,7 +141,6 @@ describe("editor", () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(getMockDateCell()).editor;
assert(Editor !== undefined);
assert(!isObjectEditorCallbackResult(Editor));

const mockCellOnChange = jest.fn();
const result = render(
Expand Down Expand Up @@ -186,7 +177,6 @@ describe("editor", () => {
// @ts-ignore
const Editor = renderer.provideEditor?.(getMockDateCell()).editor;
assert(Editor !== undefined);
assert(!isObjectEditorCallbackResult(Editor));

const mockCellOnChange = jest.fn();
const result = render(
Expand Down Expand Up @@ -218,32 +208,33 @@ describe("editor", () => {

describe("onPaste", () => {
it.each([
["2023-02-06T04:47:44.584Z"],
["1995-12-17T03:24:00"],
["Sun Dec 17 1995 03:24:00 GMT"],
[new Date(1995, 11, 17)],
[100],
[-1],
])("correctly returns a value when onPaste is called with valid value: %p", (input: string | number | Date) => {
// @ts-ignore
const { date } = renderer.onPaste(input, {});
expect(date).toStrictEqual(new Date(input));
});

it.each([
[""],
["invalid"],
["2020-20-12"],
["2020/20/12"],
])("correctly returns no value when onPaste is called with invalid value: %p", (input: string) => {
// @ts-ignore
const { date } = renderer.onPaste(input, {});
expect(date.getTime()).toBe(Number.NaN);
});
["2023-02-06T04:47:44.584Z"],
["1995-12-17T03:24:00"],
["Sun Dec 17 1995 03:24:00 GMT"],
[new Date(1995, 11, 17)],
[100],
[-1],
])(
"correctly returns a value when onPaste is called with valid value: %p",
(input: string | number | Date) => {
// @ts-ignore
const { date } = renderer.onPaste(input, {});
expect(date).toStrictEqual(new Date(input));
}
);

it('provides extra time support for onPaste', () => {
it.each([[""], ["invalid"], ["2020-20-12"], ["2020/20/12"]])(
"correctly returns no value when onPaste is called with invalid value: %p",
(input: string) => {
// @ts-ignore
const { date } = renderer.onPaste("00:00:00.000", {format: "time"});
const { date } = renderer.onPaste(input, {});
expect(date.getTime()).toBe(Number.NaN);
}
);

it("provides extra time support for onPaste", () => {
// @ts-ignore
const { date } = renderer.onPaste("00:00:00.000", { format: "time" });
expect(date).toStrictEqual(new Date("1970-01-01T00:00:00.000Z"));
})
});
});

0 comments on commit 32e5b93

Please sign in to comment.