MCPcopy
hub / github.com/ampproject/worker-dom / testReflectedProperty

Function testReflectedProperty

src/test/reflectPropertiesHelper.ts:15–53  ·  view source on GitHub ↗
(propertyPair: PropertyPair, overrideValueToTest: string | boolean | number | null = null)

Source from the content-addressed store, hash-verified

13}
14
15export function testReflectedProperty(propertyPair: PropertyPair, overrideValueToTest: string | boolean | number | null = null) {
16 const propertyName = Object.keys(propertyPair)[0];
17 const defaultValue = propertyPair[propertyName][0];
18 const attributeName = propertyPair[propertyName][1] || propertyName.toLowerCase();
19 const keywords = propertyPair[propertyName][2];
20 const valueToTest = deriveValueToTest(overrideValueToTest !== null ? overrideValueToTest : defaultValue);
21
22 test(`${propertyName} should be ${defaultValue} by default`, (t) => {
23 const { element } = t.context;
24 t.is(element[propertyName], defaultValue);
25 });
26
27 test(`${propertyName} should be settable to a single value`, (t) => {
28 const { element } = t.context;
29 element[propertyName] = valueToTest;
30 t.is(element[propertyName], valueToTest);
31 });
32
33 test(`${propertyName} property change should be reflected in attribute`, (t) => {
34 const { element } = t.context;
35 element[propertyName] = valueToTest;
36 if (keywords) {
37 // Enumerated attrs have keywords instead of 'true' and 'false', e.g. translate="yes|no".
38 const keyword = valueToTest ? keywords[0] : keywords[1];
39 t.is(element.getAttribute(attributeName), keyword);
40 } else if (typeof valueToTest === 'boolean') {
41 // Boolean attributes only care about existence of the attribute, not their values.
42 t.is(element.hasAttribute(attributeName), valueToTest);
43 } else {
44 t.is(element.getAttribute(attributeName), String(valueToTest));
45 }
46 });
47
48 test(`${propertyName} attribute change should be reflected in property`, (t) => {
49 const { element } = t.context;
50 element.setAttribute(attributeName, String(valueToTest));
51 t.is(element[propertyName], valueToTest);
52 });
53}
54
55function deriveValueToTest(valueToTest: string | boolean | number): string | boolean | number {
56 switch (typeof valueToTest) {

Calls 4

deriveValueToTestFunction · 0.85
getAttributeMethod · 0.80
hasAttributeMethod · 0.80
setAttributeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…