Skip to content

Commit

Permalink
[Tests] handle things missing in older envs
Browse files Browse the repository at this point in the history
 - node < 4 lacks `Array.from`
 - node < 1 lacks `Array#some`
 - node <= 0.12 lacks `Array#includes`
 - node <= 0.10 lacks `Symbol.iterator`
 - avoid `async`/`await` unless strictly necessary
  • Loading branch information
ljharb committed Sep 15, 2024
1 parent 2106d5e commit 28791c4
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 46 deletions.
21 changes: 15 additions & 6 deletions __tests__/src/ariaPropsMaps-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'tape';
import deepEqual from 'deep-equal-json';
import inspect from 'object-inspect';
import some from 'array.prototype.some';

import ariaPropsMap from 'aria-query/src/ariaPropsMap';
import rolesMap from 'aria-query/src/rolesMap';
Expand Down Expand Up @@ -59,7 +60,7 @@ const entriesList = [
['aria-valuetext', {'type': 'string'}],
];

test('ariaPropsMap API', async (t) => {
test('ariaPropsMap API', (t) => {
t.test('iteration', async (st) => {
st.notEqual(ariaPropsMap[Symbol.iterator], undefined, 'has an iterator defined');
st.equal([...ariaPropsMap].length, 51, 'has a specific length');
Expand Down Expand Up @@ -107,14 +108,18 @@ test('ariaPropsMap API', async (t) => {
}
});

t.test('get()', async (st) => {
t.test('get()', (st) => {
st.notEqual(ariaPropsMap.get('aria-label'), undefined, 'has a defined prop')
st.equal(ariaPropsMap.get('fake prop'), undefined, 'returns undefined for a fake prop');

st.end();
});

t.test('has()', async (st) => {
t.test('has()', (st) => {
st.equal(ariaPropsMap.has('aria-label'), true, 'has a defined prop');
st.equal(ariaPropsMap.has('fake prop'), false, 'returns false for a fake prop');

st.end();
});

t.test('keys(), iteration', async (st) => {
Expand All @@ -130,15 +135,15 @@ test('ariaPropsMap API', async (t) => {

t.test('values(), iteration', async (st) => {
for (const values of ariaPropsMap.values()) {
st.ok(entriesList.some(([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`);
st.ok(some(entriesList, ([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`);
}

[...ariaPropsMap.values()].forEach((values) => {
st.ok(entriesList.some(([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`);
st.ok(some(entriesList, ([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`);
});
});

t.test('props and role defintions', async (st) => {
t.test('props and role defintions', (st) => {
const usedProps = [];
for (const roleDefinition of rolesMap.values()) {
for (const prop of Object.keys(roleDefinition.props)) {
Expand All @@ -158,5 +163,9 @@ test('ariaPropsMap API', async (t) => {
ariaPropsMap.forEach((value, key) => {
st.ok(usedProps.filter(p => p === key)[0], `has prop: ${key}`);
});

st.end();
});

t.end();
});
19 changes: 13 additions & 6 deletions __tests__/src/domMap-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'tape';
import deepEqual from 'deep-equal-json';
import inspect from 'object-inspect';
import some from 'array.prototype.some';

import domMap from 'aria-query/src/domMap';

Expand Down Expand Up @@ -136,7 +137,7 @@ const entriesList = [
["xmp", {"reserved": false}],
];

test('domMap API', async (t) => {
test('domMap API', (t) => {
t.test('iteration', async (st) => {
st.notEqual(domMap[Symbol.iterator], undefined, 'has an iterator defined');
st.equal([...domMap].length, 129, 'has a specific length');
Expand Down Expand Up @@ -178,20 +179,24 @@ test('domMap API', async (t) => {
for (let i = 0; i < output.length; i++) {
const [obj, roles] = output[i];
const found = entriesList.filter(([o]) => deepEqual(o, obj))[0];

st.ok(found, `\`forEach\` has element: ${inspect(obj)}`);
st.deepEqual(roles, found[1], `\`forEach\` has object elements`);
}
});

t.test('get()', async (st) => {
t.test('get()', (st) => {
st.notEqual(domMap.get('a'), undefined, 'has a defined element')
st.equal(domMap.get('fake element'), undefined, 'returns undefined for a fake element');

st.end();
});

t.test('has()', async (st) => {
t.test('has()', (st) => {
st.equal(domMap.has('a'), true, 'has a defined element');
st.equal(domMap.has('fake element'), false, 'returns false for a fake element');

st.end();
});

t.test('keys(), iteration', async (st) => {
Expand All @@ -207,11 +212,13 @@ test('domMap API', async (t) => {

t.test('values(), iteration', async (st) => {
for (const values of domMap.values()) {
st.ok(entriesList.some(([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`);
st.ok(some(entriesList, (([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`));
}

[...domMap.values()].forEach((values) => {
st.ok(entriesList.some(([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`);
st.ok(some(entriesList, (([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`));
});
});

t.end();
});
43 changes: 29 additions & 14 deletions __tests__/src/elementRoleMap-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'tape';
import deepEqual from 'deep-equal-json';
import inspect from 'object-inspect';
import some from 'array.prototype.some';

import elementRoleMap from 'aria-query/src/elementRoleMap';

Expand Down Expand Up @@ -125,7 +126,7 @@ const entriesList = [
[{"name": "time"}, ["time"]],
];

test('elementRoleMap API', async (t) => {
test('elementRoleMap API', (t) => {
t.test('iteration', async (st) => {
st.notEqual(elementRoleMap[Symbol.iterator], undefined, 'has an iterator defined');
st.equal([...elementRoleMap].length, 112, 'has a specific length');
Expand Down Expand Up @@ -154,7 +155,7 @@ test('elementRoleMap API', async (t) => {
});
});

t.test('forEach()', async (st) => {
t.test('forEach()', (st) => {
const output = [];
let context;
elementRoleMap.forEach((value, key, map) => {
Expand All @@ -171,28 +172,32 @@ test('elementRoleMap API', async (t) => {
st.ok(found, `\`forEach\` has element: ${inspect(obj)}`);
st.deepEqual(roles, found[1], `\`forEach\` has object elements`);
}

st.end();
});

t.test('get()', async (st) => {
st.ok(
t.test('get()', (st) => {
st.ok(some(
elementRoleMap.get({
attributes: [
{ constraints: ["set"], name: 'href' }
],
name: 'a'
}).some(x => x.includes('link')),
);
}),
x => x.indexOf('link') >= 0
));

st.ok(
st.ok(some(
elementRoleMap.get({
"attributes": [
{
"name": "type",
"value": "radio"
}
], "name": "input"
}).some(x => x.includes('radio')),
);
}),
x => x.indexOf('radio') >= 0
));

st.equal(
elementRoleMap.get({
Expand All @@ -203,9 +208,11 @@ test('elementRoleMap API', async (t) => {
}),
undefined,
);

st.end();
});

t.test('has()', async (st) => {
t.test('has()', (st) => {
st.equal(
elementRoleMap.has({
attributes: [
Expand All @@ -225,9 +232,11 @@ test('elementRoleMap API', async (t) => {
}),
false,
);

st.end();
});

t.test('keys(), iteration', async (st) => {
t.test('keys(), iteration', (st) => {
const entriesKeys = entriesList.map(entry => entry[0]);
for (const obj of elementRoleMap.keys()) {
st.ok(entriesKeys.filter((k) => deepEqual(k, obj))[0], `for-of has key: ${inspect(obj)}`);
Expand All @@ -236,15 +245,21 @@ test('elementRoleMap API', async (t) => {
[...elementRoleMap.keys()].forEach((obj) => {
st.ok(entriesKeys.filter((k) => deepEqual(k, obj))[0], `spread has key: ${inspect(obj)}`);
});

st.end();
});

t.test('values(), iteration', async (st) => {
t.test('values(), iteration', (st) => {
for (const values of elementRoleMap.values()) {
st.ok(entriesList.some(([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`);
st.ok(some(entriesList, (([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`));
}

[...elementRoleMap.values()].forEach((values) => {
st.ok(entriesList.some(([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`);
st.ok(some(entriesList, (([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`));
});

st.end();
});

t.end();
});
19 changes: 13 additions & 6 deletions __tests__/src/roleElementMap-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'tape';
import inspect from 'object-inspect';
import deepEqual from 'deep-equal-json';
import some from 'array.prototype.some';

import roleElementMap from 'aria-query/src/roleElementMap';

Expand Down Expand Up @@ -65,7 +66,7 @@ const entriesList = [
["time", [{"name": "time"}]],
];

test('roleElementMap API', async (t) => {
test('roleElementMap API', (t) => {
t.test('iteration', async (st) => {
st.notEqual(roleElementMap[Symbol.iterator], undefined, 'has an iterator defined');
st.equal([...roleElementMap].length, 55, 'has a specific length');
Expand Down Expand Up @@ -114,7 +115,7 @@ test('roleElementMap API', async (t) => {
}
});

t.test('get()', async (st) => {
t.test('get()', (st) => {
const map = roleElementMap.get('button');

[
Expand All @@ -124,15 +125,19 @@ test('roleElementMap API', async (t) => {
{"attributes": [{"name": "type", "value": "submit"}], "name": "input"},
{"name": "button"}
].forEach((element) => {
st.ok(map.some((e) => deepEqual(e, element)), `has element: ${inspect(element)}`);
st.ok(some(map, (e) => deepEqual(e, element)), `has element: ${inspect(element)}`);
});

st.equal(roleElementMap.get('fake role'), undefined, 'returns undefined for a fake role');

st.end();
});

t.test('has()', async (st) => {
t.test('has()', (st) => {
st.equal(roleElementMap.has('button'), true, 'has a defined role');
st.equal(roleElementMap.has('fake role'), false, 'returns false for a fake role');

st.end();
});

t.test('keys(), iteration', async (st) => {
Expand All @@ -148,11 +153,13 @@ test('roleElementMap API', async (t) => {

t.test('values(), iteration', async (st) => {
for (const values of roleElementMap.values()) {
st.ok(entriesList.some(([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`);
st.ok(some(entriesList, ([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`);
}

[...roleElementMap.values()].forEach((values) => {
st.ok(entriesList.some(([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`);
st.ok(some(entriesList, ([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`);
});
});

t.end();
});
25 changes: 17 additions & 8 deletions __tests__/src/rolesMap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const entriesList = [
["graphics-symbol", null],
];

test('rolesMap API', async (t) => {
test('rolesMap API', (t) => {
t.test('iteration', async (st) => {
st.notEqual(rolesMap[Symbol.iterator], undefined, 'has an iterator defined');
st.equal([...rolesMap].length, 139, 'has a specific length');
Expand Down Expand Up @@ -195,14 +195,18 @@ test('rolesMap API', async (t) => {
}
});

t.test('get()', async (st) => {
t.test('get()', (st) => {
st.notEqual(rolesMap.get('roletype'), undefined, 'has a defined role')
st.equal(rolesMap.get('fake role'), undefined, 'returns undefined for a fake role');

st.end();
});

t.test('has()', async (st) => {
t.test('has()', (st) => {
st.equal(rolesMap.has('roletype'), true, 'has a defined role');
st.equal(rolesMap.has('fake role'), false, 'returns false for a fake role');

st.end();
});

t.test('keys(), iteration', async (st) => {
Expand All @@ -226,21 +230,26 @@ test('rolesMap API', async (t) => {
});
});

t.test('props and ariaPropsMap', async (st) => {
t.test('props and ariaPropsMap', (st) => {
const roles = rolesMap.entries();

for (const [role, definition] of roles) {
const unknownProps = Object.keys(definition.props).filter(prop => !ariaPropsMap.has(prop));
st.deepEqual(unknownProps, [], `${role}: no unknown props`);
}

st.end();
});

// dpub-aria
t.test('doc-abstract role', async (st) => {
const abstract = rolesMap.get('doc-abstract');
const { props } = abstract;
t.test('doc-abstract role', (st) => {
const { props } = rolesMap.get('doc-abstract');

st.notOk('aria-describedat' in props, 'does not have aria-describedat property');
st.equal(props['aria-details'], null, 'has aria-details property');

st.end();
});

t.end();
});
Loading

0 comments on commit 28791c4

Please sign in to comment.