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';
6 class ConstantLine<T> extends Line<T> {
10 super(`Constant ${k}`);
14 value(tr: TaxReturn): T {
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);
27 expect(l.value(tr)).toBe(42);
28 expect(l.description).toBe('Computed Line A');
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'),
40 const tr = new TaxReturn(2019);
41 tr.addForm(new TestForm());
43 const l1 = new ReferenceLine(TestForm, '6b');
44 let n: number = l1.value(tr);
45 expect(n).toBe(12.34);
47 const l2 = new ReferenceLine(TestForm, 's');
48 let s: string = l2.value(tr);
49 expect(s).toBe('abc');
52 //const l3 = new ReferenceLine(TestForm, '7a');
53 //let n2: string = l1.value(tr);
54 //let s2: number = l2.value(tr);
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)
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'),
74 const tr = new TaxReturn(2019);
75 const f = new TestForm();
77 tr.addForm(new OtherForm());
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);
85 test('input line', () => {
90 class TestForm extends Form<TestForm['_lines'], Input> {
92 protected readonly _lines = {
93 '1': new InputLine<Input>('key'),
94 '2': new InputLine<Input>('key2'),
95 '3': new InputLine<Input>('key2', undefined, 'FALLBACK')
98 const tr = new TaxReturn(2019);
99 const f = new TestForm({ 'key': 'value' });
102 expect(f.getLine('1').value(tr)).toBe('value');
103 expect(f.getLine('1').id).toBe('1');
105 const l2 = f.getLine('2');
106 expect(() => l2.value(tr)).toThrow(NotFoundError);
108 expect(f.getLine('3').value(tr)).toBe('FALLBACK');
111 test('line stack', () => {
112 class FormZ extends Form<FormZ['_lines'], {'input': number}> {
114 protected readonly _lines = {
115 '3': new InputLine<any, any>('input')
119 class FormZ2 extends Form<FormZ2['_lines']> {
120 readonly name = 'Z-2';
121 protected readonly _lines = {
122 '2c': new ComputedLine<number>((tr: TaxReturn): any => {
123 return tr.getForm(FormZ).getLine('3').value(tr) * 0.2;
128 const tr = new TaxReturn(2019);
129 tr.addForm(new FormZ({ 'input': 100 }));
130 tr.addForm(new FormZ2());
132 const l = new ReferenceLine(FormZ2, '2c');
133 expect(l.value(tr)).toBe(20);
136 test('accumulator line', () => {
137 class TestForm extends Form<TestForm['_lines']> {
138 readonly name = 'Form B';
139 readonly supportsMultipleCopies = true;
140 protected readonly _lines = {
141 g: new ConstantLine<number>(100.25)
145 const tr = new TaxReturn(2019);
146 tr.addForm(new TestForm());
147 tr.addForm(new TestForm());
148 tr.addForm(new TestForm());
150 const l = new AccumulatorLine(TestForm, 'g');
151 expect(l.value(tr)).toBe(300.75);