1 import TaxReturn from './TaxReturn';
2 import Person from './Person';
3 import Form from './Form';
4 import { NotFoundError, InconsistencyError } from './Errors';
6 test('constructor', () => {
7 const tr = new TaxReturn(2019);
8 expect(tr.year).toBe(2019);
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');
17 test('add more than one Self', () => {
18 const tr = new TaxReturn(2019);
19 const p1 = Person.self('A');
21 const p2 = Person.self('B');
22 expect(() => tr.addPerson(p2)).toThrow('Cannot have more than one Self or Spouse');
25 test('add more than one Spouse', () => {
26 const tr = new TaxReturn(2019);
27 const p1 = Person.spouse('A');
29 const p2 = Person.spouse('B');
30 expect(() => tr.addPerson(p2)).toThrow('Cannot have more than one Self or Spouse');
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');
40 expect(tr.getPerson('Billy')).toBe(self);
41 expect(tr.getPerson('Jilly')).toBe(spouse);
43 expect(() => tr.getPerson('Bob')).toThrow('too imprecise');
46 test('get non-existent person', () => {
47 const tr = new TaxReturn(2019);
48 const self = Person.self('Billy Bob');
51 expect(tr.getPerson('Billy Bob')).toBe(self);
52 expect(() => tr.getPerson('Jilly')).toThrow('not found');
55 test('single-copy forms', () => {
56 class TestForm extends Form<null> {
57 readonly name = 'Test Form';
58 protected readonly _lines = null;
61 const tr = new TaxReturn(2019);
62 const f = new TestForm();
64 expect(() => tr.addForm(new TestForm)).toThrow(InconsistencyError);
65 expect(tr.getForm(TestForm)).toBe(f);
66 expect(tr.findForm(TestForm)).toBe(f);
69 test('multiple-copy forms', () => {
70 class TestForm extends Form<null> {
71 readonly name = 'Test Form';
72 readonly supportsMultipleCopies = true;
73 protected readonly _lines = null;
76 const tr = new TaxReturn(2019);
77 const f1 = new TestForm();
78 const f2 = new TestForm();
79 const f3 = new TestForm();
83 expect(() => tr.getForm(TestForm)).toThrow(InconsistencyError);
84 expect(() => tr.findForm(TestForm)).toThrow(InconsistencyError);
86 const forms = tr.findForms(TestForm);
87 expect(forms.length).toBe(2);
88 expect(forms).toContain(f1);
89 expect(forms).toContain(f2);
90 expect(forms).not.toContain(f3);
93 test('get non-existent form', () => {
94 class TestForm extends Form<null> {
95 readonly name = 'Test Form';
96 protected readonly _lines = null;
98 const tr = new TaxReturn(2019);
99 expect(() => tr.getForm(TestForm)).toThrow(NotFoundError);
100 expect(tr.findForm(TestForm)).toBeNull();
101 expect(tr.findForms(TestForm)).toEqual([]);
104 type FormClass<T extends Form<any>> = Function & { prototype: T };
107 private _forms: Form<any>[] = [];
109 add(form: Form<any>) {
110 this._forms.push(form);
113 find(name: string): Form<any> {
114 const forms = this._forms.filter(f => f.name == name);
115 if (forms.length > 0)
120 find2<T extends Form<any>>(cls: FormClass<T>): T[] {
122 const isT = (form: Form<any>): form is T => form.constructor === cls;
123 for (let form of this._forms) {
131 test('type test', () => {
132 class FormA extends Form<FormA['_lines']> {
133 readonly name = 'Form A';
134 protected readonly _lines = {};
136 class FormB extends Form<FormB['_lines']> {
137 readonly name = 'Form B';
138 readonly supportsMultipleCopies = true;
139 protected readonly _lines = {};
147 expect(tr.find('Form A')).not.toBeNull();
149 expect(tr.find2(FormB).length).toBe(1);
152 expect(tr.find2(FormB).length).toBe(2);