UNPKG

5.21 kBJavaScriptView Raw
1var should = require('should');
2var client = require('..');
3var express = require('express');
4var request = require('supertest');
5
6var app = express();
7app.use(express.static(__dirname));
8
9describe('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 // if this is the first time around refresh the scope
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 not reference native array functions', function(done) {
128 client('.apps.slice', agent)
129 .on(function(err, slice) {
130 if (err) return done(err);
131 should.not.exist(slice);
132 done();
133 });
134 });
135
136 it('should return an identity', function(done) {
137 client('thing', agent)
138 .scope({thing: 'value'})
139 .on(function(err, thing) {
140 if (err) return done(err);
141 should.exist(thing);
142 thing.should.eql('value');
143 done();
144 });
145 });
146
147 it('should return an identity down a prototype chain', function(done) {
148 var obj = Object.create({thing: 'value'});
149 client('thing', agent)
150 .scope(obj)
151 .on(function(err, thing) {
152 if (err) return done(err);
153 should.exist(thing);
154 thing.should.eql('value');
155 done();
156 });
157 });
158
159 it('should return an identity from a link', function(done) {
160 client('name', agent)
161 .scope({href: '/api/app.json'})
162 .on(function(err, name) {
163 if (err) return done(err);
164 should.exist(name);
165 name.should.eql('app');
166 done();
167 });
168 });
169
170 it('should follow a JSON pointer', function(done) {
171 client('.pointer', agent)
172 .on(function(err, pointer) {
173 if (err) return done(err);
174 should.exist(pointer);
175 pointer.should.eql('app');
176 done();
177 });
178 });
179
180 it('should follow a deeply-nested JSON pointer', function(done) {
181 client('.deep-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 deeply-nested JSON pointer', function(done) {
191 client('.deep-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});