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