Simplify the Trace API further.
[ustaxlib.git] / src / core / Trace.test.ts
1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
5
6 import Form from './Form';
7 import TaxReturn from './TaxReturn';
8 import { ComputedLine, InputLine, ReferenceLine } from './Line';
9 import { Edge, getLastTraceList } from './Trace';
10
11 class TestTaxReturn extends TaxReturn {
12 get year() { return 2019; }
13
14 get includeJointPersonForms() { return false; }
15 };
16
17 interface Input {
18 name: string;
19 value: number;
20 };
21
22 class TestForm extends Form<TestForm['_lines'], Input> {
23 readonly name = 'TF';
24
25 readonly _lines = {
26 'i1': new InputLine<Input>('name'),
27 'i2': new InputLine<Input>('value'),
28 'c1': new ComputedLine((tr): string => {
29 return `Hello ${this.getInput('name')}`;
30 }),
31 'c2': new ComputedLine((tr): number => {
32 return this.getValue(tr, 'i2') * 0.20;
33 }),
34 'r2': new ReferenceLine(TestForm as any, 'c2'),
35 };
36 };
37
38 describe('tracing', () => {
39 const tr = new TestTaxReturn();
40 const f = new TestForm({
41 name: 'ABC',
42 value: 100
43 });
44 tr.addForm(f);
45
46 test('input line', () => {
47 f.getValue(tr, 'i1');
48 const trace = getLastTraceList();
49 expect(trace).toStrictEqual([ [ 'TF-i1 (Input from name)', 'TF input: name' ] ]);
50 });
51
52 test('computed line via input', () => {
53 f.getValue(tr, 'c1');
54 const trace = getLastTraceList();
55 expect(trace).toStrictEqual([ [ 'TF-c1', 'TF input: name' ] ]);
56 });
57
58 test('computed line via input line', () => {
59 f.getValue(tr, 'c2');
60 const trace = getLastTraceList();
61 expect(trace).toStrictEqual([
62 [ 'TF-c2', 'TF-i2 (Input from value)' ],
63 [ 'TF-i2 (Input from value)', 'TF input: value' ]
64 ]);
65 });
66
67 test('reference line', () => {
68 f.getValue(tr, 'r2');
69 const trace = getLastTraceList();
70 expect(trace).toStrictEqual([
71 [ 'TF-r2 (Reference TestForm-c2)', 'TF-c2' ],
72 [ 'TF-c2', 'TF-i2 (Input from value)' ],
73 [ 'TF-i2 (Input from value)', 'TF input: value' ]
74 ]);
75 });
76 });
77