UNPKG

4.32 kBHTMLView Raw
1<html>
2<head>
3 <title>Hyper-path Test Page</title>
4 <script type="text/javascript" src="build/build.js"></script>
5</head>
6<body>
7 <a href="javascript:rootTest()">Root test</a>
8 <a href="javascript:nestedTest()">Nested test</a>
9 <a href="javascript:multiTest()">Multi test</a>
10 <a href="javascript:passedScopeTest()">Passed scope test</a>
11 <a href="javascript:changedScopeTest()">Changed scope test</a>
12 <a href="javascript:collectionScopeTest()">Collection scope test</a>
13 <script type="text/javascript">
14 var client = require('hyper-path');
15 var emitter = require('hyper-emitter');
16
17 function agent(fn) {
18 return emitter.get('/api/index.json', fn);
19 }
20
21 agent.get = function(href, fn) {
22 return emitter.get(href, fn);
23 }
24
25 function rootTest() {
26 var req = client('.apps', agent)
27 .on(function(err, apps) {
28 console.log(arguments);
29 });
30
31 var ref = setInterval(function() {
32 emitter.refresh('/api/apps.json');
33 }, 100);
34
35 setTimeout(function() {
36 unsub(req);
37 }, 1000);
38
39 setTimeout(function() {
40 clearInterval(ref);
41 }, 2000);
42 }
43
44 function nestedTest() {
45 var req = client('.apps.0.name', agent)
46 .on(function(err, name) {
47 console.log(arguments);
48 });
49
50 var ref = setInterval(function() {
51 emitter.refresh('/api/apps.json');
52 }, 100);
53
54 setTimeout(function() {
55 unsub(req);
56 }, 1000);
57
58 setTimeout(function() {
59 clearInterval(ref);
60 }, 2000);
61 }
62
63 function multiTest() {
64 var first = client('.apps.0.name', agent)
65 .on(function(err, name) {
66 console.log('first', name);
67 });
68
69 var second = client('.apps.0.name', agent)
70 .on(function(err, name) {
71 console.log('second', name);
72 });
73
74 var ref = setInterval(function() {
75 emitter.refresh('/api/apps.json');
76 }, 100);
77
78 setTimeout(function() {
79 unsub(first);
80 }, 1000);
81 setTimeout(function() {
82 unsub(second);
83 }, 2000);
84
85 setTimeout(function() {
86 clearInterval(ref);
87 }, 3000);
88 }
89
90 function passedScopeTest() {
91 var req = client('apps.0.name', agent)
92 .scope({apps: {href: '/api/apps.json'}})
93 .on(function(err, name) {
94 console.log(arguments);
95 });
96
97 var appsref = setInterval(function() {
98 emitter.refresh('/api/apps.json');
99 }, 300);
100
101 var appref = setInterval(function() {
102 emitter.refresh('/api/app.json');
103 }, 200);
104
105 setTimeout(function() {
106 unsub(req);
107 }, 1000);
108
109 setTimeout(function() {
110 clearInterval(appref);
111 clearInterval(appsref);
112 }, 2000);
113 }
114
115 function changedScopeTest() {
116 var req = client('apps.0.name', agent)
117 .scope({apps: {href: '/api/apps.json'}})
118 .on(function(err, name) {
119 console.log(arguments);
120 });
121
122 setTimeout(function() {
123 req.scope({apps: {href: '/api/other-apps.json'}});
124 }, 1000);
125
126 var appref = setInterval(function() {
127 emitter.refresh('/api/app.json');
128 }, 100);
129
130 var otherref = setInterval(function() {
131 emitter.refresh('/api/other-app.json');
132 }, 200);
133
134 setTimeout(function() {
135 unsub(req);
136 }, 2000);
137
138 setTimeout(function() {
139 clearInterval(appref);
140 clearInterval(otherref);
141 }, 3000);
142 }
143
144 function collectionScopeTest() {
145 var root = [{href: '/api/app.json'}, {href: '/api/other-app.json'}];
146 root.href = '/api/apps.json';
147 var req1 = client('apps.count', agent)
148 .scope({apps: root})
149 .on(function(err, count) {
150 console.log('count should be 2', count);
151 unsub(req1);
152 });
153
154 var req2 = client('apps.length', agent)
155 .scope({apps: root})
156 .on(function(err, count) {
157 console.log('length should be 2', count);
158 unsub(req2);
159 });
160
161 var req3 = client('apps.thingy', agent)
162 .scope({apps: root})
163 .on(function(err, val) {
164 console.log('thingy should be undefined', val);
165 unsub(req3);
166 });
167 }
168
169 function unsub(req) {
170 if (req) req.off();
171 }
172
173 </script>
174</body>
175</html>