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

get.js

Source: get.js Github

copy
1import Vue from 'vue';
2import { DEFAULT_COMPONENT_VALUE } from '../constants';
3import { isSimilarNum, isStr, isArr, isFunc } from './is';
4
5export function getEmptyArray() {
6  return [];
7}
8
9export function getEmptyObject() {
10  return {};
11}
12
13export function getValidator(arr = []) {
14  return function(val) {
15    return arr.includes(val);
16  };
17}
18
19export function getSizePx(size) {
20  const sizeIsNum = isSimilarNum(size);
21  if (sizeIsNum || isStr(size)) {
22    return sizeIsNum ? `${size}px` : size;
23  }
24  return size;
25}
26
27export function getCamelCase(str) {
28  const splitArr = str.split('-');
29  if (str.length <= 1) {
30    return str;
31  }
32  const pascalCaseArr = [];
33  const camelCaseArr = splitArr.map((splitItem, index) => {
34    const result = splitItem.slice(0, 1).toUpperCase() + splitItem.slice(1);
35    pascalCaseArr.push(index === 0 ? splitItem : result);
36    return result;
37  });
38  return {
39    camelCase: camelCaseArr.join(''),
40    pascalCase: pascalCaseArr.join(''),
41  };
42}
43
44export function getParamCase(str) {
45  return str.replace(/([A-Z])/g, (all, letter, index) => {
46    if (index === 0) {
47      return letter;
48    }
49    return `-${letter}`;
50  }).toLowerCase();
51}
52
53
54function getPropDefaultValue(prop) {
55  if (isFunc(prop)) {
56    return prop();
57  }
58  if (isArr(prop)) {
59    return prop?.[0]?.();
60  }
61  if (typeof prop.default !== 'undefined') {
62    return isFunc(prop.default) ? prop.default() : prop.default;
63  }
64  if (prop?.type) {
65    return getPropDefaultValue(prop.type);
66  }
67  return DEFAULT_COMPONENT_VALUE;
68}
69
70export function getDefaultValueByComponentType(component) {
71  if (isStr(component)) {
72    const components = Vue?.options?.components;
73    if (components[component]) {
74      return getDefaultValueByComponentType(components[component]?.options);
75    }
76    // 需要进行组件名转换
77    if (component.indexOf('-') !== -1) {
78      const { camelCase, pascalCase } = getCamelCase(component);
79      if (components[camelCase]) {
80        return getDefaultValueByComponentType(components[camelCase]?.options);
81      }
82      if (components[pascalCase]) {
83        return getDefaultValueByComponentType(components[pascalCase]?.options);
84      }
85    } else {
86      const paramCase = getParamCase(component);
87      if (components[paramCase]) {
88        return getDefaultValueByComponentType(components[paramCase]?.options);
89      }
90    }
91  } else {
92    const props = component?.props;
93    if (isArr(props)) {
94      return DEFAULT_COMPONENT_VALUE;
95    }
96    const value = props?.value;
97    return getPropDefaultValue(value);
98  }
99  return DEFAULT_COMPONENT_VALUE;
100}
101
Full Screen

props.js

Source: props.js Github

copy
1import { hasOwn } from "../../shared/util";
2import { observe } from "../observer";
3
4export function validateProp(key, propOptions, propsData, vm) {
5  // prop的配置
6  const prop = propOptions[key]
7  const absent = !hasOwn(propsData, key)
8  // 动态设置的属性值
9  let value = propsData[key]
10
11  // 检查属性设置的type是否为boolean  这里对布尔值进行处理
12  const booleanIndex = getTypeIndex(Boolean, prop.type)
13  if (booleanIndex > -1) {
14    // 如果属性没有传值  也没有设置默认值  则设置为false
15    if (absent && !hasOwn(prop, 'default')) {
16      value = false
17    }
18  }
19  // 这里检测是否有默认值 有就返回默认值
20  if (value === undefined) {
21    value = getPropDefaultValue(vm, prop, key)
22
23    observe(value)
24  }
25
26  return value
27}
28
29function getType(fn) {
30  const match = fn && fn.toString().match(/^\s*function (\w+)/)
31  return match ? match[1] : ''
32}
33
34function getPropDefaultValue(vm, prop, key) {
35  //没有配置默认值 直接返回undefined
36  if (!hasOwn(prop, 'default')) {
37    return undefined
38  }
39  // 取出默认值选项
40  const def = prop.default
41  // 如果当前的数据中没有值  但是之前存储的数据中有值  则使用之前的值
42  if (vm && vm.$options.propsData && vm.$options.propsData[key] === undefined && vm._props[key] !== undefined) {
43    return vm._props[key]
44  }
45  // 因为对象的默认值需要设置一个函数,所以这里处理函数
46  return typeof def === 'function' && getType(prop.type) !== 'Function' ? def.call(vm) : def
47}
48
49function isSameType(a, b) {
50  return getType(a) === getType(b)
51}
52
53/**
54 * 判断类型是否符合要求
55 * @param {String} type 
56 * @param {String | Array} expectedTypes 
57 */
58function getTypeIndex(type, expectedTypes) {
59  if (!Array.isArray(expectedTypes)) {
60    return isSameType(expectedTypes,type) ? 0 : -1
61  }
62  for (let i = 0,len = expectedTypes.length; i < len; i++) {
63    if (isSameType(expectedTypes[i], type)) {
64      return i
65    }
66  }
67  return -1
68}
69
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)