1 | var should = require('should');
|
2 | var client = require('..');
|
3 | var express = require('express');
|
4 | var request = require('supertest');
|
5 |
|
6 | var app = express();
|
7 | app.use(express.static(__dirname));
|
8 |
|
9 | describe('hyper-path', function() {
|
10 | var agent = {
|
11 | root: function (fn) {
|
12 | return agent.get('/api/index.json', fn);
|
13 | },
|
14 | get: function(href, fn) {
|
15 | request(app)
|
16 | .get(href)
|
17 | .end(function(err, res) {
|
18 | if (!res.ok) return fn(new Error(res.body || 'HTTP Error ' + res.status));
|
19 | fn(err, res.body);
|
20 | });
|
21 | }
|
22 | };
|
23 |
|
24 | it('should return the root resource', function(done) {
|
25 | client('.apps', agent)
|
26 | .on(function(err, apps) {
|
27 | if (err) return done(err);
|
28 | should.exist(apps);
|
29 | apps.length.should.eql(2);
|
30 | apps.should.be.an.Array;
|
31 | done();
|
32 | });
|
33 | });
|
34 |
|
35 | it('should traverse objects and arrays', function(done) {
|
36 | client('.apps.0.name', agent)
|
37 | .on(function(err, name) {
|
38 | if (err) return done(err);
|
39 | should.exist(name);
|
40 | name.should.eql('app');
|
41 | done();
|
42 | });
|
43 | });
|
44 |
|
45 | it('should get the length of an array', function(done) {
|
46 | client('.apps.length', agent)
|
47 | .on(function(err, length) {
|
48 | if (err) return done(err);
|
49 | should.exist(length);
|
50 | length.should.eql(2);
|
51 | done();
|
52 | });
|
53 | });
|
54 |
|
55 | it('shouldn\'t choke on null properties', function(done) {
|
56 | client('.apps.null-prop.thingy', agent)
|
57 | .on(function(err, thingy) {
|
58 | if (err) return done(err);
|
59 | should.not.exist(thingy);
|
60 | done();
|
61 | });
|
62 | });
|
63 |
|
64 | it('should start from a passed scope', function(done) {
|
65 | client('apps.0.name', agent)
|
66 | .scope({apps: {href: '/api/apps.json'}})
|
67 | .on(function(err, name) {
|
68 | if (err) return done(err);
|
69 | should.exist(name);
|
70 | name.should.eql('app');
|
71 | done();
|
72 | });
|
73 | });
|
74 |
|
75 | it('should refresh on scope change', function(done) {
|
76 | var hasResponded = false;
|
77 | var req = client('apps.0.name', agent)
|
78 | .scope({apps: {href: '/api/apps.json'}})
|
79 | .on(function(err, name) {
|
80 | if (err) return done(err);
|
81 | should.exist(name);
|
82 |
|
83 |
|
84 | if (!hasResponded) {
|
85 | hasResponded = true;
|
86 | name.should.eql('app');
|
87 | return req.scope({apps: {href: '/api/other-apps.json'}});
|
88 | }
|
89 |
|
90 | name.should.eql('other app');
|
91 | done();
|
92 | });
|
93 | });
|
94 |
|
95 | it('should reference resource properties from a standalone collection', function(done) {
|
96 | var root = [
|
97 | {href: '/api/app.json'},
|
98 | {href: '/api/other-app.json'}
|
99 | ];
|
100 | root.href = '/api/apps.json';
|
101 |
|
102 | client('apps.count', agent)
|
103 | .scope({apps: root})
|
104 | .on(function(err, count) {
|
105 | if (err) return done(err);
|
106 | should.exist(count);
|
107 | count.should.eql(2);
|
108 | done();
|
109 | });
|
110 | });
|
111 |
|
112 | it('should return undefined for undefined values on a standalone collection', function(done) {
|
113 | var root = [
|
114 | {href: '/api/app.json'}
|
115 | ];
|
116 | root.href = '/api/apps.json';
|
117 |
|
118 | client('apps.thingy', agent)
|
119 | .scope({apps: root})
|
120 | .on(function(err, thingy) {
|
121 | if (err) return done(err);
|
122 | should.not.exist(thingy);
|
123 | done();
|
124 | });
|
125 | });
|
126 |
|
127 | it('should return a "href"', function(done) {
|
128 | client('.apps.href', agent)
|
129 | .on(function(err, href) {
|
130 | if (err) return done(err);
|
131 | should.exist(href);
|
132 | href.should.match(/apps.json/);
|
133 | done();
|
134 | });
|
135 | });
|
136 |
|
137 | it('should not reference native array functions', function(done) {
|
138 | client('.apps.slice', agent)
|
139 | .on(function(err, slice) {
|
140 | if (err) return done(err);
|
141 | should.not.exist(slice);
|
142 | done();
|
143 | });
|
144 | });
|
145 |
|
146 | it('should return an identity', function(done) {
|
147 | client('thing', agent)
|
148 | .scope({thing: 'value'})
|
149 | .on(function(err, thing) {
|
150 | if (err) return done(err);
|
151 | should.exist(thing);
|
152 | thing.should.eql('value');
|
153 | done();
|
154 | });
|
155 | });
|
156 |
|
157 | it('should return an identity down a prototype chain', function(done) {
|
158 | var obj = Object.create({thing: 'value'});
|
159 | client('thing', agent)
|
160 | .scope(obj)
|
161 | .on(function(err, thing) {
|
162 | if (err) return done(err);
|
163 | should.exist(thing);
|
164 | thing.should.eql('value');
|
165 | done();
|
166 | });
|
167 | });
|
168 |
|
169 | it('should return an identity from a link', function(done) {
|
170 | client('name', agent)
|
171 | .scope({href: '/api/app.json'})
|
172 | .on(function(err, name) {
|
173 | if (err) return done(err);
|
174 | should.exist(name);
|
175 | name.should.eql('app');
|
176 | done();
|
177 | });
|
178 | });
|
179 |
|
180 | it('should follow a JSON pointer', function(done) {
|
181 | client('.pointer', agent)
|
182 | .on(function(err, pointer) {
|
183 | if (err) return done(err);
|
184 | should.exist(pointer);
|
185 | pointer.should.eql('app');
|
186 | done();
|
187 | });
|
188 | });
|
189 |
|
190 | it('should follow a local JSON pointer', function(done) {
|
191 | client('.local-pointer', agent)
|
192 | .on(function(err, pointer) {
|
193 | if (err) return done(err);
|
194 | should.exist(pointer);
|
195 | pointer.should.eql('app');
|
196 | done();
|
197 | });
|
198 | });
|
199 |
|
200 | it('should follow a deeply-nested JSON pointer', function(done) {
|
201 | client('.deep-pointer', agent)
|
202 | .on(function(err, pointer) {
|
203 | if (err) return done(err);
|
204 | should.exist(pointer);
|
205 | pointer.should.eql('app');
|
206 | done();
|
207 | });
|
208 | });
|
209 |
|
210 | it('should follow a deeply-nested JSON pointer', function(done) {
|
211 | client('.deep-pointer', agent)
|
212 | .on(function(err, pointer) {
|
213 | if (err) return done(err);
|
214 | should.exist(pointer);
|
215 | pointer.should.eql('app');
|
216 | done();
|
217 | });
|
218 | });
|
219 |
|
220 | it('should handle JSON pointer collections', function(done) {
|
221 | client('.deeply-nested-collection', agent)
|
222 | .on(function(err, collection) {
|
223 | if (err) return done(err);
|
224 | should.exist(collection);
|
225 | collection.length.should.eql(2);
|
226 |
|
227 | client('col.count', agent)
|
228 | .scope({col: collection})
|
229 | .on(function(err, count) {
|
230 | if (err) return done(err);
|
231 | should.exist(count);
|
232 | count.should.eql(2);
|
233 | done();
|
234 | });
|
235 | });
|
236 | });
|
237 | });
|