open-nomad/ui/tests/integration/components/attributes-table-test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
3 KiB
JavaScript
Raw Normal View History

import { find, findAll, render } from '@ember/test-helpers';
2019-03-13 00:04:16 +00:00
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
2017-09-19 14:47:10 +00:00
import hbs from 'htmlbars-inline-precompile';
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
import flat from 'flat';
2017-09-19 14:47:10 +00:00
const { flatten } = flat;
2021-12-28 14:45:20 +00:00
module('Integration | Component | attributes table', function (hooks) {
2019-03-13 00:04:16 +00:00
setupRenderingTest(hooks);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
const commonAttributes = {
key: 'value',
nested: {
props: 'are',
supported: 'just',
fine: null,
},
so: {
are: {
deeply: {
nested: 'properties',
like: 'these ones',
},
2017-09-19 14:47:10 +00:00
},
},
2019-03-13 00:04:16 +00:00
};
2017-09-19 14:47:10 +00:00
2021-12-28 14:45:20 +00:00
test('should render a row for each key/value pair in a deep object', async function (assert) {
assert.expect(2);
2019-03-13 00:04:16 +00:00
this.set('attributes', commonAttributes);
await render(hbs`<AttributesTable @attributePairs={{attributes}} />`);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
const rowsCount = Object.keys(flatten(commonAttributes)).length;
assert.equal(
2021-12-28 16:08:12 +00:00
this.element.querySelectorAll(
'[data-test-attributes-section] [data-test-value]'
).length,
2019-03-13 00:04:16 +00:00
rowsCount,
`Table has ${rowsCount} rows with values`
);
await componentA11yAudit(this.element, assert);
2019-03-13 00:04:16 +00:00
});
2017-09-19 14:47:10 +00:00
2021-12-28 14:45:20 +00:00
test('should render the full path of key/value pair from the root of the object', async function (assert) {
2019-03-13 00:04:16 +00:00
this.set('attributes', commonAttributes);
await render(hbs`<AttributesTable @attributePairs={{attributes}} />`);
2017-09-19 14:47:10 +00:00
2021-12-28 16:08:12 +00:00
assert.equal(
find('[data-test-key]').textContent.trim(),
'key',
'Row renders the key'
);
assert.equal(
find('[data-test-value]').textContent.trim(),
'value',
'Row renders the value'
);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
const deepRow = findAll('[data-test-attributes-section]')[8];
assert.equal(
deepRow.querySelector('[data-test-key]').textContent.trim(),
'so.are.deeply.nested',
'Complex row renders the full path to the key'
);
assert.equal(
deepRow.querySelector('[data-test-prefix]').textContent.trim(),
'so.are.deeply.',
'The prefix is faded to put emphasis on the attribute'
);
2021-12-28 16:08:12 +00:00
assert.equal(
deepRow.querySelector('[data-test-value]').textContent.trim(),
'properties'
);
2019-03-13 00:04:16 +00:00
});
2017-09-19 14:47:10 +00:00
2021-12-28 14:45:20 +00:00
test('should render a row for key/value pairs even when the value is another object', async function (assert) {
2019-03-13 00:04:16 +00:00
this.set('attributes', commonAttributes);
await render(hbs`<AttributesTable @attributePairs={{attributes}} />`);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
const countOfParentRows = countOfParentKeys(commonAttributes);
assert.equal(
findAll('[data-test-heading]').length,
countOfParentRows,
'Each key for a nested object gets a row with no value'
);
});
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
function countOfParentKeys(obj) {
return Object.keys(obj).reduce((count, key) => {
const value = obj[key];
return isObject(value) ? count + 1 + countOfParentKeys(value) : count;
}, 0);
}
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
function isObject(value) {
return !Array.isArray(value) && value != null && typeof value === 'object';
}
});