Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GrandSearchSpec.js « search « layout « ui « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a4b84515ad5fdbe97af601e48919739980a83bf3 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*****************************************************************************
 * Open MCT, Copyright (c) 2014-2022, United States Government
 * as represented by the Administrator of the National Aeronautics and Space
 * Administration. All rights reserved.
 *
 * Open MCT 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 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.
 *****************************************************************************/

import {
    createOpenMct,
    resetApplicationState
} from 'utils/testing';
import Vue from 'vue';
import GrandSearch from './GrandSearch.vue';
import ExampleTagsPlugin from '../../../../example/exampleTags/plugin';
import DisplayLayoutPlugin from '../../../plugins/displayLayout/plugin';

describe("GrandSearch", () => {
    let openmct;
    let grandSearchComponent;
    let viewContainer;
    let parent;
    let sharedWorkerToRestore;
    let mockDomainObject;
    let mockAnnotationObject;
    let mockDisplayLayout;
    let mockFolderObject;
    let mockAnotherFolderObject;
    let mockTopObject;
    let originalRouterPath;
    let mockNewObject;
    let mockObjectProvider;

    beforeEach((done) => {
        openmct = createOpenMct();
        originalRouterPath = openmct.router.path;
        openmct.router.path = [mockDisplayLayout];
        openmct.editor.edit();

        openmct.install(new ExampleTagsPlugin());
        openmct.install(new DisplayLayoutPlugin());
        const availableTags = openmct.annotation.getAvailableTags();
        mockDomainObject = {
            type: 'notebook',
            name: 'fooRabbitNotebook',
            location: 'fooNameSpace:topObject',
            identifier: {
                key: 'some-object',
                namespace: 'fooNameSpace'
            },
            configuration: {
                entries: {
                    someSection: {
                        somePage: [
                            {
                                id: 'fooBarEntry',
                                text: 'Foo Bar Text'
                            }
                        ]
                    }
                }
            }
        };
        mockTopObject = {
            type: 'root',
            name: 'Top Folder',
            composition: [],
            identifier: {
                key: 'topObject',
                namespace: 'fooNameSpace'
            }
        };
        mockAnotherFolderObject = {
            type: 'folder',
            name: 'Another Test Folder',
            composition: [],
            location: 'fooNameSpace:topObject',
            identifier: {
                key: 'someParent',
                namespace: 'fooNameSpace'
            }
        };
        mockFolderObject = {
            type: 'folder',
            name: 'Test Folder',
            composition: [],
            location: 'fooNameSpace:someParent',
            identifier: {
                key: 'someFolder',
                namespace: 'fooNameSpace'
            }
        };
        mockDisplayLayout = {
            type: 'layout',
            name: 'Bar Layout',
            composition: [],
            identifier: {
                key: 'some-layout',
                namespace: 'fooNameSpace'
            },
            configuration: {
                items: [],
                layoutGrid: [10, 10]
            }
        };
        mockAnnotationObject = {
            type: 'annotation',
            name: 'Some Notebook Annotation',
            annotationType: openmct.annotation.ANNOTATION_TYPES.NOTEBOOK,
            tags: [availableTags[0].id, availableTags[1].id],
            identifier: {
                key: 'anAnnotationKey',
                namespace: 'fooNameSpace'
            },
            targets: {
                'fooNameSpace:some-object': {
                    entryId: 'fooBarEntry'
                }
            }
        };
        mockNewObject = {
            type: 'folder',
            name: 'New Apple Test Folder',
            composition: [],
            location: 'fooNameSpace:topObject',
            identifier: {
                key: 'newApple',
                namespace: 'fooNameSpace'
            }
        };

        openmct.router.isNavigatedObject = jasmine.createSpy().and.returnValue(false);
        mockObjectProvider = jasmine.createSpyObj("mock object provider", [
            "create",
            "update",
            "get"
        ]);
        // eslint-disable-next-line require-await
        mockObjectProvider.get = async (identifier) => {
            if (identifier.key === mockDomainObject.identifier.key) {
                return mockDomainObject;
            } else if (identifier.key === mockAnnotationObject.identifier.key) {
                return mockAnnotationObject;
            } else if (identifier.key === mockDisplayLayout.identifier.key) {
                return mockDisplayLayout;
            } else if (identifier.key === mockFolderObject.identifier.key) {
                return mockFolderObject;
            } else if (identifier.key === mockAnotherFolderObject.identifier.key) {
                return mockAnotherFolderObject;
            } else if (identifier.key === mockTopObject.identifier.key) {
                return mockTopObject;
            } else if (identifier.key === mockNewObject.identifier.key) {
                return mockNewObject;
            } else {
                return null;
            }
        };

        mockObjectProvider.create.and.returnValue(Promise.resolve(true));
        mockObjectProvider.update.and.returnValue(Promise.resolve(true));

        openmct.objects.addProvider('fooNameSpace', mockObjectProvider);

        const mockViewProvider = jasmine.createSpyObj("mock view provider", [
            "key",
            "view",
            "canView"
        ]);

        openmct.objectViews.addProvider(mockViewProvider);

        openmct.on('start', async () => {
            // use local worker
            sharedWorkerToRestore = openmct.objects.inMemorySearchProvider.worker;
            openmct.objects.inMemorySearchProvider.worker = null;
            await openmct.objects.inMemorySearchProvider.index(mockTopObject);
            await openmct.objects.inMemorySearchProvider.index(mockDomainObject);
            await openmct.objects.inMemorySearchProvider.index(mockDisplayLayout);
            await openmct.objects.inMemorySearchProvider.index(mockFolderObject);
            await openmct.objects.inMemorySearchProvider.index(mockAnnotationObject);
            parent = document.createElement('div');
            document.body.appendChild(parent);
            viewContainer = document.createElement('div');
            parent.append(viewContainer);
            grandSearchComponent = new Vue({
                el: viewContainer,
                components: {
                    GrandSearch
                },
                provide: {
                    openmct
                },
                template: '<GrandSearch/>'
            }).$mount();
            await Vue.nextTick();
            done();
        });
        openmct.startHeadless();
    });

    afterEach(() => {
        openmct.objects.inMemorySearchProvider.worker = sharedWorkerToRestore;
        openmct.router.path = originalRouterPath;
        grandSearchComponent.$destroy();
        document.body.removeChild(parent);

        return resetApplicationState(openmct);
    });

    it("should render an object search result", async () => {
        await grandSearchComponent.$children[0].searchEverything('foo');
        await Vue.nextTick();
        const searchResults = document.querySelectorAll('[aria-label="fooRabbitNotebook notebook result"]');
        expect(searchResults.length).toBe(1);
        expect(searchResults[0].innerText).toContain('Rabbit');
    });

    it("should render an object search result if new object added", async () => {
        const composition = openmct.composition.get(mockFolderObject);
        composition.add(mockNewObject);
        // after adding, need to wait a beat for the folder to be indexed
        await Vue.nextTick();
        await grandSearchComponent.$children[0].searchEverything('apple');
        await Vue.nextTick();
        const searchResults = document.querySelectorAll('[aria-label="New Apple Test Folder folder result"]');
        expect(searchResults.length).toBe(1);
        expect(searchResults[0].innerText).toContain('Apple');
    });

    it("should not use InMemorySearch provider if object provider provides search", async () => {
        // eslint-disable-next-line require-await
        mockObjectProvider.search = async (query, abortSignal, searchType) => {
            if (searchType === openmct.objects.SEARCH_TYPES.OBJECTS) {
                return mockNewObject;
            } else {
                return [];
            }
        };

        mockObjectProvider.supportsSearchType = (someType) => {
            return true;
        };

        const composition = openmct.composition.get(mockFolderObject);
        composition.add(mockNewObject);
        await grandSearchComponent.$children[0].searchEverything('apple');
        await Vue.nextTick();
        const searchResults = document.querySelectorAll('[aria-label="New Apple Test Folder folder result"]');
        // This will be of length 2 (doubles) if we're incorrectly searching with InMemorySearchProvider as well
        expect(searchResults.length).toBe(1);
        expect(searchResults[0].innerText).toContain('Apple');
    });

    it("should render an annotation search result", async () => {
        await grandSearchComponent.$children[0].searchEverything('S');
        await Vue.nextTick();
        const annotationResults = document.querySelectorAll('[aria-label="Search Result"]');
        expect(annotationResults.length).toBe(2);
        expect(annotationResults[1].innerText).toContain('Driving');
    });

    it("should render no annotation search results if no match", async () => {
        await grandSearchComponent.$children[0].searchEverything('Qbert');
        await Vue.nextTick();
        const annotationResults = document.querySelectorAll('[aria-label="Search Result"]');
        expect(annotationResults.length).toBe(0);
    });

    it("should preview object search results in edit mode if object clicked", async () => {
        await grandSearchComponent.$children[0].searchEverything('Folder');
        grandSearchComponent._provided.openmct.router.path = [mockDisplayLayout];
        await Vue.nextTick();
        const searchResults = document.querySelectorAll('[name="Test Folder"]');
        expect(searchResults.length).toBe(1);
        expect(searchResults[0].innerText).toContain('Folder');
        searchResults[0].click();
        const previewWindow = document.querySelector('.js-preview-window');
        expect(previewWindow.innerText).toContain('Snapshot');
    });
});