Files: 70bdc654bff8ac23d9006bcbee7a5fa93d8abf62 / test / on-listen.js
1329 bytesRaw
1 | var test = require('tape') |
2 | |
3 | // currently only these types support onListen/unlisten |
4 | // in the future, all types should allow it! |
5 | |
6 | var types = [ |
7 | function array (opts) { |
8 | return require('../array')([], opts) |
9 | }, |
10 | function dict (opts) { |
11 | return require('../dict')({}, opts) |
12 | }, |
13 | function map (opts) { |
14 | return require('../map')([], () => {}, opts) |
15 | }, |
16 | function computed (opts) { |
17 | return require('../computed')({}, () => {}, opts) |
18 | } |
19 | ] |
20 | |
21 | types.forEach(ctor => { |
22 | test(`${ctor.name} - onListen / onUnlisten`, t => { |
23 | var onListenCount = 0 |
24 | var onUnlistenCount = 0 |
25 | var onListenReleaseCount = 0 |
26 | |
27 | var obs = ctor({ |
28 | onListen: () => { |
29 | onListenCount += 1 |
30 | return () => { |
31 | onListenReleaseCount += 1 |
32 | } |
33 | }, |
34 | onUnlisten: () => { |
35 | onUnlistenCount += 1 |
36 | } |
37 | }) |
38 | |
39 | t.equal(onListenCount, 0) |
40 | |
41 | var unlisten1 = obs(() => {}) |
42 | t.equal(onListenCount, 1) |
43 | |
44 | var unlisten2 = obs(() => {}) |
45 | t.equal(onListenCount, 1) // should still be 1 |
46 | |
47 | unlisten1() |
48 | t.equal(onUnlistenCount, 0) |
49 | |
50 | unlisten2() |
51 | t.equal(onUnlistenCount, 1) |
52 | t.equal(onListenReleaseCount, 1) |
53 | |
54 | var unlisten3 = obs(() => {}) |
55 | t.equal(onListenCount, 2) |
56 | |
57 | unlisten3() |
58 | t.equal(onUnlistenCount, 2) |
59 | t.equal(onListenReleaseCount, 2) |
60 | |
61 | t.end() |
62 | }) |
63 | }) |
64 |
Built with git-ssb-web