1 |
|
2 |
|
3 | var amalgamatic = require('../index.js');
|
4 |
|
5 | var pluginTestDouble = {
|
6 | search: function (query, callback) {
|
7 | if (query.searchTerm === 'error') {
|
8 | callback(new Error('There was an error! Oh noes!'));
|
9 | } else if (query.searchTerm === 'options') {
|
10 | callback(null, query);
|
11 | } else {
|
12 | callback(null, {data: [
|
13 | {name: 'Result 1', url: 'http://example.com/1'},
|
14 | {name: 'Result 2', url: 'http://example.com/2'}
|
15 | ]});
|
16 | }
|
17 | }
|
18 | };
|
19 |
|
20 | amalgamatic.add('plugin', pluginTestDouble);
|
21 |
|
22 | var Lab = require('lab');
|
23 | var lab = exports.lab = Lab.script();
|
24 |
|
25 | var expect = Lab.expect;
|
26 | var describe = lab.experiment;
|
27 | var it = lab.test;
|
28 |
|
29 | describe('exports', function () {
|
30 | it('should have a search property', function (done) {
|
31 | expect(typeof amalgamatic.search).to.equal('function');
|
32 | done();
|
33 | });
|
34 |
|
35 | it('should pass the entire query object to the plugin', function (done) {
|
36 | var query = {searchTerm: 'options', fhqwhgads: 'fhqwhgads'};
|
37 | var expectedResult = query;
|
38 | expectedResult.name = 'plugin';
|
39 | amalgamatic.search(query, function (err, results) {
|
40 | expect(results).to.deep.equal([expectedResult]);
|
41 | done();
|
42 | });
|
43 | });
|
44 |
|
45 | it('returns only specified collection', function (done) {
|
46 | amalgamatic.search({searchTerm: 'medicine', collections: ['plugin']}, function (err, results) {
|
47 | expect(results[0].name).to.equal('plugin');
|
48 | expect(results[0].data.length > 0).to.be.true;
|
49 | expect(err).to.be.not.ok;
|
50 | done();
|
51 | });
|
52 | });
|
53 |
|
54 | it('returns an error if an invalid collection is specified', function (done) {
|
55 | amalgamatic.search({searchTerm: 'medicine', collections: ['fhqwhgads']}, function (err, results) {
|
56 | expect(results).to.be.not.ok;
|
57 | expect(err).to.deep.equal(new Error('Collection "fhqwhgads" does not exist'));
|
58 | done();
|
59 | });
|
60 | });
|
61 |
|
62 | it('returns multiple collections if specified', function (done) {
|
63 | amalgamatic.search({searchTerm: 'medicine', collections: ['plugin', 'fhqwhgads']}, function (err, results) {
|
64 | expect(results).to.be.not.ok;
|
65 | expect(err).to.be.ok;
|
66 | done();
|
67 | });
|
68 | });
|
69 |
|
70 | it('returns all collections if no collection specified', function (done) {
|
71 | amalgamatic.search({searchTerm: 'medicine'}, function (err, results) {
|
72 | expect(results[0].name).to.equal('plugin');
|
73 | expect(results[0].data).to.be.ok;
|
74 | done();
|
75 | });
|
76 | });
|
77 |
|
78 | it('provides the main callback with errors that are returned by the plugin', function (done) {
|
79 | amalgamatic.search({searchTerm: 'error', collections: ['plugin']}, function (err, results) {
|
80 | expect(results).to.be.not.ok;
|
81 | expect(err).to.deep.equal(new Error('There was an error! Oh noes!'));
|
82 | done();
|
83 | });
|
84 | });
|
85 |
|
86 | it('provides the plugin callbacks with errors that are returned by the plugin', function (done) {
|
87 | var pluginCallback = function (err, value) {
|
88 | expect(err).to.be.ok;
|
89 | expect(value).to.be.not.ok;
|
90 | done();
|
91 | };
|
92 |
|
93 | amalgamatic.search({searchTerm: 'error', pluginCallback: pluginCallback});
|
94 | });
|
95 |
|
96 | it('limits results to sepcified maxResults', function (done) {
|
97 | amalgamatic.search({searchTerm: 'medicine', maxResults: 1}, function (err, results) {
|
98 | expect(err).to.be.null;
|
99 | expect(results[0].name).to.equal('plugin');
|
100 | expect(results[0].data.length).to.equal(1);
|
101 | done();
|
102 | });
|
103 | });
|
104 |
|
105 | it('does not cause a TypeError if maxResults set and plugin returns an error', function (done) {
|
106 | var pluginCallback = function (err, value) {
|
107 | expect(err).to.be.ok;
|
108 | expect(value).to.be.not.ok;
|
109 | done();
|
110 | };
|
111 |
|
112 | amalgamatic.search({searchTerm: 'error', pluginCallback: pluginCallback, maxResults: 5});
|
113 | });
|
114 |
|
115 | it('runs pluginCallback for each plugin before runnning its own callback', function (done) {
|
116 | var pluginCallbackRan = false;
|
117 |
|
118 | var pluginCallback = function (err, value) {
|
119 | expect(value.data.length).to.equal(2);
|
120 | pluginCallbackRan = true;
|
121 | };
|
122 |
|
123 | amalgamatic.search({searchTerm: 'medicine', pluginCallback: pluginCallback}, function () {
|
124 | expect(pluginCallbackRan).to.be.ok;
|
125 | done();
|
126 | });
|
127 | });
|
128 |
|
129 | it('should include the plugin name in the value for pluginCallback', function (done) {
|
130 | var pluginCallback = function (err, value) {
|
131 | expect(value.name).to.equal('plugin');
|
132 | done();
|
133 | };
|
134 |
|
135 | amalgamatic.search({searchTerm: 'medicine', pluginCallback: pluginCallback});
|
136 | });
|
137 |
|
138 | it('should run with a null callback as the user can still send a plugin-level callback', function (done) {
|
139 | amalgamatic.search({searchTerm: 'medicine'});
|
140 | done();
|
141 | });
|
142 |
|
143 | it('should not give callbacks access to local vars', function (done) {
|
144 | amalgamatic.search({searchTerm: 'medicine'}, function () {
|
145 | var requestedCollections = requestedCollections || 'fhqwhgads';
|
146 | expect(requestedCollections).to.equal('fhqwhgads');
|
147 | done();
|
148 | });
|
149 | });
|
150 |
|
151 | it('should return identically formatted data whether using main callback or plugin callback', function (done) {
|
152 | var pluginResults = [];
|
153 | var pluginCallback = function (err, result) {
|
154 | pluginResults.push(result);
|
155 | };
|
156 |
|
157 | amalgamatic.search({searchTerm: 'medicine', pluginCallback: pluginCallback}, function (err, results) {
|
158 | expect(results).to.deep.equal(pluginResults);
|
159 | done();
|
160 | });
|
161 | });
|
162 | });
|
163 |
|