1 |
3a515b92
|
cagy
|
var tape = require('tape')
|
2 |
|
|
var thunky = require('./')
|
3 |
|
|
|
4 |
|
|
tape('run only once', function (t) {
|
5 |
|
|
t.plan(3)
|
6 |
|
|
|
7 |
|
|
var ran = 0
|
8 |
|
|
var run = thunky(function (cb) {
|
9 |
|
|
ran++
|
10 |
|
|
cb()
|
11 |
|
|
})
|
12 |
|
|
|
13 |
|
|
run(function () {
|
14 |
|
|
t.same(ran, 1, 'ran once')
|
15 |
|
|
})
|
16 |
|
|
run(function () {
|
17 |
|
|
t.same(ran, 1, 'ran once')
|
18 |
|
|
})
|
19 |
|
|
run(function () {
|
20 |
|
|
t.same(ran, 1, 'ran once')
|
21 |
|
|
})
|
22 |
|
|
})
|
23 |
|
|
|
24 |
|
|
tape('run only once async', function (t) {
|
25 |
|
|
t.plan(3)
|
26 |
|
|
|
27 |
|
|
var ran = 0
|
28 |
|
|
var run = thunky(function (cb) {
|
29 |
|
|
process.nextTick(function () {
|
30 |
|
|
ran++
|
31 |
|
|
cb()
|
32 |
|
|
})
|
33 |
|
|
})
|
34 |
|
|
|
35 |
|
|
run(function () {
|
36 |
|
|
t.same(ran, 1, 'ran once')
|
37 |
|
|
})
|
38 |
|
|
run(function () {
|
39 |
|
|
t.same(ran, 1, 'ran once')
|
40 |
|
|
})
|
41 |
|
|
run(function () {
|
42 |
|
|
t.same(ran, 1, 'ran once')
|
43 |
|
|
})
|
44 |
|
|
})
|
45 |
|
|
|
46 |
|
|
tape('re-run on error', function (t) {
|
47 |
|
|
t.plan(3)
|
48 |
|
|
|
49 |
|
|
var ran = 0
|
50 |
|
|
var run = thunky(function (cb) {
|
51 |
|
|
ran++
|
52 |
|
|
cb(new Error('stop'))
|
53 |
|
|
})
|
54 |
|
|
|
55 |
|
|
run(function () {
|
56 |
|
|
t.same(ran, 1, 'ran once')
|
57 |
|
|
run(function () {
|
58 |
|
|
t.same(ran, 2, 'ran once')
|
59 |
|
|
run(function () {
|
60 |
|
|
t.same(ran, 3, 'ran once')
|
61 |
|
|
})
|
62 |
|
|
})
|
63 |
|
|
})
|
64 |
|
|
})
|
65 |
|
|
|
66 |
|
|
tape('pass arguments', function (t) {
|
67 |
|
|
t.plan(6)
|
68 |
|
|
|
69 |
|
|
var ran = 0
|
70 |
|
|
var run = thunky(function (fn) {
|
71 |
|
|
ran++
|
72 |
|
|
fn({ hello: 'world' })
|
73 |
|
|
})
|
74 |
|
|
|
75 |
|
|
run(function (val) {
|
76 |
|
|
t.same(ran, 1, 'ran once')
|
77 |
|
|
t.same(val, { hello: 'world' })
|
78 |
|
|
run(function (val) {
|
79 |
|
|
t.same(ran, 1, 'ran once')
|
80 |
|
|
t.same(val, { hello: 'world' })
|
81 |
|
|
run(function (val) {
|
82 |
|
|
t.same(ran, 1, 'ran once')
|
83 |
|
|
t.same(val, { hello: 'world' })
|
84 |
|
|
})
|
85 |
|
|
})
|
86 |
|
|
})
|
87 |
|
|
})
|
88 |
|
|
|
89 |
|
|
tape('callback is optional', function (t) {
|
90 |
|
|
t.plan(2)
|
91 |
|
|
|
92 |
|
|
var ran = 0
|
93 |
|
|
var run = thunky(function (fn) {
|
94 |
|
|
ran++
|
95 |
|
|
fn({ hello: 'world' })
|
96 |
|
|
})
|
97 |
|
|
|
98 |
|
|
run()
|
99 |
|
|
run(function (val) {
|
100 |
|
|
t.same(ran, 1, 'ran once')
|
101 |
|
|
t.same(val, { hello: 'world' })
|
102 |
|
|
})
|
103 |
|
|
})
|
104 |
|
|
|
105 |
|
|
tape('always async', function (t) {
|
106 |
|
|
t.plan(2)
|
107 |
|
|
|
108 |
|
|
var run = thunky(function (cb) {
|
109 |
|
|
process.nextTick(cb)
|
110 |
|
|
})
|
111 |
|
|
|
112 |
|
|
var sync = true
|
113 |
|
|
run(function () {
|
114 |
|
|
t.ok(!sync, 'not sync')
|
115 |
|
|
var innerSync = true
|
116 |
|
|
run(function () {
|
117 |
|
|
t.ok(!innerSync, 'not sync')
|
118 |
|
|
})
|
119 |
|
|
innerSync = false
|
120 |
|
|
})
|
121 |
|
|
sync = false
|
122 |
|
|
})
|