AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 12 | * 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import deepFreeze from 'deep-freeze'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 17 | import times from 'lodash/times'; |
| 18 | import pick from 'lodash/pick'; |
| 19 | import intersection from 'lodash/intersection'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 20 | import ReactTestUtils from 'react-addons-test-utils'; |
| 21 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 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 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame^] | 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 | }; |