1 import { Line, AccumulatorLine, InputLine, ReferenceLine, ComputedLine } from './Line';
2 import Form from './Form';
3 import TaxReturn from './TaxReturn';
4 import { NotFoundError } from './Errors';
6 class ConstantLine<T> extends Line<T> {
9 constructor(id: string, k: T) {
10 super(id, `Constant ${k}`);
14 value(tr: TaxReturn): T {
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);
28 expect(l.value(tr)).toBe(42);
29 expect(l.id).toBe('A');
30 expect(l.description).toBe('Computed Line A');
33 test('reference line', () => {
34 class TestForm extends Form {
35 get name() { return 'Form 1'; }
37 protected getLines() {
38 return [ new ConstantLine('6b', 12.34) ];
42 const tr = new TaxReturn(2019);
43 tr.addForm(new TestForm());
45 const l1 = new ReferenceLine<number>('C', 'Form 1', '6b');
46 expect(l1.value(tr)).toBe(12.34);
48 const l2 = new ReferenceLine<number>('x', 'Form 2', '6b');
49 expect(() => l2.value(tr)).toThrow(NotFoundError);
51 const l3 = new ReferenceLine<number>('y', 'Form 1', '7a');
52 expect(() => l3.value(tr)).toThrow(NotFoundError);
55 test('input line', () => {
60 class TestForm extends Form {
61 get name() { return 'F1'; }
63 protected getLines() {
65 new InputLine<Input>('1', 'key'),
66 new InputLine<Input>('2', 'key2')
70 const tr = new TaxReturn(2019);
71 const f = new TestForm({ 'key': 'value' });
73 expect(f.getLine('1').value(tr)).toBe('value');
75 const l2 = f.getLine('2');
76 expect(() => l2.value(tr)).toThrow(NotFoundError);
79 test('line stack', () => {
80 class FormZ extends Form {
81 get name() { return 'Z'; }
83 protected getLines() {
84 return [ new InputLine<any, any>('3', 'input') ];
88 class FormZ2 extends Form {
89 get name() { return 'Z-2'; }
91 protected getLines() {
93 new ComputedLine<number>('2c', (tr: TaxReturn, l: Line<number>): any => {
94 return tr.getForm('Z').getLine('3').value(tr) * 0.2;
100 const tr = new TaxReturn(2019);
101 tr.addForm(new FormZ({ 'input': 100 }));
102 tr.addForm(new FormZ2());
104 const l = new ReferenceLine<number>('32', 'Z-2', '2c');
105 expect(l.value(tr)).toBe(20);
108 test('accumulator line', () => {
109 class TestForm extends Form {
110 get name() { return 'Form B'; }
112 readonly supportsMultipleCopies = true;
114 protected getLines() {
115 return [ new ConstantLine<number>('g', 100.25) ]
119 const tr = new TaxReturn(2019);
120 tr.addForm(new TestForm());
121 tr.addForm(new TestForm());
122 tr.addForm(new TestForm());
124 const l = new AccumulatorLine('line', 'Form B', 'g');
125 expect(l.value(tr)).toBe(300.75);