Skip to content

Commit

Permalink
Add test for GridTemplateType and GridDataGet
Browse files Browse the repository at this point in the history
  • Loading branch information
garryxiao committed Dec 6, 2024
1 parent 40e5f89 commit a3a9e65
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 18 deletions.
55 changes: 55 additions & 0 deletions __tests__/GridLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { DataTypes } from "@etsoo/shared";
import {
GridDataGetData,
GridTemplateType
} from "../src/components/GridLoader";

const template = {
keyword: "string",
deviceId: "number",
creationStart: "date",
creationEnd: "date"
} as const satisfies DataTypes.BasicTemplate;

test("Tests for GridTemplateType", () => {
const data: GridTemplateType<typeof template> = {
keyword: "test",
deviceId: 1,
creationStart: new Date()
};

expect(data.keyword).toBe("test");
expect(data.deviceId).toBe(1);
});

test("Tests for GridDataGetData with keeping source", () => {
const data = {
keyword: "test",
deviceId: 1,
creationStart: "2024/12/06",
other: false
};

const result = GridDataGetData(data, template, true);

expect(result.keyword).toBe("test");
expect(result.deviceId).toBe(1);
expect(result.creationStart).toBeInstanceOf(Date);
expect((result as any).other).toBe(false);
});

test("Tests for GridDataGetData without keeping source", () => {
const data = {
keyword: "test",
deviceId: 1,
creationStart: "2024/12/06",
other: false
};

const result = GridDataGetData(data, template, false);

expect(result.keyword).toBe("test");
expect(result.deviceId).toBe(1);
expect(result.creationStart).toBeInstanceOf(Date);
expect((result as any).other).toBeUndefined();
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@etsoo/react",
"version": "1.8.10",
"version": "1.8.11",
"description": "TypeScript ReactJs UI Independent Framework",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
30 changes: 15 additions & 15 deletions src/components/GridLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export type GridData = FormData | DataTypes.StringRecord;
/**
* Grid template type
*/
export type GridTemplateType<o extends object> = DataTypes.BasicTemplateType<{
[k in keyof o]: k extends "date"
export type GridTemplateType<T> = DataTypes.BasicTemplateType<{
[k in keyof T]: T[k] extends "date"
? "date" | "string"
: k extends DataTypes.BasicNames
? DataTypes.BasicNames
: T[k] extends DataTypes.BasicNames
? T[k]
: never;
}>;

Expand All @@ -39,16 +39,16 @@ export type GridTemplateType<o extends object> = DataTypes.BasicTemplateType<{
* @param keepSource Keep source data
* @returns Json data
*/
export function GridDataGet(
export function GridDataGet<const T>(
props: GridLoadDataProps,
template: object = {} satisfies DataTypes.BasicTemplate,
keepSource: boolean = true
): GridJsonData & GridTemplateType<typeof template> {
template?: T,
keepSource?: boolean
): GridJsonData & GridTemplateType<T> {
// Destruct
const { data, ...rest } = props;

// DomUtils.dataAs(data, template);
return { ...GridDataGetData(data, template, keepSource), ...rest };
return { ...GridDataGetData<T>(data, template, keepSource), ...rest };
}

/**
Expand All @@ -58,24 +58,24 @@ export function GridDataGet(
* @param keepSource Keep source data
* @returns Json data
*/
export function GridDataGetData(
export function GridDataGetData<const T>(
data?: GridData,
template: object = {} satisfies DataTypes.BasicTemplate,
keepSource: boolean = true
): GridTemplateType<typeof template> {
template?: T,
keepSource?: boolean
): GridTemplateType<T> {
// Clear form empty value
if (data instanceof FormData) {
DomUtils.clearFormData(data);
}

// Conditions
// Set keepSource to true to hold form data, even they are invisible from the conditions
const conditions: GridTemplateType<typeof template> =
const conditions =
data == null
? {}
: DomUtils.dataAs(data, template as DataTypes.BasicTemplate, keepSource);

return conditions;
return conditions as any;
}

/**
Expand Down

0 comments on commit a3a9e65

Please sign in to comment.