1 | pinky = require '../lib'
|
2 |
|
3 | delay = (n, f) -> set-timeout f, n
|
4 |
|
5 | Promise = do
|
6 | then : 'function'
|
7 | always : 'function'
|
8 | otherwise : 'function'
|
9 | fulfill : 'function'
|
10 | reject : 'function'
|
11 |
|
12 | describe 'Pinky Tests' ->
|
13 | describe 'λ pinky-promise(Thenable)' ->
|
14 | [p, p2] = []
|
15 | before-each (done) ->
|
16 | p := pinky!
|
17 | p2 := pinky p
|
18 | done!
|
19 |
|
20 | o 'Should lift the Thenable into a Promise.' (done) ->
|
21 | expect p2 .to.satisfy (a) ->
|
22 | (Object.keys Promise).every (k) -> typeof a[k] is Promise[k]
|
23 | done!
|
24 |
|
25 | o 'Should be fulfilled when the Thenable is.' ->
|
26 | p.fulfill 'a'
|
27 | expect p .to.become 'a'
|
28 | expect p2 .to.become 'a'
|
29 |
|
30 | o 'Should be rejected when the Thenable is.' ->
|
31 | p.reject (new Error 'no u')
|
32 | expect p .to.be.rejected.with /no u/
|
33 | expect p2 .to.be.rejected.with /no u/
|
34 |
|
35 | describe 'λ always(Function)' ->
|
36 | o 'Should be called when promise is fulfilled.' ->
|
37 | p = pinky!fulfill 'a'
|
38 | p2 = p.always (+ 'b')
|
39 | expect p .to.become 'ab'
|
40 | expect p2 .to.become 'ab'
|
41 |
|
42 | o 'Should be called when promise is rejected.' ->
|
43 | p = pinky!fulfill 'a'
|
44 | p2 = p.always (+ 'b')
|
45 | expect p .to.become 'ab'
|
46 | expect p2 .to.become 'ab'
|
47 |
|
48 |
|
49 | describe 'λ otherwise(Function)' ->
|
50 | o 'Should not be called when the promise is fulfilled.' (done) ->
|
51 | s = -> s.called = true
|
52 | p = pinky!fulfill 'a'
|
53 | p2 = p.otherwise s
|
54 | delay 100 -> do
|
55 | expect p .to.become 'a'
|
56 | expect s.called .to.equal void
|
57 | done!
|
58 |
|
59 | o 'Should be called when the promise is rejected.' ->
|
60 | p = pinky!reject (new Error 'no u')
|
61 | p2 = p.otherwise -> ':3'
|
62 | expect p .to.be.rejected.with /no u/
|
63 | expect p2 .to.become ':3' |
\ | No newline at end of file |