blob: 415736fa606915c6269f445f36b82b523bbdd69c [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import deepFreeze from 'deep-freeze';
AviZi280f8012017-06-09 02:39:56 +030017import times from 'lodash/times';
18import pick from 'lodash/pick';
19import intersection from 'lodash/intersection';
Michael Landoefa037d2017-02-19 12:57:33 +020020import ReactTestUtils from 'react-addons-test-utils';
21
AviZi280f8012017-06-09 02:39:56 +030022export const buildListFromFactory = (factory, quantity = 3, overrides) => {
23 let list = [];
24 times(quantity, () =>{
25 list.push(factory.build(overrides));
26 });
27 return list;
28};
29
30export 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
Michael Landoefa037d2017-02-19 12:57:33 +020036//returned object should be treated as immutable.
37export 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 */
64export const findAllRenderedComponentsWithTestId = (tree, testId) => {
65 return ReactTestUtils.findAllInRenderedTree(tree, component => component.props.testId === testId);
66};
67
AviZi280f8012017-06-09 02:39:56 +030068/**
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 */
73export 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};