Skip to content

Commit

Permalink
[Tests] DRY up tests with some helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 20, 2024
1 parent 28791c4 commit 0237db8
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 341 deletions.
29 changes: 29 additions & 0 deletions __tests__/helpers/testForEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import inspect from 'object-inspect';

export default function testForEach(t, map, entriesList, predicate) {
t.test('forEach()', (st) => {
const output = [];
let context;
map.forEach((value, key, map) => {
output.push([key, value]);
if (!context) {
context = map;
}
});

for (let i = 0; i < output.length; i++) {
const [obj, values] = output[i];
const found = entriesList.filter((entry) => predicate(obj, entry))[0];

st.ok(found, `\`forEach\` has element: ${inspect(obj)}`);

if (values && found && found[1]) {
st.deepEqual(values, found[1], `\`forEach\` has object elements`);
} else {
st.ok(values && typeof values === 'object', `\`forEach\` has object elements`)
}
}

st.end();
});
}
45 changes: 45 additions & 0 deletions __tests__/helpers/testIteration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import inspect from 'object-inspect';

export default function testIteration(t, map, entriesList, expectedLength, predicate) {
t.test('iteration', { skip: typeof Symbol === 'undefined' }, (st) => {
st.notEqual(map[Symbol.iterator], undefined, 'has an iterator defined');
st.equal([...map].length, expectedLength, 'has a specific length');

st.test('supports the spread operator', (s2t) => {
[...map].forEach(([obj, values]) => {
const found = entriesList.filter((entry) => predicate(obj, entry))[0];

s2t.ok(found, `spread has element: ${inspect(obj)}`);
if (values && found && found[1]) {
s2t.deepEqual(values, found[1], `spread has object elements`);
} else {
s2t.ok(values && typeof values === 'object', 'spread has object elements')
}
});

s2t.end();
});

st.test('supports the for..of pattern', (s2t) => {
const output = [];
for (const [key, value] of map) {
output.push([key, value]);
}

output.forEach(([obj, values]) => {
const found = entriesList.filter((entry) => predicate(obj, entry))[0];

s2t.ok(found, `for-of has element: ${inspect(obj)}`);
if (values && found && found[1]) {
s2t.deepEqual(values, found[1], `for-of has object elements`);
} else {
s2t.ok(values && typeof values === 'object', 'for-of has object elements')
}
});

s2t.end();
});

st.end();
});
}
75 changes: 7 additions & 68 deletions __tests__/src/ariaPropsMaps-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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';

import testIteration from '../helpers/testIteration';
import testForEach from '../helpers/testForEach';

const entriesList = [
['aria-activedescendant', {'type': 'id'}],
['aria-atomic', {'type': 'boolean'}],
Expand Down Expand Up @@ -61,52 +62,11 @@ const entriesList = [
];

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');

st.test('supports the spread operator', async (s2t) => {
[...ariaPropsMap].forEach(([obj, roles]) => {
const found = entriesList.filter(([o]) => deepEqual(o, obj))[0];

s2t.ok(found, `spread has element: ${inspect(obj)}`);
s2t.deepEqual(roles, found[1], `for-of has object elements`);
});
});

st.test('supports the for..of pattern', async (s2t) => {
const output = [];
for (const [key, value] of ariaPropsMap) {
output.push([key, value]);
}

output.forEach(([obj, roles]) => {
const found = entriesList.filter(([o]) => deepEqual(o, obj))[0];

s2t.ok(found, `for-of has element: ${inspect(obj)}`);
s2t.deepEqual(roles, found[1], `for-of has object elements`);
});
});
});

