How to use getMaskedContext 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.

ReactFiberClassComponent.js

Source: ReactFiberClassComponent.js Github

copy
1/**
2 * Copyright 2013-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 * @providesModule ReactFiberClassComponent
10 * @flow
11 */
12
13'use strict';
14
15import type { Fiber } from 'ReactFiber';
16import type { UpdateQueue } from 'ReactFiberUpdateQueue';
17
18var {
19  getMaskedContext,
20} = require('ReactFiberContext');
21var {
22  createUpdateQueue,
23  addToQueue,
24  addCallbackToQueue,
25  mergeUpdateQueue,
26} = require('ReactFiberUpdateQueue');
27var { getComponentName, isMounted } = require('ReactFiberTreeReflection');
28var ReactInstanceMap = require('ReactInstanceMap');
29var shallowEqual = require('shallowEqual');
30var warning = require('warning');
31var invariant = require('invariant');
32
33const isArray = Array.isArray;
34
35module.exports = function(scheduleUpdate : (fiber: Fiber) => void) {
36
37  function scheduleUpdateQueue(fiber: Fiber, updateQueue: UpdateQueue) {
38    fiber.updateQueue = updateQueue;
39    // Schedule update on the alternate as well, since we don't know which tree
40    // is current.
41    if (fiber.alternate) {
42      fiber.alternate.updateQueue = updateQueue;
43    }
44    scheduleUpdate(fiber);
45  }
46
47  // Class component state updater
48  const updater = {
49    isMounted,
50    enqueueSetState(instance, partialState) {
51      const fiber = ReactInstanceMap.get(instance);
52      const updateQueue = fiber.updateQueue ?
53        addToQueue(fiber.updateQueue, partialState) :
54        createUpdateQueue(partialState);
55      scheduleUpdateQueue(fiber, updateQueue);
56    },
57    enqueueReplaceState(instance, state) {
58      const fiber = ReactInstanceMap.get(instance);
59      const updateQueue = createUpdateQueue(state);
60      updateQueue.isReplace = true;
61      scheduleUpdateQueue(fiber, updateQueue);
62    },
63    enqueueForceUpdate(instance) {
64      const fiber = ReactInstanceMap.get(instance);
65      const updateQueue = fiber.updateQueue || createUpdateQueue(null);
66      updateQueue.isForced = true;
67      scheduleUpdateQueue(fiber, updateQueue);
68    },
69    enqueueCallback(instance, callback) {
70      const fiber = ReactInstanceMap.get(instance);
71      let updateQueue = fiber.updateQueue ?
72        fiber.updateQueue :
73        createUpdateQueue(null);
74      addCallbackToQueue(updateQueue, callback);
75      scheduleUpdateQueue(fiber, updateQueue);
76    },
77  };
78
79  function checkShouldComponentUpdate(workInProgress, oldProps, newProps, newState, newContext) {
80    const updateQueue = workInProgress.updateQueue;
81    if (oldProps === null || (updateQueue && updateQueue.isForced)) {
82      return true;
83    }
84
85    const instance = workInProgress.stateNode;
86    if (typeof instance.shouldComponentUpdate === 'function') {
87      const shouldUpdate = instance.shouldComponentUpdate(newProps, newState, newContext);
88
89      if (__DEV__) {
90        warning(
91          shouldUpdate !== undefined,
92          '%s.shouldComponentUpdate(): Returned undefined instead of a ' +
93          'boolean value. Make sure to return true or false.',
94          getComponentName(workInProgress)
95        );
96      }
97
98      return shouldUpdate;
99    }
100
101    const type = workInProgress.type;
102    if (type.prototype && type.prototype.isPureReactComponent) {
103      return (
104        !shallowEqual(oldProps, newProps) ||
105        !shallowEqual(instance.state, newState)
106      );
107    }
108
109    return true;
110  }
111
112  function checkClassInstance(workInProgress: Fiber) {
113    const instance = workInProgress.stateNode;
114    if (__DEV__) {
115      const name = getComponentName(workInProgress);
116      const renderPresent = instance.render;
117      warning(
118        renderPresent,
119        '%s(...): No `render` method found on the returned component ' +
120        'instance: you may have forgotten to define `render`.',
121        name
122      );
123      const noGetInitialStateOnES6 = (
124        !instance.getInitialState ||
125        instance.getInitialState.isReactClassApproved
126      );
127      warning(
128        noGetInitialStateOnES6,
129        'getInitialState was defined on %s, a plain JavaScript class. ' +
130        'This is only supported for classes created using React.createClass. ' +
131        'Did you mean to define a state property instead?',
132        name
133      );
134      const noGetDefaultPropsOnES6 = (
135        !instance.getDefaultProps ||
136        instance.getDefaultProps.isReactClassApproved
137      );
138      warning(
139        noGetDefaultPropsOnES6,
140        'getDefaultProps was defined on %s, a plain JavaScript class. ' +
141        'This is only supported for classes created using React.createClass. ' +
142        'Use a static property to define defaultProps instead.',
143        name
144      );
145      const noInstancePropTypes = !instance.propTypes;
146      warning(
147        noInstancePropTypes,
148        'propTypes was defined as an instance property on %s. Use a static ' +
149        'property to define propTypes instead.',
150        name,
151      );
152      const noInstanceContextTypes = !instance.contextTypes;
153      warning(
154        noInstanceContextTypes,
155        'contextTypes was defined as an instance property on %s. Use a static ' +
156        'property to define contextTypes instead.',
157        name,
158      );
159      const noComponentShouldUpdate = typeof instance.componentShouldUpdate !== 'function';
160      warning(
161        noComponentShouldUpdate,
162        '%s has a method called ' +
163        'componentShouldUpdate(). Did you mean shouldComponentUpdate()? ' +
164        'The name is phrased as a question because the function is ' +
165        'expected to return a value.',
166        name
167      );
168      const noComponentDidUnmount = typeof instance.componentDidUnmount !== 'function';
169      warning(
170        noComponentDidUnmount,
171        '%s has a method called ' +
172        'componentDidUnmount(). But there is no such lifecycle method. ' +
173        'Did you mean componentWillUnmount()?',
174        name
175      );
176      const noComponentWillRecieveProps = typeof instance.componentWillRecieveProps !== 'function';
177      warning(
178        noComponentWillRecieveProps,
179        '%s has a method called ' +
180        'componentWillRecieveProps(). Did you mean componentWillReceiveProps()?',
181        name
182      );
183    }
184
185    const state = instance.state;
186    if (state && (typeof state !== 'object' || isArray(state))) {
187      invariant(
188        false,
189        '%s.state: must be set to an object or null',
190        getComponentName(workInProgress)
191      );
192    }
193    if (typeof instance.getChildContext === 'function') {
194      invariant(
195        typeof workInProgress.type.childContextTypes === 'object',
196        '%s.getChildContext(): childContextTypes must be defined in order to ' +
197        'use getChildContext().',
198        getComponentName(workInProgress)
199      );
200    }
201  }
202
203  function adoptClassInstance(workInProgress : Fiber, instance : any) : void {
204    instance.updater = updater;
205    workInProgress.stateNode = instance;
206    // The instance needs access to the fiber so that it can schedule updates
207    ReactInstanceMap.set(instance, workInProgress);
208  }
209
210  function constructClassInstance(workInProgress : Fiber) : any {
211    const ctor = workInProgress.type;
212    const props = workInProgress.pendingProps;
213    const context = getMaskedContext(workInProgress);
214    const instance = new ctor(props, context);
215    adoptClassInstance(workInProgress, instance);
216    checkClassInstance(workInProgress);
217    return instance;
218  }
219
220  // Invokes the mount life-cycles on a previously never rendered instance.
221  function mountClassInstance(workInProgress : Fiber) : void {
222    const instance = workInProgress.stateNode;
223    const state = instance.state || null;
224
225    let props = workInProgress.pendingProps;
226    if (!props) {
227      throw new Error('There must be pending props for an initial mount.');
228    }
229
230    instance.props = props;
231    instance.state = state;
232    instance.context = getMaskedContext(workInProgress);
233
234    if (typeof instance.componentWillMount === 'function') {
235      instance.componentWillMount();
236      // If we had additional state updates during this life-cycle, let's
237      // process them now.
238      const updateQueue = workInProgress.updateQueue;
239      if (updateQueue) {
240        instance.state = mergeUpdateQueue(updateQueue, instance, state, props);
241      }
242    }
243  }
244
245  // Called on a preexisting class instance. Returns false if a resumed render
246  // could be reused.
247  function resumeMountClassInstance(workInProgress : Fiber) : boolean {
248    let newState = workInProgress.memoizedState;
249    let newProps = workInProgress.pendingProps;
250    if (!newProps) {
251      // If there isn't any new props, then we'll reuse the memoized props.
252      // This could be from already completed work.
253      newProps = workInProgress.memoizedProps;
254      if (!newProps) {
255        throw new Error('There should always be pending or memoized props.');
256      }
257    }
258    const newContext = getMaskedContext(workInProgress);
259
260    // TODO: Should we deal with a setState that happened after the last
261    // componentWillMount and before this componentWillMount? Probably
262    // unsupported anyway.
263
264    if (!checkShouldComponentUpdate(
265      workInProgress,
266      workInProgress.memoizedProps,
267      newProps,
268      newState,
269      newContext
270    )) {
271      return false;
272    }
273
274    // If we didn't bail out we need to construct a new instance. We don't
275    // want to reuse one that failed to fully mount.
276    const newInstance = constructClassInstance(workInProgress);
277    newInstance.props = newProps;
278    newInstance.state = newState = newInstance.state || null;
279    newInstance.context = getMaskedContext(workInProgress);
280
281    if (typeof newInstance.componentWillMount === 'function') {
282      newInstance.componentWillMount();
283    }
284    // If we had additional state updates, process them now.
285    // They may be from componentWillMount() or from error boundary's setState()
286    // during initial mounting.
287    const newUpdateQueue = workInProgress.updateQueue;
288    if (newUpdateQueue) {
289      newInstance.state = mergeUpdateQueue(newUpdateQueue, newInstance, newState, newProps);
290    }
291    return true;
292  }
293
294  // Invokes the update life-cycles and returns false if it shouldn't rerender.
295  function updateClassInstance(current : Fiber, workInProgress : Fiber) : boolean {
296    const instance = workInProgress.stateNode;
297
298    const oldProps = workInProgress.memoizedProps || current.memoizedProps;
299    let newProps = workInProgress.pendingProps;
300    if (!newProps) {
301      // If there aren't any new props, then we'll reuse the memoized props.
302      // This could be from already completed work.
303      newProps = oldProps;
304      if (!newProps) {
305        throw new Error('There should always be pending or memoized props.');
306      }
307    }
308    const oldContext = instance.context;
309    const newContext = getMaskedContext(workInProgress);
310
311    // Note: During these life-cycles, instance.props/instance.state are what
312    // ever the previously attempted to render - not the "current". However,
313    // during componentDidUpdate we pass the "current" props.
314
315    if (oldProps !== newProps || oldContext !== newContext) {
316      if (typeof instance.componentWillReceiveProps === 'function') {
317        instance.componentWillReceiveProps(newProps, newContext);
318      }
319    }
320
321    // Compute the next state using the memoized state and the update queue.
322    const updateQueue = workInProgress.updateQueue;
323    const oldState = workInProgress.memoizedState;
324    // TODO: Previous state can be null.
325    let newState;
326    if (updateQueue) {
327      if (!updateQueue.hasUpdate) {
328        newState = oldState;
329      } else {
330        newState = mergeUpdateQueue(updateQueue, instance, oldState, newProps);
331      }
332    } else {
333      newState = oldState;
334    }
335
336    if (oldProps === newProps &&
337        oldState === newState &&
338        oldContext === newContext &&
339        updateQueue && !updateQueue.isForced) {
340      return false;
341    }
342
343    if (!checkShouldComponentUpdate(
344      workInProgress,
345      oldProps,
346      newProps,
347      newState,
348      newContext
349    )) {
350      // TODO: Should this get the new props/state updated regardless?
351      return false;
352    }
353
354    if (typeof instance.componentWillUpdate === 'function') {
355      instance.componentWillUpdate(newProps, newState, newContext);
356    }
357
358    instance.props = newProps;
359    instance.state = newState;
360    instance.context = newContext;
361    return true;
362  }
363
364  return {
365    adoptClassInstance,
366    constructClassInstance,
367    mountClassInstance,
368    resumeMountClassInstance,
369    updateClassInstance,
370  };
371
372};
373
Full Screen

