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