Introduce InputLine and the ability to acquire inputs from a TaxReturn.
[ustaxlib.git] / src / Line.test.ts
1 import { Line, InputLine, ReferenceLine, ComputedLine } from './Line';
2 import Form 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(id: string, k: T) {
10 super(id, `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>('A',
22 (taxReturn: TaxReturn, line: ComputedLine<number>): number => {
23 expect(taxReturn).toBe(tr);
24 expect(line).toBe(l);
25 return 42;
26 },
27 'Computed Line A');
28 expect(l.value(tr)).toBe(42);
29 expect(l.id).toBe('A');
30 expect(l.description).toBe('Computed Line A');
31 });
32
33 test('reference line', () => {
34 class TestForm extends Form {
35 get name() { return 'Form 1'; }
36
37 protected getLines() {
38 return [ new ConstantLine('6b', 12.34) ];
39 }
40 };
41
42 const tr = new TaxReturn(2019);
43 tr.addForm(new TestForm());
44
45 const l1 = new ReferenceLine<number>('C', 'Form 1', '6b');
46 expect(l1.value(tr)).toBe(12.34);
47
48 const l2 = new ReferenceLine<number>('x', 'Form 2', '6b');
49 expect(() => l2.value(tr)).toThrow(NotFoundError);
50
51 const l3 = new ReferenceLine<number>('y', 'Form 1', '7a');
52 expect(() => l3.value(tr)).toThrow(NotFoundError);
53 });
54
55 test('input line', () => {
56 const tr = new TaxReturn(2019, { 'key': 'value' });
57
58 const l1 = new InputLine<string>('1', 'key');
59 expect(l1.value(tr)).toBe('value');
60
61 const l2 = new InputLine<string>('2', 'key2');
62 expect(() => l2.value(tr)).toThrow(NotFoundError);
63 });
64
65 test('line stack', () => {
66 class FormZ extends Form {
67 get name() { return 'Z'; }
68
69 protected getLines() {
70 return [ new InputLine('3', 'input') ];
71 }
72 };
73
74 class FormZ2 extends Form {
75 get name() { return 'Z-2'; }
76
77 protected getLines() {
78 return [
79 new ComputedLine<number>('2c', (tr: TaxReturn, l: Line<number>): any => {
80 return tr.getForm('Z').getLine('3').value(tr) * 0.2;
81 })
82 ];
83 }
84 };
85
86 const tr = new TaxReturn(2019, { 'input': 100 });
87 tr.addForm(new FormZ());
88 tr.addForm(new FormZ2());
89
90 const l = new ReferenceLine<number>('32', 'Z-2', '2c');
91 expect(l.value(tr)).toBe(20);
92 });