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')
97 const tr = new TaxReturn(2019);
98 const f = new TestForm({ 'key': 'value' });
101 expect(f.getLine('1').value(tr)).toBe('value');
102 expect(f.getLine('1').id).toBe('1');
104 const l2 = f.getLine('2');
105 expect(() => l2.value(tr)).toThrow(NotFoundError);
108 test('line stack', () => {
109 class FormZ extends Form<FormZ['_lines'], {'input': number}> {
111 protected readonly _lines = {
112 '3': new InputLine<any, any>('input')
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;
125 const tr = new TaxReturn(2019);
126 tr.addForm(new FormZ({ 'input': 100 }));
127 tr.addForm(new FormZ2());
129 const l = new ReferenceLine(FormZ2, '2c');
130 expect(l.value(tr)).toBe(20);
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)
142 const tr = new TaxReturn(2019);
143 tr.addForm(new TestForm());
144 tr.addForm(new TestForm());
145 tr.addForm(new TestForm());
147 const l = new AccumulatorLine(TestForm, 'g');
148 expect(l.value(tr)).toBe(300.75);