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