Simplify uses of ComputedLine to not type the |tr| argument.
[ustaxlib.git] / src / Line.test.ts
1 import { Line, AccumulatorLine, InputLine, ReferenceLine, ComputedLine } from './Line';
2 import Form, { FormClass } from './Form';
3 import TaxReturn from './TaxReturn';
4 import { NotFoundError } from './Errors';
5
6 class ConstantLine<T> extends Line<T> {
7 private _k: T;
8
9 constructor(k: T) {
10 super(`Constant ${k}`);
11 this._k = k;
12 }
13
14 value(tr: TaxReturn): T {
15 return this._k;
16 }
17 };
18
19 test('computed line', () => {
20 const tr = new TaxReturn(2019);
21 const l = new ComputedLine<number>(
22 (taxReturn: TaxReturn): number => {
23 expect(taxReturn).toBe(tr);
24 return 42;
25 },
26 'Computed Line A');
27 expect(l.value(tr)).toBe(42);
28 expect(l.description).toBe('Computed Line A');
29 });
30
31 test('reference line', () => {
32 class TestForm extends Form<TestForm['_lines']> {
33 readonly name = 'Form 1';
34 protected readonly _lines = {
35 '6b': new ConstantLine(12.34),
36 's': new ConstantLine('abc'),
37 };
38 };
39
40 const tr = new TaxReturn(2019);
41 tr.addForm(new TestForm());
42
43 const l1 = new ReferenceLine(TestForm, '6b');
44 let n: number = l1.value(tr);
45 expect(n).toBe(12.34);
46
47 const l2 = new ReferenceLine(TestForm, 's');
48 let s: string = l2.value(tr);
49 expect(s).toBe('abc');
50
51 //TYPEERROR:
52 //const l3 = new ReferenceLine(TestForm, '7a');
53 //let n2: string = l1.value(tr);
54 //let s2: number = l2.value(tr);
55 });
56
57 test('self reference line', () => {
58 class OtherForm extends Form<OtherForm['_lines']> {
59 readonly name = 'Form A';
60 protected readonly _lines = {
61 '6c': new ConstantLine(55)
62 };
63 };
64 class TestForm extends Form<TestForm['_lines']> {
65 readonly name = 'Form 1';
66 protected readonly _lines = {
67 'a': new ConstantLine(100.2),
68 'b': new ReferenceLine(OtherForm, '6c'),
69 'c': new ReferenceLine((TestForm as unknown) as FormClass<Form<any>>, 'b'),
70 'd': new ReferenceLine(TestForm as any, 'b'),
71 };
72 };
73
74 const tr = new TaxReturn(2019);
75 const f = new TestForm();
76 tr.addForm(f);
77 tr.addForm(new OtherForm());
78
79 expect(f.getValue(tr, 'a')).toBe(100.2);
80 expect(f.getValue(tr, 'b')).toBe(55);
81 expect(f.getValue(tr, 'c')).toBe(55);
82 expect(f.getValue(tr, 'd')).toBe(55);
83 });
84
85 test('input line', () => {
86 interface Input {
87 key: string;
88 key2?: string;
89 }
90 class TestForm extends Form<TestForm['_lines'], Input> {
91 readonly name = 'F1';
92 protected readonly _lines = {
93 '1': new InputLine<Input>('key'),
94 '2': new InputLine<Input>('key2')
95 };
96 };
97 const tr = new TaxReturn(2019);
98 const f = new TestForm({ 'key': 'value' });
99 tr.addForm(f);
100
101 expect(f.getLine('1').value(tr)).toBe('value');
102 expect(f.getLine('1').id).toBe('1');
103
104 const l2 = f.getLine('2');
105 expect(() => l2.value(tr)).toThrow(NotFoundError);
106 });
107
108 test('line stack', () => {
109 class FormZ extends Form<FormZ['_lines'], {'input': number}> {
110 readonly name = 'Z';
111 protected readonly _lines = {
112 '3': new InputLine<any, any>('input')
113 }
114 };
115
116 class FormZ2 extends Form<FormZ2['_lines']> {
117 readonly name = 'Z-2';
118 protected readonly _lines = {
119 '2c': new ComputedLine<number>((tr: TaxReturn): any => {
120 return tr.getForm(FormZ).getLine('3').value(tr) * 0.2;
121 })
122 };
123 };
124
125 const tr = new TaxReturn(2019);
126 tr.addForm(new FormZ({ 'input': 100 }));
127 tr.addForm(new FormZ2());
128
129 const l = new ReferenceLine(FormZ2, '2c');
130 expect(l.value(tr)).toBe(20);
131 });
132
133 test('accumulator line', () => {
134 class TestForm extends Form<TestForm['_lines']> {
135 readonly name = 'Form B';
136 readonly supportsMultipleCopies = true;
137 protected readonly _lines = {
138 g: new ConstantLine<number>(100.25)
139 };
140 };
141
142 const tr = new TaxReturn(2019);
143 tr.addForm(new TestForm());
144 tr.addForm(new TestForm());
145 tr.addForm(new TestForm());
146
147 const l = new AccumulatorLine(TestForm, 'g');
148 expect(l.value(tr)).toBe(300.75);
149 });