2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
22 export const buildListFromFactory = (factory, quantity = 3, overrides) => {
24 times(quantity, () =>{
25 list.push(factory.build(overrides));
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);
36 //returned object should be treated as immutable.
37 export const cloneAndSet = (obj, path, value) => {
38 let retVal = {...obj};
41 if (typeof path === 'string') {
42 path = path.split('.');
45 for (let i = 0; i < path.length - 1; i++) {
49 inner = inner[path[i]];
51 inner[path[path.length - 1]] = value;
52 return deepFreeze(retVal);
56 * array findAllRenderedDOMComponentsWithTestId(
60 * @param tree - ReactComponent
61 * @param testId - string
62 * @returns {Array.<T>}
64 export const findAllRenderedComponentsWithTestId = (tree, testId) => {
65 return ReactTestUtils.findAllInRenderedTree(tree, component => component.props.testId === testId);
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.
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;