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, line: ComputedLine<number>): number => {
23 expect(taxReturn).toBe(tr);
28 expect(l.value(tr)).toBe(42);
29 expect(l.description).toBe('Computed Line A');
32 test('reference line', () => {
33 class TestForm extends Form<TestForm['_lines']> {
34 readonly name = 'Form 1';
35 protected readonly _lines = {
36 '6b': new ConstantLine(12.34),
37 's': new ConstantLine('abc'),
41 const tr = new TaxReturn(2019);
42 tr.addForm(new TestForm());
44 const l1 = new ReferenceLine(TestForm, '6b');
45 let n: number = l1.value(tr);
46 expect(n).toBe(12.34);
48 const l2 = new ReferenceLine(TestForm, 's');
49 let s: string = l2.value(tr);
50 expect(s).toBe('abc');
53 //const l3 = new ReferenceLine(TestForm, '7a');
54 //let n2: string = l1.value(tr);
55 //let s2: number = l2.value(tr);
58 test('self reference line', () => {
59 class OtherForm extends Form<OtherForm['_lines']> {
60 readonly name = 'Form A';
61 protected readonly _lines = {
62 '6c': new ConstantLine(55)
65 class TestForm extends Form<TestForm['_lines']> {
66 readonly name = 'Form 1';
67 protected readonly _lines = {
68 'a': new ConstantLine(100.2),
69 'b': new ReferenceLine(OtherForm, '6c'),
70 'c': new ReferenceLine((TestForm as unknown) as FormClass<Form<any>>, 'b'),
71 'd': new ReferenceLine(TestForm as any, 'b'),
75 const tr = new TaxReturn(2019);
76 const f = new TestForm();
78 tr.addForm(new OtherForm());
80 expect(f.getValue(tr, 'a')).toBe(100.2);
81 expect(f.getValue(tr, 'b')).toBe(55);
82 expect(f.getValue(tr, 'c')).toBe(55);
83 expect(f.getValue(tr, 'd')).toBe(55);
86 test('input line', () => {
91 class TestForm extends Form<TestForm['_lines'], Input> {
93 protected readonly _lines = {
94 '1': new InputLine<Input>('key'),
95 '2': new InputLine<Input>('key2')
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);
109 test('line stack', () => {
110 class FormZ extends Form<FormZ['_lines'], {'input': number}> {
112 protected readonly _lines = {
113 '3': new InputLine<any, any>('input')
117 class FormZ2 extends Form<FormZ2['_lines']> {
118 readonly name = 'Z-2';
119 protected readonly _lines = {
120 '2c': new ComputedLine<number>((tr: TaxReturn, l: Line<number>): any => {
121 return tr.getForm(FormZ).getLine('3').value(tr) * 0.2;
126 const tr = new TaxReturn(2019);
127 tr.addForm(new FormZ({ 'input': 100 }));
128 tr.addForm(new FormZ2());
130 const l = new ReferenceLine(FormZ2, '2c');
131 expect(l.value(tr)).toBe(20);
134 test('accumulator line', () => {
135 class TestForm extends Form<TestForm['_lines']> {
136 readonly name = 'Form B';
137 readonly supportsMultipleCopies = true;
138 protected readonly _lines = {
139 g: new ConstantLine<number>(100.25)
143 const tr = new TaxReturn(2019);
144 tr.addForm(new TestForm());
145 tr.addForm(new TestForm());
146 tr.addForm(new TestForm());
148 const l = new AccumulatorLine(TestForm, 'g');
149 expect(l.value(tr)).toBe(300.75);