Extract constant values from fed2019 Forms as named definitions.
[ustaxlib.git] / src / core / Form.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 { ComputedLine, Line } from './Line';
7 import TaxReturn from './TaxReturn';
8 import Form, { isFormT } from './Form';
9 import { InconsistencyError, NotFoundError } from './Errors';
10
11 class TestTaxReturn extends TaxReturn {
12 readonly constants = undefined;
13 get year() { return 2019; }
14 get includeJointPersonForms() { return true; }
15 };
16
17 test('add and get line', () => {
18 const l = new ComputedLine<number>(() => 42);
19
20 class TestForm extends Form {
21 readonly name = 'Test Form';
22
23 readonly lines = { '1': l };
24 };
25
26 const f = new TestForm();
27 expect(f.getLine('1')).toBe(l);
28 });
29
30 test('get non-existent line', () => {
31 class TestForm extends Form {
32 readonly name = 'Test';
33 readonly lines = {};
34 };
35
36 const f = new TestForm();
37 const fAsAny: Form = f;
38 expect(() => fAsAny.getLine('line')).toThrow(NotFoundError);
39
40 //TYPEERROR:
41 //expect(() => f.getLine('line')).toThrow(NotFoundError);
42 });
43
44 test('input', () => {
45 interface TestInput {
46 filingStatus: string;
47 money: number;
48 };
49 class TestForm extends Form<TestInput> {
50 readonly name = '1040';
51
52 readonly lines = null;
53 };
54
55 const f = new TestForm({ filingStatus: 'S', money: 100.0 });
56 expect(f.getInput('filingStatus')).toBe('S');
57 });
58
59 test('get value', () => {
60 class TestForm extends Form {
61 readonly name = 'Form';
62
63 readonly lines = {
64 line: new ComputedLine<number>(() => 42),
65 };
66 };
67
68 const f = new TestForm();
69 const tr = new TestTaxReturn();
70 expect(f.getValue(tr, 'line')).toBe(42);
71
72 //TYPEERROR:
73 //let s: string = f.getValue(tr, 'line');
74
75 const fAsAny: Form = f;
76 expect(() => fAsAny.getValue(tr, 'other')).toThrow(NotFoundError);
77 //TYPEERROR:
78 //expect(() => f.getValue(tr, 'other')).toThrow(NotFoundError);
79 });
80
81 test('form types', () => {
82 class FormA extends Form {
83 readonly name = 'A';
84 readonly lines = {};
85 };
86 class FormB extends Form {
87 readonly name = 'B';
88 readonly lines = {};
89 };
90
91 expect(isFormT(new FormA(), FormA)).toBe(true);
92 expect(isFormT(new FormB(), FormA)).toBe(false);
93 expect(isFormT(new FormA(), FormB)).toBe(false);
94 expect(isFormT(new FormB(), FormB)).toBe(true);
95 });
96
97 test('derived form types', () => {
98 class Base extends Form<any> {
99 readonly name = 'Base';
100 readonly lines = {};
101 };
102 class Derived extends Base {};
103 class SecondDerived extends Derived {};
104
105 expect(isFormT(new Base(), Base)).toBe(true);
106 expect(isFormT(new Derived(), Derived)).toBe(true);
107
108 expect(isFormT(new Derived(), Base)).toBe(true);
109 expect(isFormT(new Base(), Derived)).toBe(false);
110
111 expect(isFormT(new SecondDerived(), SecondDerived)).toBe(true);
112 expect(isFormT(new SecondDerived(), Derived)).toBe(true);
113 expect(isFormT(new SecondDerived(), Base)).toBe(true);
114 });