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

RuleSpec.js « test « summaryWidget « plugins « src - github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: df5108f7aee5b15d1d208341bebb2f45b5b493ce (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
define(['../src/Rule'], function (Rule) {
    describe('A Summary Widget Rule', function () {
        let mockRuleConfig;
        let mockDomainObject;
        let mockOpenMCT;
        let mockConditionManager;
        let mockWidgetDnD;
        let mockEvaluator;
        let mockContainer;
        let testRule;
        let removeSpy;
        let duplicateSpy;
        let changeSpy;
        let conditionChangeSpy;

        beforeEach(function () {
            mockRuleConfig = {
                name: 'Name',
                id: 'mockRule',
                icon: 'test-icon-name',
                style: {
                    'background-color': '',
                    'border-color': '',
                    'color': ''
                },
                expanded: true,
                conditions: [{
                    object: '',
                    key: '',
                    operation: '',
                    values: []
                }, {
                    object: 'blah',
                    key: 'blah',
                    operation: 'blah',
                    values: ['blah.', 'blah!', 'blah?']
                }]
            };
            mockDomainObject = {
                configuration: {
                    ruleConfigById: {
                        mockRule: mockRuleConfig,
                        otherRule: {}
                    },
                    ruleOrder: ['default', 'mockRule', 'otherRule']
                }
            };

            mockOpenMCT = {};
            mockOpenMCT.objects = {};
            mockOpenMCT.objects.mutate = jasmine.createSpy('mutate');

            mockEvaluator = {};
            mockEvaluator.getOperationDescription = jasmine.createSpy('evaluator')
                .and.returnValue('Operation Description');

            mockConditionManager = jasmine.createSpyObj('mockConditionManager', [
                'on',
                'getComposition',
                'loadCompleted',
                'getEvaluator',
                'getTelemetryMetadata',
                'metadataLoadCompleted',
                'getObjectName',
                'getTelemetryPropertyName'
            ]);
            mockConditionManager.loadCompleted.and.returnValue(false);
            mockConditionManager.metadataLoadCompleted.and.returnValue(false);
            mockConditionManager.getEvaluator.and.returnValue(mockEvaluator);
            mockConditionManager.getComposition.and.returnValue({});
            mockConditionManager.getTelemetryMetadata.and.returnValue({});
            mockConditionManager.getObjectName.and.returnValue('Object Name');
            mockConditionManager.getTelemetryPropertyName.and.returnValue('Property Name');

            mockWidgetDnD = jasmine.createSpyObj('dnd', [
                'on',
                'setDragImage',
                'dragStart'
            ]);

            mockContainer = document.createElement('div');

            removeSpy = jasmine.createSpy('removeCallback');
            duplicateSpy = jasmine.createSpy('duplicateCallback');
            changeSpy = jasmine.createSpy('changeCallback');
            conditionChangeSpy = jasmine.createSpy('conditionChangeCallback');

            testRule = new Rule(mockRuleConfig, mockDomainObject, mockOpenMCT, mockConditionManager,
                mockWidgetDnD);
            testRule.on('remove', removeSpy);
            testRule.on('duplicate', duplicateSpy);
            testRule.on('change', changeSpy);
            testRule.on('conditionChange', conditionChangeSpy);
        });

        it('closes its configuration panel on initial load', function () {
            expect(testRule.getProperty('expanded')).toEqual(false);
        });

        it('gets its DOM element', function () {
            mockContainer.append(testRule.getDOM());
            expect(mockContainer.querySelectorAll('.l-widget-rule').length).toBeGreaterThan(0);
        });

        it('gets its configuration properties', function () {
            expect(testRule.getProperty('name')).toEqual('Name');
            expect(testRule.getProperty('icon')).toEqual('test-icon-name');
        });

        it('can duplicate itself', function () {
            testRule.duplicate();
            mockRuleConfig.expanded = true;
            expect(duplicateSpy).toHaveBeenCalledWith(mockRuleConfig);
        });

        it('can remove itself from the configuration', function () {
            testRule.remove();
            expect(removeSpy).toHaveBeenCalled();
            expect(mockDomainObject.configuration.ruleConfigById.mockRule).not.toBeDefined();
            expect(mockDomainObject.configuration.ruleOrder).toEqual(['default', 'otherRule']);
        });

        it('updates its configuration on a condition change and invokes callbacks', function () {
            testRule.onConditionChange({
                value: 'newValue',
                property: 'object',
                index: 0
            });
            expect(testRule.getProperty('conditions')[0].object).toEqual('newValue');
            expect(conditionChangeSpy).toHaveBeenCalled();
        });

        it('allows initializing a new condition with a default configuration', function () {
            testRule.initCondition();
            expect(mockRuleConfig.conditions).toEqual([{
                object: '',
                key: '',
                operation: '',
                values: []
            }, {
                object: 'blah',
                key: 'blah',
                operation: 'blah',
                values: ['blah.', 'blah!', 'blah?']
            }, {
                object: '',
                key: '',
                operation: '',
                values: []
            }]);
        });

        it('allows initializing a new condition from a given configuration', function () {
            testRule.initCondition({
                sourceCondition: {
                    object: 'object1',
                    key: 'key1',
                    operation: 'operation1',
                    values: [1, 2, 3]
                },
                index: 0
            });
            expect(mockRuleConfig.conditions).toEqual([{
                object: '',
                key: '',
                operation: '',
                values: []
            }, {
                object: 'object1',
                key: 'key1',
                operation: 'operation1',
                values: [1, 2, 3]
            }, {
                object: 'blah',
                key: 'blah',
                operation: 'blah',
                values: ['blah.', 'blah!', 'blah?']
            }]);
        });

        it('invokes mutate when updating the domain object', function () {
            testRule.updateDomainObject();
            expect(mockOpenMCT.objects.mutate).toHaveBeenCalled();
        });

        it('builds condition view from condition configuration', function () {
            mockContainer.append(testRule.getDOM());
            expect(mockContainer.querySelectorAll('.t-condition').length).toEqual(2);
        });

        it('responds to input of style properties, and updates the preview', function () {
            testRule.colorInputs['background-color'].set('#434343');
            expect(mockRuleConfig.style['background-color']).toEqual('#434343');
            testRule.colorInputs['border-color'].set('#666666');
            expect(mockRuleConfig.style['border-color']).toEqual('#666666');
            testRule.colorInputs.color.set('#999999');
            expect(mockRuleConfig.style.color).toEqual('#999999');

            expect(testRule.thumbnail.style['background-color']).toEqual('rgb(67, 67, 67)');
            expect(testRule.thumbnail.style['border-color']).toEqual('rgb(102, 102, 102)');
            expect(testRule.thumbnail.style.color).toEqual('rgb(153, 153, 153)');

            expect(changeSpy).toHaveBeenCalled();
        });

        it('responds to input for the icon property', function () {
            testRule.iconInput.set('icon-alert-rect');
            expect(mockRuleConfig.icon).toEqual('icon-alert-rect');
            expect(changeSpy).toHaveBeenCalled();
        });

        /*
        test for js condition commented out for v1
        */

        // it('responds to input of text properties', function () {
        //     var testInputs = ['name', 'label', 'message', 'jsCondition'],
        //         input;

        //     testInputs.forEach(function (key) {
        //         input = testRule.textInputs[key];
        //         input.prop('value', 'A new ' + key);
        //         input.trigger('input');
        //         expect(mockRuleConfig[key]).toEqual('A new ' + key);
        //     });

        //     expect(changeSpy).toHaveBeenCalled();
        // });

        it('allows input for when the rule triggers', function () {
            testRule.trigger.value = 'all';
            const event = new Event('change', {
                bubbles: true,
                cancelable: true
            });
            testRule.trigger.dispatchEvent(event);
            expect(testRule.config.trigger).toEqual('all');
            expect(conditionChangeSpy).toHaveBeenCalled();
        });

        it('generates a human-readable description from its conditions', function () {
            testRule.generateDescription();
            expect(testRule.config.description).toContain(
                'Object Name\'s Property Name Operation Description'
            );
            testRule.config.trigger = 'js';
            testRule.generateDescription();
            expect(testRule.config.description).toContain(
                'when a custom JavaScript condition evaluates to true'
            );
        });

        it('initiates a drag event when its grippy is clicked', function () {
            const event = new Event('mousedown', {
                bubbles: true,
                cancelable: true
            });
            testRule.grippy.dispatchEvent(event);

            expect(mockWidgetDnD.setDragImage).toHaveBeenCalled();
            expect(mockWidgetDnD.dragStart).toHaveBeenCalledWith('mockRule');
        });

        /*
        test for js condition commented out for v1
        */

        it('can remove a condition from its configuration', function () {
            testRule.removeCondition(0);
            expect(testRule.config.conditions).toEqual([{
                object: 'blah',
                key: 'blah',
                operation: 'blah',
                values: ['blah.', 'blah!', 'blah?']
            }]);
        });
    });
});