ContextUtils-test.js

Source: ContextUtils-test.js Github

copy
1/**
2 * @copyright 2016, Prometheus Research, LLC
3 */
4
5import assert from "assert";
6
7import { getMaskedContext, contextToParams } from "../ContextUtils";
8
9import { createEntity } from "../model/Entity";
10import { RecordType, RowType, anytype } from "../model/Type";
11
12describe("ContextUtils", function() {
13  describe("getMaskedContext", function() {
14    it("returns a piece of context described by type", function() {
15      let inputType = new RecordType({
16        a: new RowType("a", anytype),
17        b: new RowType("b", anytype)
18      });
19      assert.deepEqual(getMaskedContext({}, inputType), {});
20      assert.deepEqual(getMaskedContext({ a: 1, b: 2 }, inputType), {
21        a: 1,
22        b: 2
23      });
24      assert.deepEqual(getMaskedContext({ a: 1, b: 2, c: 3 }, inputType), {
25        a: 1,
26        b: 2
27      });
28      assert.deepEqual(getMaskedContext({ a: 1, b: null }, inputType), {
29        a: 1
30      });
31    });
32  });
33
34  describe("contextToParams", function() {
35    it("applies context to producible", function() {
36      let inputType = new RecordType({
37        value: new RowType("value", anytype),
38        entity: new RowType("entity", anytype)
39      });
40
41      assert.deepEqual(contextToParams({}, inputType), {
42        ":value": undefined,
43        ":entity": undefined
44      });
45
46      assert.deepEqual(contextToParams({}, inputType, { query: true }), {
47        value: undefined,
48        entity: undefined
49      });
50
51      assert.deepEqual(contextToParams({ value: 1 }, inputType), {
52        ":value": 1,
53        ":entity": undefined
54      });
55
56      assert.deepEqual(
57        contextToParams({ entity: createEntity("obj", 1) }, inputType),
58        {
59          ":value": undefined,
60          ":entity": 1
61        }
62      );
63
64      assert.deepEqual(contextToParams({ extra: "ok" }, inputType), {
65        ":value": undefined,
66        ":entity": undefined
67      });
68    });
69  });
70});
71
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)