Make core/TaxReturn an abstract class.
[ustaxlib.git] / src / core / Form.test.ts
1 import { ComputedLine, Line } from './Line';
2 import TaxReturn from './TaxReturn';
3 import Form, { isFormT } from './Form';
4 import { InconsistencyError, NotFoundError } from './Errors';
5
6 class TestTaxReturn extends TaxReturn {
7 get year() { return 2019; }
8 get includeJointPersonForms() { return true; }
9 };
10
11 test('add and get line', () => {
12 const l = new ComputedLine<number>(() => 42);
13
14 class TestForm extends Form<TestForm['_lines']> {
15 readonly name = 'Test Form';
16
17 protected readonly _lines = { '1': l };
18 };
19
20 const f = new TestForm();
21 expect(f.getLine('1')).toBe(l);
22 });
23
24 test('get non-existent line', () => {
25 class TestForm extends Form<TestForm['_lines']> {
26 readonly name = 'Test';
27 protected readonly _lines = {};
28 };
29
30 const f = new TestForm();
31 const fAsAny: Form<any> = f;
32 expect(() => fAsAny.getLine('line')).toThrow(NotFoundError);
33
34 //TYPEERROR:
35 //expect(() => f.getLine('line')).toThrow(NotFoundError);
36 });
37
38 test('input', () => {
39 interface TestInput {
40 filingStatus: string;
41 money: number;
42 };
43 class TestForm extends Form<any, TestInput> {
44 readonly name = '1040';
45
46 protected readonly _lines = null;
47 };
48
49 const f = new TestForm({ filingStatus: 'S', money: 100.0 });
50 expect(f.getInput('filingStatus')).toBe('S');
51 });
52
53 test('get value', () => {
54 class TestForm extends Form<TestForm['_lines']> {
55 readonly name = 'Form';
56
57 protected readonly _lines = {
58 line: new ComputedLine<number>(() => 42),
59 };
60 };
61
62 const f = new TestForm();
63 const tr = new TestTaxReturn();
64 expect(f.getValue(tr, 'line')).toBe(42);
65
66 //TYPEERROR:
67 //let s: string = f.getValue(tr, 'line');
68
69 const fAsAny: Form<any> = f;
70 expect(() => fAsAny.getValue(tr, 'other')).toThrow(NotFoundError);
71 //TYPEERROR:
72 //expect(() => f.getValue(tr, 'other')).toThrow(NotFoundError);
73 });
74
75 test('form types', () => {
76 class FormA extends Form<any> {
77 readonly name = 'A';
78 protected readonly _lines = {};
79 };
80 class FormB extends Form<any> {
81 readonly name = 'B';
82 protected readonly _lines = {};
83 };
84
85 expect(isFormT(new FormA(), FormA)).toBe(true);
86 expect(isFormT(new FormB(), FormA)).toBe(false);
87 expect(isFormT(new FormA(), FormB)).toBe(false);
88 expect(isFormT(new FormB(), FormB)).toBe(true);
89 });