Extract constant values from fed2019 Forms as named definitions.
[ustaxlib.git] / src / core / TaxReturn.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 TaxReturn from './TaxReturn';
7 import Person from './Person';
8 import Form from './Form';
9 import { NotFoundError, InconsistencyError } from './Errors';
10
11 class TestTaxReturn extends TaxReturn {
12 get year() { return 2019; }
13
14 readonly constants = {};
15
16 includeJointPersonForms = false;
17 };
18
19 test('does not support Dependents', () => {
20 const tr = new TestTaxReturn();
21 const p = Person.dependent('Baby');
22 expect(() => tr.addPerson(p)).toThrow('Dependents are not supported');
23 });
24
25 test('add more than one Self', () => {
26 const tr = new TestTaxReturn();
27 const p1 = Person.self('A');
28 tr.addPerson(p1);
29 const p2 = Person.self('B');
30 expect(() => tr.addPerson(p2)).toThrow('Cannot have more than one Self or Spouse');
31 });
32
33 test('add more than one Spouse', () => {
34 const tr = new TestTaxReturn();
35 const p1 = Person.spouse('A');
36 tr.addPerson(p1);
37 const p2 = Person.spouse('B');
38 expect(() => tr.addPerson(p2)).toThrow('Cannot have more than one Self or Spouse');
39 });
40
41 test('add Self and Spouse', () => {
42 const tr = new TestTaxReturn();
43 const self = Person.self('Billy Bob');
44 const spouse = Person.spouse('Jilly Bob');
45 tr.addPerson(self);
46 tr.addPerson(spouse);
47
48 expect(tr.getPerson('Billy')).toBe(self);
49 expect(tr.getPerson('Jilly')).toBe(spouse);
50
51 expect(() => tr.getPerson('Bob')).toThrow('too imprecise');
52 });
53
54 test('get non-existent person', () => {
55 const tr = new TestTaxReturn();
56 const self = Person.self('Billy Bob');
57 tr.addPerson(self);
58
59 expect(tr.getPerson('Billy Bob')).toBe(self);
60 expect(() => tr.getPerson('Jilly')).toThrow('not found');
61 });
62
63 test('single-copy forms', () => {
64 class TestForm extends Form {
65 readonly name = 'Test Form';
66 readonly lines = null;
67 };
68
69 const tr = new TestTaxReturn();
70 const f = new TestForm();
71 tr.addForm(f);
72 expect(() => tr.addForm(new TestForm)).toThrow(InconsistencyError);
73 expect(tr.getForm(TestForm)).toBe(f);
74 expect(tr.findForm(TestForm)).toBe(f);
75 });
76
77 test('multiple-copy forms', () => {
78 class TestForm extends Form {
79 readonly name = 'Test Form';
80 readonly supportsMultipleCopies = true;
81 readonly lines = null;
82 };
83
84 const tr = new TestTaxReturn();
85 const f1 = new TestForm();
86 const f2 = new TestForm();
87 const f3 = new TestForm();
88 tr.addForm(f1);
89 tr.addForm(f2);
90
91 expect(() => tr.getForm(TestForm)).toThrow(InconsistencyError);
92 expect(() => tr.findForm(TestForm)).toThrow(InconsistencyError);
93
94 const forms = tr.findForms(TestForm);
95 expect(forms.length).toBe(2);
96 expect(forms).toContain(f1);
97 expect(forms).toContain(f2);
98 expect(forms).not.toContain(f3);
99 });
100
101 test('get non-existent form', () => {
102 class TestForm extends Form {
103 readonly name = 'Test Form';
104 readonly lines = null;
105 }
106 const tr = new TestTaxReturn();
107 expect(() => tr.getForm(TestForm)).toThrow(NotFoundError);
108 expect(tr.findForm(TestForm)).toBeNull();
109 expect(tr.findForms(TestForm)).toEqual([]);
110 });
111
112 class PerPersonForm extends Form {
113 private _person?: Person;
114
115 readonly name = 'Per Person';
116
117 readonly supportsMultipleCopies = true;
118
119 readonly lines = {};
120
121 constructor(person?: Person) {
122 super(undefined);
123 this._person = person;
124 }
125
126 person() { return this._person; }
127 };
128
129 test('find forms for person', () => {
130 const p1 = Person.self('1');
131 const p2 = Person.spouse('2');
132
133 const addFormsToTaxReturn = (tr) => {
134 tr.addForm(new PerPersonForm(undefined));
135 tr.addForm(new PerPersonForm(undefined));
136 tr.addForm(new PerPersonForm(p1));
137 tr.addForm(new PerPersonForm(p2));
138 tr.addForm(new PerPersonForm(p2));
139 tr.addForm(new PerPersonForm(Person.joint));
140 };
141
142 const mfsp1 = new TestTaxReturn();
143 mfsp1.includeJointPersonForms = false;
144 mfsp1.addPerson(p1);
145 addFormsToTaxReturn(mfsp1);
146 expect(mfsp1.findForms(PerPersonForm).length).toBe(3);
147
148 const mfsp2 = new TestTaxReturn();
149 mfsp2.includeJointPersonForms = false;
150 mfsp2.addPerson(p2);
151 addFormsToTaxReturn(mfsp2);
152 expect(mfsp2.findForms(PerPersonForm).length).toBe(4);
153
154 const mfj = new TestTaxReturn();
155 mfj.includeJointPersonForms = true;
156 mfj.addPerson(p1);
157 mfj.addPerson(p2);
158 addFormsToTaxReturn(mfj);
159 expect(mfj.findForms(PerPersonForm).length).toBe(6);
160 });