1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import {reqPath} from './functions';
|
13 | import {
|
14 | throwIfResultError,
|
15 | mappedThrowIfResultError,
|
16 | reqPathThrowing,
|
17 | reqStrPathThrowing,
|
18 | reqPathPropEqThrowing,
|
19 | findOneThrowing,
|
20 | onlyOneThrowing,
|
21 | onlyOneValueThrowing,
|
22 | findOneValueByParamsThrowing
|
23 | } from './throwingFunctions';
|
24 | import * as Result from 'folktale/result';
|
25 | import * as R from 'ramda';
|
26 |
|
27 | describe('throwingFunctions', () => {
|
28 | test('throwIfResultError', () => {
|
29 |
|
30 | expect(throwIfResultError(reqPath(['a'], {a: 1}))).toBe(1);
|
31 | expect(() => throwIfResultError(reqPath(['a', 'b'], {a: {c: 1}}))).toThrow();
|
32 | });
|
33 |
|
34 | test('mappedThrowIfResultError', () => {
|
35 |
|
36 | expect(mappedThrowIfResultError(() => null, reqPath(['a'], {a: 1})).unsafeGet()).toBe(1);
|
37 | expect(() => mappedThrowIfResultError(arg => `Error ${arg}`, Result.Error([1, 2]))).toThrow(
|
38 | 'Error 1; Error 2'
|
39 | );
|
40 | });
|
41 |
|
42 | test('reqPathThrowing', () => {
|
43 | expect(reqPathThrowing(['a'], {a: 1})).toBe(1);
|
44 | expect(() => reqPathThrowing(['a', 'b'], {a: {c: 1}})).toThrow('Only found non-nil path up to a of path a.b for obj { a: { c: 1 } }');
|
45 | expect(() => reqPathThrowing(['apple', 'b'], {a: {c: 1}})).toThrow('Found no non-nil value of path apple.b for obj { a: { c: 1 } }');
|
46 | });
|
47 |
|
48 | test('reqStrPathThrowing', () => {
|
49 | expect(reqStrPathThrowing('foo.bar.myboo.1.gone', {
|
50 | foo: {
|
51 | bar: {
|
52 | goo: 1,
|
53 | myboo: ['is', {gone: 'forever'}]
|
54 | }
|
55 | }
|
56 | })).toEqual('forever');
|
57 |
|
58 | expect(() => reqStrPathThrowing('foo.bar.goo', {
|
59 | foo: {
|
60 | car: {
|
61 | goo: 1
|
62 | }
|
63 | }
|
64 | })).toThrow();
|
65 | });
|
66 |
|
67 | test('reqPathPropEqThrowing', () => {
|
68 | expect(reqPathPropEqThrowing(['a'], 1, {a: 1})).toBe(true);
|
69 | expect(() => reqPathPropEqThrowing(['a', 'b'], 1, {a: {c: 1}})).toThrow();
|
70 | });
|
71 |
|
72 | test('findOneThrowing', () => {
|
73 |
|
74 | expect(
|
75 | findOneThrowing(R.equals('Eli Whitney'), {a: 1, b: 'Eli Whitney'})
|
76 | ).toEqual(
|
77 | {b: 'Eli Whitney'}
|
78 | );
|
79 |
|
80 |
|
81 | expect(
|
82 | findOneThrowing(R.equals('Eli Whitney'), [1, 'Eli Whitney'])
|
83 | ).toEqual(
|
84 | ['Eli Whitney']
|
85 | );
|
86 |
|
87 |
|
88 | expect(
|
89 | () => findOneThrowing(R.equals('Eli Whitney'), {a: 1, b: 2})
|
90 | ).toThrow();
|
91 |
|
92 |
|
93 | expect(
|
94 | () => findOneThrowing(R.equals('Eli Whitney'), {a: 'Eli Whitney', b: 'Eli Whitney'})
|
95 | ).toThrow();
|
96 | });
|
97 |
|
98 | test('onlyOneThrowing', () => {
|
99 | expect(
|
100 | onlyOneThrowing({a: 'Eli Whitney'})).toEqual(
|
101 | {a: 'Eli Whitney'}
|
102 | );
|
103 |
|
104 |
|
105 | expect(
|
106 | () => onlyOneThrowing({})
|
107 | ).toThrow();
|
108 |
|
109 |
|
110 | expect(
|
111 | () => onlyOneThrowing({a: 'Eli Whitney', b: 'Eli Whitney'})
|
112 | ).toThrow();
|
113 | });
|
114 |
|
115 | test('onlyOneValueThrowing', () => {
|
116 | expect(
|
117 | onlyOneValueThrowing({a: 'Eli Whitney'})).toEqual(
|
118 | 'Eli Whitney'
|
119 | );
|
120 |
|
121 |
|
122 | expect(
|
123 | () => onlyOneValueThrowing({})
|
124 | ).toThrow();
|
125 |
|
126 |
|
127 | expect(
|
128 | () => onlyOneValueThrowing({a: 'Eli Whitney', b: 'Eli Whitney'})
|
129 | ).toThrow();
|
130 | });
|
131 |
|
132 | test('findOneValueByParamsThrowing', () => {
|
133 | const items = [
|
134 | {brand: 'crush', flavor: 'grape'},
|
135 | {brand: 'fanta', flavor: 'strawberry'},
|
136 | {brand: 'crush', flavor: 'orange'}
|
137 | ];
|
138 | const params = {brand: 'crush', flavor: 'orange'};
|
139 | expect(findOneValueByParamsThrowing(params, items)).toEqual(
|
140 | {brand: 'crush', flavor: 'orange'}
|
141 | );
|
142 | const badParams = {brand: 'crush', flavor: 'pretzel'};
|
143 | expect(() => findOneValueByParamsThrowing(badParams, items)).toThrow();
|
144 | const tooGoodParams = {brand: 'crush'};
|
145 | expect(() => findOneValueByParamsThrowing(tooGoodParams, items)).toThrow();
|
146 | });
|
147 | });
|
148 |
|