How to use buildSimulators method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

ReactTestUtils.js

Source: ReactTestUtils.js Github

copy
1/* */ 
2"use strict";
3var EventConstants = require("./EventConstants");
4var EventPluginHub = require("./EventPluginHub");
5var EventPropagators = require("./EventPropagators");
6var React = require("./React");
7var ReactElement = require("./ReactElement");
8var ReactBrowserEventEmitter = require("./ReactBrowserEventEmitter");
9var ReactMount = require("./ReactMount");
10var ReactTextComponent = require("./ReactTextComponent");
11var ReactUpdates = require("./ReactUpdates");
12var SyntheticEvent = require("./SyntheticEvent");
13var assign = require("./Object.assign");
14var topLevelTypes = EventConstants.topLevelTypes;
15function Event(suffix) {}
16var ReactTestUtils = {
17  renderIntoDocument: function(instance) {
18    var div = document.createElement('div');
19    return React.render(instance, div);
20  },
21  isElement: function(element) {
22    return ReactElement.isValidElement(element);
23  },
24  isElementOfType: function(inst, convenienceConstructor) {
25    return (ReactElement.isValidElement(inst) && inst.type === convenienceConstructor.type);
26  },
27  isDOMComponent: function(inst) {
28    return !!(inst && inst.mountComponent && inst.tagName);
29  },
30  isDOMComponentElement: function(inst) {
31    return !!(inst && ReactElement.isValidElement(inst) && !!inst.tagName);
32  },
33  isCompositeComponent: function(inst) {
34    return typeof inst.render === 'function' && typeof inst.setState === 'function';
35  },
36  isCompositeComponentWithType: function(inst, type) {
37    return !!(ReactTestUtils.isCompositeComponent(inst) && (inst.constructor === type.type));
38  },
39  isCompositeComponentElement: function(inst) {
40    if (!ReactElement.isValidElement(inst)) {
41      return false;
42    }
43    var prototype = inst.type.prototype;
44    return (typeof prototype.render === 'function' && typeof prototype.setState === 'function');
45  },
46  isCompositeComponentElementWithType: function(inst, type) {
47    return !!(ReactTestUtils.isCompositeComponentElement(inst) && (inst.constructor === type));
48  },
49  isTextComponent: function(inst) {
50    return inst instanceof ReactTextComponent.type;
51  },
52  findAllInRenderedTree: function(inst, test) {
53    if (!inst) {
54      return [];
55    }
56    var ret = test(inst) ? [inst] : [];
57    if (ReactTestUtils.isDOMComponent(inst)) {
58      var renderedChildren = inst._renderedChildren;
59      var key;
60      for (key in renderedChildren) {
61        if (!renderedChildren.hasOwnProperty(key)) {
62          continue;
63        }
64        ret = ret.concat(ReactTestUtils.findAllInRenderedTree(renderedChildren[key], test));
65      }
66    } else if (ReactTestUtils.isCompositeComponent(inst)) {
67      ret = ret.concat(ReactTestUtils.findAllInRenderedTree(inst._renderedComponent, test));
68    }
69    return ret;
70  },
71  scryRenderedDOMComponentsWithClass: function(root, className) {
72    return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
73      var instClassName = inst.props.className;
74      return ReactTestUtils.isDOMComponent(inst) && (instClassName && (' ' + instClassName + ' ').indexOf(' ' + className + ' ') !== -1);
75    });
76  },
77  findRenderedDOMComponentWithClass: function(root, className) {
78    var all = ReactTestUtils.scryRenderedDOMComponentsWithClass(root, className);
79    if (all.length !== 1) {
80      throw new Error('Did not find exactly one match for class:' + className);
81    }
82    return all[0];
83  },
84  scryRenderedDOMComponentsWithTag: function(root, tagName) {
85    return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
86      return ReactTestUtils.isDOMComponent(inst) && inst.tagName === tagName.toUpperCase();
87    });
88  },
89  findRenderedDOMComponentWithTag: function(root, tagName) {
90    var all = ReactTestUtils.scryRenderedDOMComponentsWithTag(root, tagName);
91    if (all.length !== 1) {
92      throw new Error('Did not find exactly one match for tag:' + tagName);
93    }
94    return all[0];
95  },
96  scryRenderedComponentsWithType: function(root, componentType) {
97    return ReactTestUtils.findAllInRenderedTree(root, function(inst) {
98      return ReactTestUtils.isCompositeComponentWithType(inst, componentType);
99    });
100  },
101  findRenderedComponentWithType: function(root, componentType) {
102    var all = ReactTestUtils.scryRenderedComponentsWithType(root, componentType);
103    if (all.length !== 1) {
104      throw new Error('Did not find exactly one match for componentType:' + componentType);
105    }
106    return all[0];
107  },
108  mockComponent: function(module, mockTagName) {
109    mockTagName = mockTagName || module.mockTagName || "div";
110    var ConvenienceConstructor = React.createClass({
111      displayName: "ConvenienceConstructor",
112      render: function() {
113        return React.createElement(mockTagName, null, this.props.children);
114      }
115    });
116    module.mockImplementation(ConvenienceConstructor);
117    module.type = ConvenienceConstructor.type;
118    module.isReactLegacyFactory = true;
119    return this;
120  },
121  simulateNativeEventOnNode: function(topLevelType, node, fakeNativeEvent) {
122    fakeNativeEvent.target = node;
123    ReactBrowserEventEmitter.ReactEventListener.dispatchEvent(topLevelType, fakeNativeEvent);
124  },
125  simulateNativeEventOnDOMComponent: function(topLevelType, comp, fakeNativeEvent) {
126    ReactTestUtils.simulateNativeEventOnNode(topLevelType, comp.getDOMNode(), fakeNativeEvent);
127  },
128  nativeTouchData: function(x, y) {
129    return {touches: [{
130        pageX: x,
131        pageY: y
132      }]};
133  },
134  Simulate: null,
135  SimulateNative: {}
136};
137function makeSimulator(eventType) {
138  return function(domComponentOrNode, eventData) {
139    var node;
140    if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
141      node = domComponentOrNode.getDOMNode();
142    } else if (domComponentOrNode.tagName) {
143      node = domComponentOrNode;
144    }
145    var fakeNativeEvent = new Event();
146    fakeNativeEvent.target = node;
147    var event = new SyntheticEvent(ReactBrowserEventEmitter.eventNameDispatchConfigs[eventType], ReactMount.getID(node), fakeNativeEvent);
148    assign(event, eventData);
149    EventPropagators.accumulateTwoPhaseDispatches(event);
150    ReactUpdates.batchedUpdates(function() {
151      EventPluginHub.enqueueEvents(event);
152      EventPluginHub.processEventQueue();
153    });
154  };
155}
156function buildSimulators() {
157  ReactTestUtils.Simulate = {};
158  var eventType;
159  for (eventType in ReactBrowserEventEmitter.eventNameDispatchConfigs) {
160    ReactTestUtils.Simulate[eventType] = makeSimulator(eventType);
161  }
162}
163var oldInjectEventPluginOrder = EventPluginHub.injection.injectEventPluginOrder;
164EventPluginHub.injection.injectEventPluginOrder = function() {
165  oldInjectEventPluginOrder.apply(this, arguments);
166  buildSimulators();
167};
168var oldInjectEventPlugins = EventPluginHub.injection.injectEventPluginsByName;
169EventPluginHub.injection.injectEventPluginsByName = function() {
170  oldInjectEventPlugins.apply(this, arguments);
171  buildSimulators();
172};
173buildSimulators();
174function makeNativeSimulator(eventType) {
175  return function(domComponentOrNode, nativeEventData) {
176    var fakeNativeEvent = new Event(eventType);
177    assign(fakeNativeEvent, nativeEventData);
178    if (ReactTestUtils.isDOMComponent(domComponentOrNode)) {
179      ReactTestUtils.simulateNativeEventOnDOMComponent(eventType, domComponentOrNode, fakeNativeEvent);
180    } else if (!!domComponentOrNode.tagName) {
181      ReactTestUtils.simulateNativeEventOnNode(eventType, domComponentOrNode, fakeNativeEvent);
182    }
183  };
184}
185var eventType;
186for (eventType in topLevelTypes) {
187  var convenienceName = eventType.indexOf('top') === 0 ? eventType.charAt(3).toLowerCase() + eventType.substr(4) : eventType;
188  ReactTestUtils.SimulateNative[convenienceName] = makeNativeSimulator(eventType);
189}
190module.exports = ReactTestUtils;
191
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)