expect(tr.findForm(TestForm)).toBeNull();
expect(tr.findForms(TestForm)).toEqual([]);
});
-
-type FormClass<T extends Form<any>> = Function & { prototype: T };
-
-class TR {
- private _forms: Form<any>[] = [];
-
- add(form: Form<any>) {
- this._forms.push(form);
- }
-
- find(name: string): Form<any> {
- const forms = this._forms.filter(f => f.name == name);
- if (forms.length > 0)
- return forms[0];
- return null;
- }
-
- find2<T extends Form<any>>(cls: FormClass<T>): T[] {
- let forms: T[] = [];
- const isT = (form: Form<any>): form is T => form.constructor === cls;
- for (let form of this._forms) {
- if (isT(form))
- forms.push(form);
- }
- return forms;
- }
-};
-
-test('type test', () => {
- class FormA extends Form<FormA['_lines']> {
- readonly name = 'Form A';
- protected readonly _lines = {};
- };
- class FormB extends Form<FormB['_lines']> {
- readonly name = 'Form B';
- readonly supportsMultipleCopies = true;
- protected readonly _lines = {};
- };
-
- const tr = new TR();
-
- tr.add(new FormA());
- tr.add(new FormB());
-
- expect(tr.find('Form A')).not.toBeNull();
-
- expect(tr.find2(FormB).length).toBe(1);
-
- tr.add(new FormB());
- expect(tr.find2(FormB).length).toBe(2);
-
-});