Add license information.
[ustaxlib.git] / src / core / Form.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 { ComputedLine, Line } from './Line';
7 import TaxReturn from './TaxReturn';
8 import Form, { isFormT } from './Form';
9 import { InconsistencyError, NotFoundError } from './Errors';
10
11 class TestTaxReturn extends TaxReturn {
12 get year() { return 2019; }
13 get includeJointPersonForms() { return true; }
14 };
15
16 test('add and get line', () => {
17 const l = new ComputedLine<number>(() => 42);
18
19 class TestForm extends Form<TestForm['_lines']> {
20 readonly name = 'Test Form';
21
22 protected readonly _lines = { '1': l };
23 };
24
25 const f = new TestForm();
26 expect(f.getLine('1')).toBe(l);
27 });
28
29 test('get non-existent line', () => {
30 class TestForm extends Form<TestForm['_lines']> {
31 readonly name = 'Test';
32 protected readonly _lines = {};
33 };
34
35 const f = new TestForm();
36 const fAsAny: Form<any> = f;
37 expect(() => fAsAny.getLine('line')).toThrow(NotFoundError);
38
39 //TYPEERROR:
40 //expect(() => f.getLine('line')).toThrow(NotFoundError);
41 });
42
43 test('input', () => {
44 interface TestInput {
45 filingStatus: string;
46 money: number;
47 };
48 class TestForm extends Form<any, TestInput> {
49 readonly name = '1040';
50
51 protected readonly _lines = null;
52 };
53
54 const f = new TestForm({ filingStatus: 'S', money: 100.0 });
55 expect(f.getInput('filingStatus')).toBe('S');
56 });
57
58 test('get value', () => {
59 class TestForm extends Form<TestForm['_lines']> {
60 readonly name = 'Form';
61
62 protected readonly _lines = {
63 line: new ComputedLine<number>(() => 42),
64 };
65 };
66
67 const f = new TestForm();
68 const tr = new TestTaxReturn();
69 expect(f.getValue(tr, 'line')).toBe(42);
70
71 //TYPEERROR:
72 //let s: string = f.getValue(tr, 'line');
73
74 const fAsAny: Form<any> = f;
75 expect(() => fAsAny.getValue(tr, 'other')).toThrow(NotFoundError);
76 //TYPEERROR:
77 //expect(() => f.getValue(tr, 'other')).toThrow(NotFoundError);
78 });
79
80 test('form types', () => {
81 class FormA extends Form<any> {
82 readonly name = 'A';
83 protected readonly _lines = {};
84 };
85 class FormB extends Form<any> {
86 readonly name = 'B';
87 protected readonly _lines = {};
88 };
89
90 expect(isFormT(new FormA(), FormA)).toBe(true);
91 expect(isFormT(new FormB(), FormA)).toBe(false);
92 expect(isFormT(new FormA(), FormB)).toBe(false);
93 expect(isFormT(new FormB(), FormB)).toBe(true);
94 });