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 TestTaxReturn extends TaxReturn {
7 get year() { return 2019; }
8 get includeJointPersonForms() { return false; }
11 class ConstantLine<T> extends Line<T> {
15 super(`Constant ${k}`);
19 value(tr: TaxReturn): T {
24 test('computed line', () => {
25 const tr = new TestTaxReturn();
26 const l = new ComputedLine<number>(
27 (taxReturn: TaxReturn): number => {
28 expect(taxReturn).toBe(tr);
32 expect(l.value(tr)).toBe(42);
33 expect(l.description).toBe('Computed Line A');
36 test('reference line', () => {
37 class TestForm extends Form<TestForm['_lines']> {
38 readonly name = 'Form 1';
39 protected readonly _lines = {
40 '6b': new ConstantLine(12.34),
41 's': new ConstantLine('abc'),
45 const tr = new TestTaxReturn();
46 tr.addForm(new TestForm());
48 const l1 = new ReferenceLine(TestForm, '6b');
49 let n: number = l1.value(tr);
50 expect(n).toBe(12.34);
52 const l2 = new ReferenceLine(TestForm, 's');
53 let s: string = l2.value(tr);
54 expect(s).toBe('abc');
57 //const l3 = new ReferenceLine(TestForm, '7a');
58 //let n2: string = l1.value(tr);
59 //let s2: number = l2.value(tr);
62 test('self reference line', () => {
63 class OtherForm extends Form<OtherForm['_lines']> {
64 readonly name = 'Form A';
65 protected readonly _lines = {
66 '6c': new ConstantLine(55)
69 class TestForm extends Form<TestForm['_lines']> {
70 readonly name = 'Form 1';
71 protected readonly _lines = {
72 'a': new ConstantLine(100.2),
73 'b': new ReferenceLine(OtherForm, '6c'),
74 'c': new ReferenceLine((TestForm as unknown) as FormClass<Form<any>>, 'b'),
75 'd': new ReferenceLine(TestForm as any, 'b'),
79 const tr = new TestTaxReturn();
80 const f = new TestForm();
82 tr.addForm(new OtherForm());
84 expect(f.getValue(tr, 'a')).toBe(100.2);
85 expect(f.getValue(tr, 'b')).toBe(55);
86 expect(f.getValue(tr, 'c')).toBe(55);
87 expect(f.getValue(tr, 'd')).toBe(55);
90 test('input line', () => {
95 class TestForm extends Form<TestForm['_lines'], Input> {
97 protected readonly _lines = {
98 '1': new InputLine<Input>('key'),
99 '2': new InputLine<Input>('key2'),
100 '3': new InputLine<Input>('key2', undefined, 'FALLBACK')
103 const tr = new TestTaxReturn();
104 const f = new TestForm({ 'key': 'value' });
107 expect(f.getLine('1').value(tr)).toBe('value');
108 expect(f.getLine('1').id).toBe('1');
110 const l2 = f.getLine('2');
111 expect(() => l2.value(tr)).toThrow(NotFoundError);
113 expect(f.getLine('3').value(tr)).toBe('FALLBACK');
116 test('line stack', () => {
117 class FormZ extends Form<FormZ['_lines'], {'input': number}> {
119 protected readonly _lines = {
120 '3': new InputLine<any, any>('input')
124 class FormZ2 extends Form<FormZ2['_lines']> {
125 readonly name = 'Z-2';
126 protected readonly _lines = {
127 '2c': new ComputedLine<number>((tr: TaxReturn): any => {
128 return tr.getForm(FormZ).getLine('3').value(tr) * 0.2;
133 const tr = new TestTaxReturn();
134 tr.addForm(new FormZ({ 'input': 100 }));
135 tr.addForm(new FormZ2());
137 const l = new ReferenceLine(FormZ2, '2c');
138 expect(l.value(tr)).toBe(20);
141 test('accumulator line', () => {
142 class TestForm extends Form<TestForm['_lines']> {
143 readonly name = 'Form B';
144 readonly supportsMultipleCopies = true;
145 protected readonly _lines = {
146 g: new ConstantLine<number>(100.25)
150 const tr = new TestTaxReturn();
151 tr.addForm(new TestForm());
152 tr.addForm(new TestForm());
153 tr.addForm(new TestForm());
155 const l = new AccumulatorLine(TestForm, 'g');
156 expect(l.value(tr)).toBe(300.75);