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

pluginSpec.js « formActions « plugins « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ad5f24472ce92ebfdd46a021482d553ba9989e48 (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
/*****************************************************************************
 * 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 {
    createMouseEvent,
    createOpenMct,
    resetApplicationState
} from 'utils/testing';
import Vue from 'vue';

import { debounce } from 'lodash';

describe('EditPropertiesAction plugin', () => {
    let editPropertiesAction;
    let openmct;
    let element;

    beforeEach((done) => {
        element = document.createElement('div');
        element.style.display = 'block';
        element.style.width = '1920px';
        element.style.height = '1080px';

        openmct = createOpenMct();
        openmct.on('start', done);
        openmct.startHeadless(element);

        editPropertiesAction = openmct.actions.getAction('properties');
    });

    afterEach(() => {
        editPropertiesAction = null;

        return resetApplicationState(openmct);
    });

    it('editPropertiesAction exists', () => {
        expect(editPropertiesAction.key).toEqual('properties');
    });

    it('edit properties action applies to only persistable objects', () => {
        spyOn(openmct.objects, 'isPersistable').and.returnValue(true);

        const domainObject = {
            name: 'mock folder',
            type: 'folder',
            identifier: {
                key: 'mock-folder',
                namespace: ''
            },
            composition: []
        };
        const isApplicableTo = editPropertiesAction.appliesTo([domainObject]);
        expect(isApplicableTo).toBe(true);
    });

    it('edit properties action does not apply to non persistable objects', () => {
        spyOn(openmct.objects, 'isPersistable').and.returnValue(false);

        const domainObject = {
            name: 'mock folder',
            type: 'folder',
            identifier: {
                key: 'mock-folder',
                namespace: ''
            },
            composition: []
        };
        const isApplicableTo = editPropertiesAction.appliesTo([domainObject]);
        expect(isApplicableTo).toBe(false);
    });

    it('edit properties action when invoked shows form', (done) => {
        const domainObject = {
            name: 'mock folder',
            notes: 'mock notes',
            type: 'folder',
            identifier: {
                key: 'mock-folder',
                namespace: ''
            },
            modified: 1643065068597,
            persisted: 1643065068600,
            composition: []
        };

        editPropertiesAction.invoke([domainObject])
            .then(() => {
                done();
            })
            .catch(() => {
                done();
            });

        Vue.nextTick(() => {
            const form = document.querySelector('.js-form');
            const title = form.querySelector('input');
            expect(title.value).toEqual(domainObject.name);

            const notes = form.querySelector('textArea');
            expect(notes.value).toEqual(domainObject.notes);

            const buttons = form.querySelectorAll('button');
            expect(buttons[0].textContent.trim()).toEqual('OK');
            expect(buttons[1].textContent.trim()).toEqual('Cancel');

            const clickEvent = createMouseEvent('click');
            buttons[1].dispatchEvent(clickEvent);
        });
    });

    it('edit properties action saves changes', (done) => {
        const oldName = 'mock folder';
        const newName = 'renamed mock folder';
        const domainObject = {
            name: oldName,
            notes: 'mock notes',
            type: 'folder',
            identifier: {
                key: 'mock-folder',
                namespace: ''
            },
            modified: 1643065068597,
            persisted: 1643065068600,
            composition: []
        };
        let unObserve;

        function callback(newObject) {
            expect(newObject.name).not.toEqual(oldName);
            expect(newObject.name).toEqual(newName);

            unObserve();
            done();
        }

        const deBouncedCallback = debounce(callback, 300);
        unObserve = openmct.objects.observe(domainObject, '*', deBouncedCallback);

        editPropertiesAction.invoke([domainObject]);

        Vue.nextTick(() => {
            const form = document.querySelector('.js-form');
            const title = form.querySelector('input');
            const notes = form.querySelector('textArea');

            const buttons = form.querySelectorAll('button');
            expect(buttons[0].textContent.trim()).toEqual('OK');
            expect(buttons[1].textContent.trim()).toEqual('Cancel');

            expect(title.value).toEqual(domainObject.name);
            expect(notes.value).toEqual(domainObject.notes);

            // change input field value and dispatch event for it
            title.focus();
            title.value = newName;
            title.dispatchEvent(new Event('input'));
            title.blur();

            const clickEvent = createMouseEvent('click');
            buttons[0].dispatchEvent(clickEvent);
        });
    });

    it('edit properties action discards changes', (done) => {
        const name = 'mock folder';
        const domainObject = {
            name,
            notes: 'mock notes',
            type: 'folder',
            identifier: {
                key: 'mock-folder',
                namespace: ''
            },
            modified: 1643065068597,
            persisted: 1643065068600,
            composition: []
        };

        editPropertiesAction.invoke([domainObject])
            .then(() => {
                expect(domainObject.name).toEqual(name);
                done();
            })
            .catch(() => {
                expect(domainObject.name).toEqual(name);
                done();
            });

        const form = document.querySelector('.js-form');
        const buttons = form.querySelectorAll('button');
        const clickEvent = createMouseEvent('click');
        buttons[1].dispatchEvent(clickEvent);
    });
});