openmct/platform/search/test/services/GenericSearchWorkerSpec.js

133 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-08-03 17:21:50 +00:00
/*****************************************************************************
* Open MCT Web, Copyright (c) 2014-2015, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT Web includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/
/*global define,describe,it,expect,runs,waitsFor,beforeEach,jasmine,Worker,require*/
2015-08-03 17:21:50 +00:00
/**
* SearchSpec. Created by shale on 07/31/2015.
*/
define(
[],
function () {
2015-08-03 17:21:50 +00:00
"use strict";
describe("The generic search worker ", function () {
// If this test fails, make sure this path is correct
var worker = new Worker(require.toUrl('platform/search/src/services/GenericSearchWorker.js')),
2015-08-03 17:21:50 +00:00
numObjects = 5;
2015-08-03 17:21:50 +00:00
beforeEach(function () {
var i;
for (i = 0; i < numObjects; i += 1) {
worker.postMessage(
{
request: "index",
id: i,
model: {
name: "object " + i,
id: i,
type: "something"
}
}
);
}
});
2015-08-03 17:21:50 +00:00
it("searches can reach all objects", function () {
var flag = false,
workerOutput,
resultsLength = 0;
2015-08-03 17:21:50 +00:00
// Search something that should return all objects
runs(function () {
worker.postMessage(
{
request: "search",
input: "object",
maxResults: 100,
2015-08-03 17:21:50 +00:00
timestamp: Date.now(),
timeout: 1000
}
);
});
2015-08-03 17:21:50 +00:00
worker.onmessage = function (event) {
var id;
2015-08-03 17:21:50 +00:00
workerOutput = event.data;
for (id in workerOutput.results) {
resultsLength += 1;
}
flag = true;
};
2015-08-03 17:21:50 +00:00
waitsFor(function () {
return flag;
}, "The worker should be searching", 1000);
2015-08-03 17:21:50 +00:00
runs(function () {
expect(workerOutput).toBeDefined();
expect(resultsLength).toEqual(numObjects);
});
});
2015-08-03 17:21:50 +00:00
it("searches return only matches", function () {
var flag = false,
workerOutput,
resultsLength = 0;
2015-08-03 17:21:50 +00:00
// Search something that should return 1 object
runs(function () {
worker.postMessage(
{
request: "search",
input: "2",
maxResults: 100,
2015-08-03 17:21:50 +00:00
timestamp: Date.now(),
timeout: 1000
}
);
});
2015-08-03 17:21:50 +00:00
worker.onmessage = function (event) {
var id;
2015-08-03 17:21:50 +00:00
workerOutput = event.data;
for (id in workerOutput.results) {
resultsLength += 1;
}
flag = true;
};
2015-08-03 17:21:50 +00:00
waitsFor(function () {
return flag;
}, "The worker should be searching", 1000);
2015-08-03 17:21:50 +00:00
runs(function () {
expect(workerOutput).toBeDefined();
expect(resultsLength).toEqual(1);
expect(workerOutput.results[2]).toBeDefined();
});
});
});
}
);