t.test('forEach()', async (st) => {
const output = [];
let context;
ariaPropsMap.forEach((value, key, map) => {
output.push([key, value]);
if (!context) {
context = map;
}
});
const predicate = (obj, [o]) => deepEqual(o, obj);

testIteration(t, ariaPropsMap, entriesList, 51, predicate);

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`);
}
});
testForEach(t, ariaPropsMap, entriesList, predicate);

t.test('get()', (st) => {
st.notEqual(ariaPropsMap.get('aria-label'), undefined, 'has a defined prop')
Expand All @@ -121,27 +81,6 @@ test('ariaPropsMap API', (t) => {

st.end();
});

t.test('keys(), iteration', async (st) => {
const entriesKeys = entriesList.map(entry => entry[0]);
for (const obj of ariaPropsMap.keys()) {
st.ok(entriesKeys.filter((k) => deepEqual(k, obj))[0], `for-of has key: ${inspect(obj)}`);
}

[...ariaPropsMap.keys()].forEach((obj) => {
st.ok(entriesKeys.filter((k) => deepEqual(k, obj))[0], `spread has key: ${inspect(obj)}`);
});
});

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

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

t.test('props and role defintions', (st) => {
const usedProps = [];
Expand Down
73 changes: 6 additions & 67 deletions __tests__/src/domMap-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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';

import testIteration from '../helpers/testIteration';
import testForEach from '../helpers/testForEach';

const entriesList = [
["a", {"reserved": false}],
["abbr", {"reserved": false}],
Expand Down Expand Up @@ -138,52 +139,11 @@ const entriesList = [
];

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');

st.test('supports the spread operator', async (s2t) => {
[...domMap].forEach(([obj, roles]) => {
const found = entriesList.filter(([o]) => deepEqual(o, obj))[0];

s2t.ok(found, `spread has element: ${inspect(obj)}`);
s2t.deepEqual(roles, found[1], `for-of has object elements`);
});
});

st.test('supports the for..of pattern', async (s2t) => {
const output = [];
for (const [key, value] of domMap) {
output.push([key, value]);
}
const predicate = (obj, [o]) => deepEqual(o, obj);

output.forEach(([obj, roles]) => {
const found = entriesList.filter(([o]) => deepEqual(o, obj))[0];
testIteration(t, domMap, entriesList, 129, predicate);

s2t.ok(found, `for-of has element: ${inspect(obj)}`);
s2t.deepEqual(roles, found[1], `for-of has object elements`);
});
});
});

t.test('forEach()', async (st) => {
const output = [];
let context;
domMap.forEach((value, key, map) => {
output.push([key, value]);
if (!context) {
context = map;
}
});

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`);
}
});
testForEach(t, domMap, entriesList, predicate);

t.test('get()', (st) => {
st.notEqual(domMap.get('a'), undefined, 'has a defined element')
Expand All @@ -199,26 +159,5 @@ test('domMap API', (t) => {
st.end();
});

t.test('keys(), iteration', async (st) => {
const entriesKeys = entriesList.map(entry => entry[0]);
for (const obj of domMap.keys()) {
st.ok(entriesKeys.filter((k) => deepEqual(k, obj))[0], `for-of has key: ${inspect(obj)}`);
}

[...domMap.keys()].forEach((obj) => {
st.ok(entriesKeys.filter((k) => deepEqual(k, obj))[0], `spread has key: ${inspect(obj)}`);
});
});

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

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

t.end();
});
78 changes: 6 additions & 72 deletions __tests__/src/elementRoleMap-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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';

import testIteration from '../helpers/testIteration';
import testForEach from '../helpers/testForEach';

const entriesList = [
[{"name": "article"}, ["article"]],
[{"constraints": ["scoped to the body element"], "name": "header"}, ["banner"]],
Expand Down Expand Up @@ -127,55 +129,12 @@ const entriesList = [
];

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');

st.test('supports the spread operator', async (s2t) => {
[...elementRoleMap].forEach(([obj, roles]) => {
const found = entriesList.filter(([o]) => deepEqual(o, obj))[0];

s2t.ok(found, `spread has element: ${inspect(obj)}`);
s2t.deepEqual(roles, found[1], `for-of has object elements`);
});
});

st.test('supports the for..of pattern', async (s2t) => {
const output = [];
for (const [key, value] of elementRoleMap) {
output.push([key, value]);
}

output.forEach(([obj, roles]) => {
const found = entriesList.filter(([o]) => deepEqual(o, obj))[0];
const predicate = (obj, [o]) => deepEqual(o, obj);

s2t.ok(found, `for-of has element: ${inspect(obj)}`);
s2t.deepEqual(roles, found[1], `for-of has object elements`);
});
});
});

t.test('forEach()', (st) => {
const output = [];
let context;
elementRoleMap.forEach((value, key, map) => {
output.push([key, value]);
if (!context) {
context = map;
}
});
testIteration(t, elementRoleMap, entriesList, 112, predicate);

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`);
}
testForEach(t, elementRoleMap, entriesList, predicate);

st.end();
});

t.test('get()', (st) => {
st.ok(some(
elementRoleMap.get({
Expand Down Expand Up @@ -236,30 +195,5 @@ test('elementRoleMap API', (t) => {
st.end();
});

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)}`);
}

[...elementRoleMap.keys()].forEach((obj) => {
st.ok(entriesKeys.filter((k) => deepEqual(k, obj))[0], `spread has key: ${inspect(obj)}`);
});

st.end();
});

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

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

st.end();
});

t.end();
});
Loading

0 comments on commit 0237db8

Please sign in to comment.