зеркало из https://github.com/Azure/BatchExplorer.git
Sets proper name on record type for identification
Also supports a custom name on the `@Model()` decorator.
This commit is contained in:
Родитель
7ccf1f58a1
Коммит
1c2cb2cf59
|
@ -56,13 +56,12 @@ export function ListProp<T>(type: any) {
|
|||
};
|
||||
}
|
||||
|
||||
export function Model() {
|
||||
export function Model(name?: string) {
|
||||
return <T extends new(...args: any[]) => {}>(ctr: T) => {
|
||||
if (!(ctr.prototype instanceof Record)) {
|
||||
throw new RecordMissingExtendsError(ctr);
|
||||
}
|
||||
|
||||
return (class extends ctr {
|
||||
const model = (class extends ctr {
|
||||
constructor(...args: any[]) {
|
||||
const [data] = args;
|
||||
if (data instanceof ctr) {
|
||||
|
@ -72,5 +71,7 @@ export function Model() {
|
|||
(this as any)._completeInitialization();
|
||||
}
|
||||
});
|
||||
Object.defineProperty(model, "name", { value: name || ctr.name });
|
||||
return model;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -44,6 +44,12 @@ class InheritedTestRec extends SimpleTestRec {
|
|||
public d: number;
|
||||
}
|
||||
|
||||
@Model("CustomName")
|
||||
class CustomNameRec extends Record<any> {
|
||||
@Prop()
|
||||
public id: string = "default-id";
|
||||
}
|
||||
|
||||
describe("Record", () => {
|
||||
it("should throw an exeption when record doesn't extends Record class", () => {
|
||||
try {
|
||||
|
@ -176,4 +182,12 @@ describe("Record", () => {
|
|||
expect(SimpleTestRec.isStaticMethod).not.toBeFalsy();
|
||||
expect(SimpleTestRec.isStaticMethod()).toBe(true);
|
||||
});
|
||||
|
||||
it("should allow a custom record name to be set", () => {
|
||||
const rec1 = new TestRec();
|
||||
expect(rec1.constructor.name).toEqual("TestRec");
|
||||
|
||||
const rec2 = new CustomNameRec();
|
||||
expect(rec2.constructor.name).toEqual("CustomName");
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче