From 0237db8f3e65cfc89eff8f413718e866c91b043e Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 20 Sep 2024 11:52:22 -0700 Subject: [PATCH] [Tests] DRY up tests with some helpers --- __tests__/helpers/testForEach.js | 29 +++++++++++ __tests__/helpers/testIteration.js | 45 ++++++++++++++++ __tests__/src/ariaPropsMaps-test.js | 75 +++----------------------- __tests__/src/domMap-test.js | 73 +++----------------------- __tests__/src/elementRoleMap-test.js | 78 +++------------------------- __tests__/src/roleElementMap-test.js | 72 +++---------------------- __tests__/src/rolesMap-test.js | 75 +++----------------------- 7 files changed, 106 insertions(+), 341 deletions(-) create mode 100644 __tests__/helpers/testForEach.js create mode 100644 __tests__/helpers/testIteration.js diff --git a/__tests__/helpers/testForEach.js b/__tests__/helpers/testForEach.js new file mode 100644 index 0000000..4fb94ae --- /dev/null +++ b/__tests__/helpers/testForEach.js @@ -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(); + }); +} \ No newline at end of file diff --git a/__tests__/helpers/testIteration.js b/__tests__/helpers/testIteration.js new file mode 100644 index 0000000..9452cac --- /dev/null +++ b/__tests__/helpers/testIteration.js @@ -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(); + }); + } \ No newline at end of file diff --git a/__tests__/src/ariaPropsMaps-test.js b/__tests__/src/ariaPropsMaps-test.js index 1b5a3ab..6a4aa5c 100644 --- a/__tests__/src/ariaPropsMaps-test.js +++ b/__tests__/src/ariaPropsMaps-test.js @@ -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'}], @@ -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') @@ -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 = []; diff --git a/__tests__/src/domMap-test.js b/__tests__/src/domMap-test.js index e1da6b1..0463f51 100644 --- a/__tests__/src/domMap-test.js +++ b/__tests__/src/domMap-test.js @@ -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}], @@ -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') @@ -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(); }); diff --git a/__tests__/src/elementRoleMap-test.js b/__tests__/src/elementRoleMap-test.js index 3c8dfa1..ff705f7 100644 --- a/__tests__/src/elementRoleMap-test.js +++ b/__tests__/src/elementRoleMap-test.js @@ -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"]], @@ -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({ @@ -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(); }); diff --git a/__tests__/src/roleElementMap-test.js b/__tests__/src/roleElementMap-test.js index 374ff06..7d8ad72 100644 --- a/__tests__/src/roleElementMap-test.js +++ b/__tests__/src/roleElementMap-test.js @@ -5,6 +5,9 @@ import some from 'array.prototype.some'; import roleElementMap from 'aria-query/src/roleElementMap'; +import testIteration from '../helpers/testIteration'; +import testForEach from '../helpers/testForEach'; + const entriesList = [ ["article", [{"name": "article"}]], ["banner", [{"constraints": ["scoped to the body element"], "name": "header"}]], @@ -67,53 +70,11 @@ const entriesList = [ ]; 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'); - - st.test('supports the spread operator', async (s2t) => { - [...roleElementMap].forEach(([role, elements]) => { - const found = entriesList.filter(([r]) => r === role)[0]; - - s2t.ok(found, `spread has role: ${role}`); - s2t.ok(elements && typeof elements === 'object', `spread has object elements`) - }); - }); - - st.test('supports the for..of pattern', async (s2t) => { - const output = []; - for (const [key, value] of roleElementMap) { - output.push([key, value]); - } + const predicate = (role, [r]) => role === r; - output.forEach(([role, elements]) => { - const found = entriesList.filter(([r]) => r === role)[0]; + testIteration(t, roleElementMap, entriesList, 55, predicate); - s2t.ok(found, `for-of has role: ${role}`); - s2t.ok(elements && typeof elements === 'object', `for-of has object elements`) - }); - }); - }); - - t.test('forEach()', async (st) => { - const output = []; - let context; - roleElementMap.forEach((value, key, map) => { - output.push([key, value]); - if (!context) { - context = map; - } - }); - - for (let i = 0; i < output.length; i++) { - const [role, elements] = output[i]; - st.ok( - entriesList.filter(([r]) => r === role)[0], - `\`forEach\` has role: ${role}` - ); - st.ok(elements && typeof elements === 'object', `\`forEach\` has object elements`) - } - }); + testForEach(t, roleElementMap, entriesList, predicate); t.test('get()', (st) => { const map = roleElementMap.get('button'); @@ -140,26 +101,5 @@ test('roleElementMap API', (t) => { st.end(); }); - t.test('keys(), iteration', async (st) => { - const entriesKeys = entriesList.map(entry => entry[0]); - for (const key of roleElementMap.keys()) { - st.ok(entriesKeys.filter((k) => k === key)[0], `for-of has key: ${key}`); - } - - [...roleElementMap.keys()].forEach(([key]) => { - st.ok(entriesKeys.filter(([k]) => k === key)[0], `spread has key: ${key}`); - }); - }); - - t.test('values(), iteration', async (st) => { - for (const values of roleElementMap.values()) { - st.ok(some(entriesList, ([, x]) => deepEqual(x, values)), `for-of has object values: ${inspect(values)}`); - } - - [...roleElementMap.values()].forEach((values) => { - st.ok(some(entriesList, ([, x]) => deepEqual(x, values)), `spread has object values: ${inspect(values)}`); - }); - }); - t.end(); }); \ No newline at end of file diff --git a/__tests__/src/rolesMap-test.js b/__tests__/src/rolesMap-test.js index be7f49a..cbda919 100644 --- a/__tests__/src/rolesMap-test.js +++ b/__tests__/src/rolesMap-test.js @@ -1,9 +1,11 @@ import test from 'tape'; -import inspect from 'object-inspect'; import rolesMap from 'aria-query/src/rolesMap'; import ariaPropsMap from 'aria-query/src/ariaPropsMap'; +import testIteration from '../helpers/testIteration'; +import testForEach from '../helpers/testForEach'; + const entriesList = [ ["alert", null], ["alertdialog", null], @@ -147,53 +149,11 @@ const entriesList = [ ]; 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'); - - st.test('supports the spread operator', async (s2t) => { - [...rolesMap].forEach(([role, elements]) => { - const found = entriesList.filter(([r]) => r === role)[0]; - - s2t.ok(found, `spread has role: ${role}`); - s2t.ok(elements && typeof elements === 'object', `spread has object elements`) - }); - }); - - st.test('supports the for..of pattern', async (s2t) => { - const output = []; - for (const [key, value] of rolesMap) { - output.push([key, value]); - } - - output.forEach(([role, elements]) => { - const found = entriesList.filter(([r]) => r === role)[0]; - - s2t.ok(found, `for-of has role: ${role}`); - s2t.ok(elements && typeof elements === 'object', `for-of has object elements`) - }); - }); - }); + const predicate = (role, [r]) => role === r; - t.test('forEach()', async (st) => { - const output = []; - let context; - rolesMap.forEach((value, key, map) => { - output.push([key, value]); - if (!context) { - context = map; - } - }); - - for (let i = 0; i < output.length; i++) { - const [role, elements] = output[i]; - st.ok( - entriesList.filter(([r]) => r === role)[0], - `\`forEach\` has role: ${role}` - ); - st.ok(elements && typeof elements === 'object', `\`forEach\` has object elements`) - } - }); + testIteration(t, rolesMap, entriesList, 139, predicate); + + testForEach(t, rolesMap, entriesList, predicate); t.test('get()', (st) => { st.notEqual(rolesMap.get('roletype'), undefined, 'has a defined role') @@ -209,27 +169,6 @@ test('rolesMap API', (t) => { st.end(); }); - t.test('keys(), iteration', async (st) => { - const entriesKeys = entriesList.map(entry => entry[0]); - for (const key of rolesMap.keys()) { - st.ok(entriesKeys.filter((k) => k === key)[0], `for-of has key: ${key}`); - } - - [...rolesMap.keys()].forEach(([key]) => { - st.ok(entriesKeys.filter(([k]) => k === key)[0], `spread has key: ${key}`); - }); - }); - - t.test('values(), iteration', async (st) => { - for (const values of rolesMap.values()) { - st.ok(values && typeof values === 'object', `for-of has object values: ${inspect(values)}`); - } - - [...rolesMap.values()].forEach((values) => { - st.ok(values && typeof values === 'object', `spread has object values: ${inspect(values)}`); - }); - }); - t.test('props and ariaPropsMap', (st) => { const roles = rolesMap.entries();