UNPKG

5.03 kBJavaScriptView Raw
1/**
2 * Created by Andy Likuski on 2017.07.03
3 * Copyright (c) 2017 Andy Likuski
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 *
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 */
11
12import {reqPath} from './functions';
13import {
14 throwIfResultError,
15 mappedThrowIfResultError,
16 reqPathThrowing,
17 reqStrPathThrowing,
18 reqPathPropEqThrowing,
19 findOneThrowing,
20 onlyOneThrowing,
21 onlyOneValueThrowing,
22 findOneValueByParamsThrowing
23} from './throwingFunctions';
24import * as Result from 'folktale/result';
25import * as R from 'ramda';
26
27describe('throwingFunctions', () => {
28 test('throwIfResultError', () => {
29 // Use a pure function that returns Result. throwIfResultError should throw if the either is an ResultError
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 // Use a pure function that returns Result. throwIfResultError should throw if the either is an ResultError
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 // Works with objects
74 expect(
75 findOneThrowing(R.equals('Eli Whitney'), {a: 1, b: 'Eli Whitney'})
76 ).toEqual(
77 {b: 'Eli Whitney'}
78 );
79
80 // Works with arrays
81 expect(
82 findOneThrowing(R.equals('Eli Whitney'), [1, 'Eli Whitney'])
83 ).toEqual(
84 ['Eli Whitney']
85 );
86
87 // None
88 expect(
89 () => findOneThrowing(R.equals('Eli Whitney'), {a: 1, b: 2})
90 ).toThrow();
91
92 // Too many
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 // None
105 expect(
106 () => onlyOneThrowing({})
107 ).toThrow();
108
109 // Too many
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 // None
122 expect(
123 () => onlyOneValueThrowing({})
124 ).toThrow();
125
126 // Too many
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