UNPKG

4.29 kBtext/coffeescriptView Raw
1Promise = require('promise')
2_ = require('../lib/util.coffee')
3
4ValidationError = require('../lib/errors/validation_error.coffee')
5
6describe "Util", ->
7 lazy "validator", -> test: ->
8
9 describe "#defaults", ->
10 it "extends an object from the left to right", ->
11 incomeOptions =
12 a: 1
13 b: 2
14
15 options = _.defaults incomeOptions,
16 b: 3
17 c: 4
18
19 expect(options).eql {a: 1, b: 2, c: 4}
20
21 describe "#isString", ->
22 it "correctly detects strings", ->
23 expect(_.isString(null)).false
24 expect(_.isString(false)).false
25 expect(_.isString({})).false
26 expect(_.isString([])).false
27 expect(_.isString(undefined)).false
28 expect(_.isString('')).true
29 expect(_.isString(' ')).true
30
31 describe "#isFunction", ->
32 it "checks if a value is a function", ->
33 expect(_.isFunction(null)).false
34 expect(_.isFunction(true)).false
35 expect(_.isFunction(false)).false
36 expect(_.isFunction([])).false
37 expect(_.isFunction([1])).false
38 expect(_.isFunction({})).false
39
40 expect(_.isFunction(->)).true
41
42 describe "#isValidator", ->
43 it "checks if the object is a validator", ->
44 expect(_.isValidator({test: ->})).true
45 expect(_.isValidator({test: null})).false
46 expect(_.isValidator({})).false
47 expect(_.isValidator(null)).false
48
49 describe "#isArray", ->
50 it "checks if value is a list", ->
51 expect(_.isArray(undefined)).false
52 expect(_.isArray(null)).false
53 expect(_.isArray(false)).false
54 expect(_.isArray(true)).false
55 expect(_.isArray({})).false
56 expect(_.isArray("string")).false
57 expect(_.isArray(arguments)).false
58 expect(_.isArray([])).true
59
60 describe "#guardValidator", ->
61 it "raises an error if the given argument is not validator", ->
62 expect(-> _.guardValidator(null)).throw('null is not a valid validator')
63
64 it "does nothing when it's valid", (validator) ->
65 expect(-> _.guardValidator(validator)).not.throw()
66
67 describe "#guardValidationError", ->
68 it "throws the error if it's not a validation error", ->
69 expect(-> _.guardValidationError(new Error('boom'))).throw('boom')
70
71 it "doesn't throw an error if it's a ValidationError", ->
72 expect(-> _.guardValidationError(new ValidationError())).not.throw()
73
74 it "doesn't throw an error if it's a ValidationError descendent", ->
75 class CustomValidationError extends ValidationError
76
77 expect(-> _.guardValidationError(new CustomValidationError())).not.throw()
78
79 describe "#contains", ->
80 it "test if an element is present on a list", ->
81 expect(_.contains([], 1)).false
82 expect(_.contains([1], 1)).true
83 expect(_.contains([0, 1, 2], 0)).true
84 expect(_.contains([0, 1, 2], 1)).true
85 expect(_.contains([0, 1, 2], 2)).trueQ
86 expect(_.contains([0, 1, 2], 3)).false
87
88 describe "#map", ->
89 it "maps a list into another", ->
90 expect(_.map([1, 2, 3], (x) -> x * 2)).eql [2, 4, 6]
91
92 describe "#reduce", ->
93 it "reduces the list into a value", ->
94 expect(_.reduce([1, 2, 3], 0, (acc, x) -> x + acc)).eq 6
95
96 describe "#lift", ->
97 describe "dealing with values", ->
98 it "converts the return into a promise", ->
99 fn = (input) -> "#{input}-value"
100
101 _.lift(fn)('in').then (value) ->
102 expect(value).eq 'in-value'
103
104 it "raises a rejected promise when the function throw an error", ->
105 fn = -> throw new Error('err')
106
107 expect(_.lift(fn)()).hold.reject('err')
108
109 describe "dealing with promises", ->
110 it "returns a promise if it's already one", ->
111 fn = -> Promise.resolve('value')
112
113 _.lift(fn)().then (value) ->
114 expect(value).eq 'value'
115
116 it "raises a rejected promise when the given promise is failed throw an error", ->
117 fn = -> Promise.reject(new Error('err'))
118
119 expect(_.lift(fn)()).hold.reject('err')
120
121 describe "#humanizeFieldName", ->
122 it "transforms the field name a more human form", ->
123 expect(_.humanizeFieldName('')).eq ''
124 expect(_.humanizeFieldName('Name')).eq 'Name'
125 expect(_.humanizeFieldName('name')).eq 'Name'
126 expect(_.humanizeFieldName('NAME')).eq 'Name'
127 expect(_.humanizeFieldName('password_confirmation')).eq 'Password confirmation'