UI support for service update via tosca template import
[sdc.git] / openecomp-ui / test-utils / Util.js
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import deepFreeze from 'deep-freeze';
17 import times from 'lodash/times';
18 import pick from 'lodash/pick';
19 import intersection from 'lodash/intersection';
20 import ReactTestUtils from 'react-dom/test-utils';
21
22 export const buildListFromFactory = (factory, quantity = 3, overrides) => {
23         let list = [];
24         times(quantity, () =>{
25                 list.push(factory.build(overrides));
26         });
27         return list;
28 };
29
30 export const buildFromExistingObject = (factory, obj, overrides = {}, options = {}) => {
31         const mock = factory.build();
32         const sharedProperties = intersection(Object.keys(mock), Object.keys(obj));
33         return factory.build({...pick(obj, sharedProperties), ...overrides}, options);
34 };
35
36 //returned object should be treated as immutable.
37 export const cloneAndSet = (obj, path, value) => {
38         let retVal = {...obj};
39         let inner = retVal;
40
41         if (typeof path === 'string') {
42                 path = path.split('.');
43         }
44
45         for (let i = 0; i < path.length - 1; i++) {
46                 inner[path[i]] = {
47                         ...inner[path[i]]
48                 };
49                 inner = inner[path[i]];
50         }
51         inner[path[path.length - 1]] = value;
52         return deepFreeze(retVal);
53 };
54
55 /**
56  * array findAllRenderedDOMComponentsWithTestId(
57  ReactComponent tree,
58  function test
59  )
60  * @param tree - ReactComponent
61  * @param testId - string
62  * @returns {Array.<T>}
63  */
64 export const findAllRenderedComponentsWithTestId = (tree, testId) => {
65         return ReactTestUtils.findAllInRenderedTree(tree, component => component.props.testId === testId);
66 };
67
68 /**
69  * Finds all instance of components in the rendered tree that are DOM
70  * components with the data-test-id
71  * @return {array} an array of all the matches.
72  */
73 export const scryRenderedDOMComponentsWithTestId = (root, testId) => {
74         return ReactTestUtils.findAllInRenderedTree(root, function (inst) {
75                 if (ReactTestUtils.isDOMComponent(inst)) {
76                         var compTestId = inst.getAttribute('data-test-id');
77                         return compTestId === testId;
78                 }
79                 return false;
80         });
81 };