跳到主要内容

ReactTestSelectors

该文件包含了测试用方法。

一、常数设定

以二进制数据制定了类型,并判定平台是否支持 Symbol ,若支持,使用 Symbol 保证唯一。

let COMPONENT_TYPE: symbol | number = 0b000;
let HAS_PSEUDO_CLASS_TYPE: symbol | number = 0b001;
let ROLE_TYPE: symbol | number = 0b010;
let TEST_NAME_TYPE: symbol | number = 0b011;
let TEXT_TYPE: symbol | number = 0b100;

if (typeof Symbol === 'function' && Symbol.for) {
const symbolFor = Symbol.for;
COMPONENT_TYPE = symbolFor('selector.component');
HAS_PSEUDO_CLASS_TYPE = symbolFor('selector.has_pseudo_class');
ROLE_TYPE = symbolFor('selector.role');
TEST_NAME_TYPE = symbolFor('selector.test_id');
TEXT_TYPE = symbolFor('selector.text');
}

二、包裹

使用函数,将特定的选择类型进行了与上面的类型进行了绑定。

export function createComponentSelector(
component: component(),
): ComponentSelector {
return {
$$typeof: COMPONENT_TYPE,
value: component,
};
}

export function createHasPseudoClassSelector(
selectors: Array<Selector>,
): HasPseudoClassSelector {
return {
$$typeof: HAS_PSEUDO_CLASS_TYPE,
value: selectors,
};
}

export function createRoleSelector(role: string): RoleSelector {
return {
$$typeof: ROLE_TYPE,
value: role,
};
}

export function createTextSelector(text: string): TextSelector {
return {
$$typeof: TEXT_TYPE,
value: text,
};
}

export function createTestNameSelector(id: string): TestNameSelector {
return {
$$typeof: TEST_NAME_TYPE,
value: id,
};
}
信息

好多内容需要先理解了 Fiber 才能看懂。