Make Form input strongly typed with generics.
[ustaxlib.git] / src / Form.test.ts
1 import { ComputedLine, Line } from './Line';
2 import TaxReturn from './TaxReturn';
3 import Form from './Form';
4 import { InconsistencyError, NotFoundError } from './Errors';
5
6 test('add and get line', () => {
7 const l = new ComputedLine<number>('1', () => 42);
8
9 class TestForm extends Form {
10 get name(): string {
11 return 'Test Form';
12 }
13
14 protected getLines() {
15 return [l];
16 }
17 };
18
19 const f = new TestForm();
20 expect(f.getLine('1')).toBe(l);
21 });
22
23 test('get non-existent line', () => {
24 class TestForm extends Form {
25 get name(): string {
26 return 'Test Form';
27 }
28
29 protected getLines() {
30 return [];
31 }
32 };
33
34 const f = new TestForm();
35 expect(() => f.getLine('1')).toThrow(NotFoundError);
36 });
37
38 test('add duplicate line', () => {
39 const l1 = new ComputedLine<number>('1', () => 42);
40 const l2 = new ComputedLine<number>('1', () => 36);
41
42 class TestForm extends Form {
43 get name(): string {
44 return 'Test Form';
45 }
46
47 protected getLines() {
48 return [l1, l2];
49 }
50 };
51
52 expect(() => new TestForm()).toThrow(InconsistencyError);
53 });
54
55 test('add line to two forms', () => {
56 const l = new ComputedLine<string>('bad', () => 'bad');
57
58 class TestForm1 extends Form {
59 get name(): string { return '1'; }
60
61 protected getLines() { return [ l ]; }
62 };
63 class TestForm2 extends Form {
64 get name(): string { return '2'; }
65
66 protected getLines() { return [ l ]; }
67 };
68
69 const f1 = new TestForm1();
70 expect(() => new TestForm2()).toThrow(InconsistencyError);
71 });
72
73 test('input', () => {
74 interface TestInput {
75 filingStatus: string;
76 money: number;
77 };
78 class TestForm extends Form<TestInput> {
79 get name() { return '1040'; }
80
81 protected getLines() { return []; }
82 };
83
84 const f = new TestForm({ filingStatus: 'S', money: 100.0 });
85 expect(f.getInput('filingStatus')).toBe('S');
86 });