-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathqueryID.js
96 lines (73 loc) · 2.13 KB
/
queryID.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var algoliasearchHelper = require('../../../index');
var fakeClient = {};
test('the queryid should keep increasing when new requests arrives', function () {
var initialQueryID;
var client = {
search: function () {
initialQueryID++;
return new Promise(function () {});
},
};
var helper = algoliasearchHelper(client, null, {});
initialQueryID = helper._queryId;
helper.search().search().search().search().search();
expect(helper._queryId).toBe(initialQueryID);
});
test('the response handler should check that the query is not outdated', function (done) {
var testData =
require('../../../test/datasets/SearchParameters/search.dataset')();
var shouldTriggerResult = true;
var callCount = 0;
var helper = algoliasearchHelper(fakeClient, null, {});
helper.on('result', function () {
callCount++;
if (!shouldTriggerResult) {
done.fail('The id was outdated');
}
});
var states = [
{
state: helper.state,
queriesCount: 1,
helper: helper,
},
];
helper._dispatchAlgoliaResponse(
states,
helper._lastQueryIdReceived + 1,
testData.response
);
helper._dispatchAlgoliaResponse(
states,
helper._lastQueryIdReceived + 10,
testData.response
);
expect(callCount).toBe(2);
shouldTriggerResult = false;
helper._dispatchAlgoliaResponse(
states,
helper._lastQueryIdReceived - 1,
testData.response
);
expect(callCount).toBe(2);
done();
});
test('the error handler should check that the query is not outdated', function (done) {
var shouldTriggerError = true;
var callCount = 0;
var helper = algoliasearchHelper(fakeClient, null, {});
helper.on('error', function () {
callCount++;
if (!shouldTriggerError) {
done.fail('The id was outdated');
}
});
helper._dispatchAlgoliaError(helper._lastQueryIdReceived + 1, new Error());
helper._dispatchAlgoliaError(helper._lastQueryIdReceived + 10, new Error());
expect(callCount).toBe(2);
shouldTriggerError = false;
helper._dispatchAlgoliaError(helper._lastQueryIdReceived - 1, new Error());
expect(callCount).toBe(2);
done();
});