github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/integration/components/attributes-table-test.js (about) 1 import { find, findAll, render } from '@ember/test-helpers'; 2 import { module, test } from 'qunit'; 3 import { setupRenderingTest } from 'ember-qunit'; 4 import hbs from 'htmlbars-inline-precompile'; 5 import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit'; 6 import flat from 'flat'; 7 8 const { flatten } = flat; 9 10 module('Integration | Component | attributes table', function(hooks) { 11 setupRenderingTest(hooks); 12 13 const commonAttributes = { 14 key: 'value', 15 nested: { 16 props: 'are', 17 supported: 'just', 18 fine: null, 19 }, 20 so: { 21 are: { 22 deeply: { 23 nested: 'properties', 24 like: 'these ones', 25 }, 26 }, 27 }, 28 }; 29 30 test('should render a row for each key/value pair in a deep object', async function(assert) { 31 this.set('attributes', commonAttributes); 32 await render(hbs`<AttributesTable @attributePairs={{attributes}} />`); 33 34 const rowsCount = Object.keys(flatten(commonAttributes)).length; 35 assert.equal( 36 this.element.querySelectorAll('[data-test-attributes-section] [data-test-value]').length, 37 rowsCount, 38 `Table has ${rowsCount} rows with values` 39 ); 40 41 await componentA11yAudit(this.element, assert); 42 }); 43 44 test('should render the full path of key/value pair from the root of the object', async function(assert) { 45 this.set('attributes', commonAttributes); 46 await render(hbs`<AttributesTable @attributePairs={{attributes}} />`); 47 48 assert.equal(find('[data-test-key]').textContent.trim(), 'key', 'Row renders the key'); 49 assert.equal(find('[data-test-value]').textContent.trim(), 'value', 'Row renders the value'); 50 51 const deepRow = findAll('[data-test-attributes-section]')[8]; 52 assert.equal( 53 deepRow.querySelector('[data-test-key]').textContent.trim(), 54 'so.are.deeply.nested', 55 'Complex row renders the full path to the key' 56 ); 57 assert.equal( 58 deepRow.querySelector('[data-test-prefix]').textContent.trim(), 59 'so.are.deeply.', 60 'The prefix is faded to put emphasis on the attribute' 61 ); 62 assert.equal(deepRow.querySelector('[data-test-value]').textContent.trim(), 'properties'); 63 }); 64 65 test('should render a row for key/value pairs even when the value is another object', async function(assert) { 66 this.set('attributes', commonAttributes); 67 await render(hbs`<AttributesTable @attributePairs={{attributes}} />`); 68 69 const countOfParentRows = countOfParentKeys(commonAttributes); 70 assert.equal( 71 findAll('[data-test-heading]').length, 72 countOfParentRows, 73 'Each key for a nested object gets a row with no value' 74 ); 75 }); 76 77 function countOfParentKeys(obj) { 78 return Object.keys(obj).reduce((count, key) => { 79 const value = obj[key]; 80 return isObject(value) ? count + 1 + countOfParentKeys(value) : count; 81 }, 0); 82 } 83 84 function isObject(value) { 85 return !Array.isArray(value) && value != null && typeof value === 'object'; 86 } 87 });