Strongly type Form lines.
[ustaxlib.git] / src / 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 test('constructor', () => {
7 const tr = new TaxReturn(2019);
8 expect(tr.year).toBe(2019);
9 });
10
11 test('does not support Dependents', () => {
12 const tr = new TaxReturn(2019);
13 const p = Person.dependent('Baby');
14 expect(() => tr.addPerson(p)).toThrow('Dependents are not supported');
15 });
16
17 test('add more than one Self', () => {
18 const tr = new TaxReturn(2019);
19 const p1 = Person.self('A');
20 tr.addPerson(p1);
21 const p2 = Person.self('B');
22 expect(() => tr.addPerson(p2)).toThrow('Cannot have more than one Self or Spouse');
23 });
24
25 test('add more than one Spouse', () => {
26 const tr = new TaxReturn(2019);
27 const p1 = Person.spouse('A');
28 tr.addPerson(p1);
29 const p2 = Person.spouse('B');
30 expect(() => tr.addPerson(p2)).toThrow('Cannot have more than one Self or Spouse');
31 });
32
33 test('add Self and Spouse', () => {
34 const tr = new TaxReturn(2019);
35 const self = Person.self('Billy Bob');
36 const spouse = Person.spouse('Jilly Bob');
37 tr.addPerson(self);
38 tr.addPerson(spouse);
39
40 expect(tr.getPerson('Billy')).toBe(self);
41 expect(tr.getPerson('Jilly')).toBe(spouse);
42
43 expect(() => tr.getPerson('Bob')).toThrow('too imprecise');
44 });
45
46 test('get non-existent person', () => {
47 const tr = new TaxReturn(2019);
48 const self = Person.self('Billy Bob');
49 tr.addPerson(self);
50
51 expect(tr.getPerson('Billy Bob')).toBe(self);
52 expect(() => tr.getPerson('Jilly')).toThrow('not found');
53 });
54
55 test('single-copy forms', () => {
56 class TestForm extends Form<null> {
57 readonly name = 'Test Form';
58 protected readonly _lines = null;
59 };
60
61 const tr = new TaxReturn(2019);
62 const f = new TestForm();
63 tr.addForm(f);
64 expect(() => tr.addForm(new TestForm)).toThrow(InconsistencyError);
65 expect(tr.getForm(f.name)).toBe(f);
66 });
67
68 test('multiple-copy forms', () => {
69 class TestForm extends Form<null> {
70 readonly name = 'Test Form';
71 readonly supportsMultipleCopies = true;
72 protected readonly _lines = null;
73 };
74
75 const tr = new TaxReturn(2019);
76 const f1 = new TestForm();
77 const f2 = new TestForm();
78 const f3 = new TestForm();
79 tr.addForm(f1);
80 tr.addForm(f2);
81
82 expect(() => tr.getForm(f1.name)).toThrow(InconsistencyError);
83
84 const forms = tr.getForms(f1.name);
85 expect(forms.length).toBe(2);
86 expect(forms).toContain(f1);
87 expect(forms).toContain(f2);
88 expect(forms).not.toContain(f3);
89 });
90
91 test('get non-existent form', () => {
92 const tr = new TaxReturn(2019);
93 expect(() => tr.getForm('form')).toThrow(NotFoundError);
94 expect(tr.getForms('form')).toEqual([]);
95 });