|
| 1 | +import { module, test } from 'qunit'; |
| 2 | +import { setupRenderingTest } from 'ember-qunit'; |
| 3 | +import { click, render } from '@ember/test-helpers'; |
| 4 | +import hbs from 'htmlbars-inline-precompile'; |
| 5 | + |
| 6 | +module( |
| 7 | + 'Integration | Component | au-data-table-content-body', |
| 8 | + function (hooks) { |
| 9 | + setupRenderingTest(hooks); |
| 10 | + |
| 11 | + test('it renders', async function (assert) { |
| 12 | + // Set any properties with this.set('myProperty', 'value'); |
| 13 | + // Handle any actions with this.on('myAction', function(val) { ... }); |
| 14 | + await render(hbs`<AuDataTableContentBody />`); |
| 15 | + assert.dom('tbody').exists({ count: 1 }); |
| 16 | + }); |
| 17 | + |
| 18 | + test('display rows', async function (assert) { |
| 19 | + this.set('content', [ |
| 20 | + { firstName: 'John', lastName: 'Doe', age: 20 }, |
| 21 | + { firstName: 'Jane', lastName: 'Doe', age: 21 }, |
| 22 | + ]); |
| 23 | + this.set('dataTable', {}); |
| 24 | + this.set('dataTable.parsedFields', ['firstName', 'lastName', 'age']); |
| 25 | + this.set('dataTable.selection', []); |
| 26 | + |
| 27 | + await render( |
| 28 | + hbs`{{au-data-table-content-body content=this.content data-table=this.dataTable}}`, |
| 29 | + ); |
| 30 | + |
| 31 | + assert.dom('tr').exists({ count: 2 }, 'displays 2 rows'); |
| 32 | + assert |
| 33 | + .dom('tr:first-child td') |
| 34 | + .exists({ count: 3 }, 'displays 3 columns'); |
| 35 | + assert |
| 36 | + .dom('tr:first-child td:first-child') |
| 37 | + .hasText('John', 'displays firstName in first column'); |
| 38 | + assert |
| 39 | + .dom('tr:first-child td:nth-child(2)') |
| 40 | + .hasText('Doe', 'displays lastName in second column'); |
| 41 | + assert |
| 42 | + .dom('tr:first-child td:nth-child(3)') |
| 43 | + .hasText('20', 'displays age in third column'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('add checkboxes for selection if enabled', async function (assert) { |
| 47 | + const john = { firstName: 'John', lastName: 'Doe', age: 20 }; |
| 48 | + const jane = { firstName: 'Jane', lastName: 'Doe', age: 21 }; |
| 49 | + const jeff = { firstName: 'Jeff', lastName: 'Doe', age: 22 }; |
| 50 | + this.set('content', [john, jane, jeff]); |
| 51 | + this.set('data-table', {}); |
| 52 | + this.set('data-table.parsedFields', ['firstName', 'lastName', 'age']); |
| 53 | + this.set('data-table.selection', [jane]); |
| 54 | + |
| 55 | + await render( |
| 56 | + hbs`{{au-data-table-content-body content=this.content data-table=this.data-table enableSelection=true}}`, |
| 57 | + ); |
| 58 | + |
| 59 | + assert |
| 60 | + .dom('tr:first-child td') |
| 61 | + .exists({ count: 4 }, 'displays 4 columns'); |
| 62 | + assert.dom('tr.selected').exists({ count: 1 }, 'displays 1 selected row'); |
| 63 | + assert |
| 64 | + .dom('tr input[type="checkbox"]') |
| 65 | + .exists({ count: 3 }, 'displays a checkbox on each row'); |
| 66 | + assert |
| 67 | + .dom('tr input[type="checkbox"]:checked') |
| 68 | + .isChecked('displays 1 checked checkbox'); |
| 69 | + }); |
| 70 | + |
| 71 | + test('toggles selection if checkbox is clicked', async function (assert) { |
| 72 | + const john = { firstName: 'John', lastName: 'Doe', age: 20 }; |
| 73 | + const jane = { firstName: 'Jane', lastName: 'Doe', age: 21 }; |
| 74 | + const jeff = { firstName: 'Jeff', lastName: 'Doe', age: 22 }; |
| 75 | + this.set('content', [john, jane, jeff]); |
| 76 | + this.set('data-table', {}); |
| 77 | + this.set('data-table.parsedFields', ['firstName', 'lastName', 'age']); |
| 78 | + this.set('data-table.selection', [jane]); |
| 79 | + this.set('data-table.addItemToSelection', () => |
| 80 | + this.set('data-table.selection', [john, jane]), |
| 81 | + ); // mock function |
| 82 | + this.set('data-table.removeItemFromSelection', function () {}); // mock function |
| 83 | + |
| 84 | + await render( |
| 85 | + hbs`{{au-data-table-content-body content=this.content data-table=this.data-table enableSelection=true}}`, |
| 86 | + ); |
| 87 | + |
| 88 | + assert |
| 89 | + .dom('tr input[type="checkbox"]:checked') |
| 90 | + .isChecked('displays 1 checked checkbox before selecting a row'); |
| 91 | + |
| 92 | + await click('tr:first-child input[type="checkbox"]'); |
| 93 | + |
| 94 | + assert |
| 95 | + .dom('tr input[type="checkbox"]:checked') |
| 96 | + .isChecked('displays 2 checked checkboxes after selecting a row'); |
| 97 | + }); |
| 98 | + |
| 99 | + test('add line numbers if enabled', async function (assert) { |
| 100 | + const john = { firstName: 'John', lastName: 'Doe', age: 20 }; |
| 101 | + const jane = { firstName: 'Jane', lastName: 'Doe', age: 21 }; |
| 102 | + const jeff = { firstName: 'Jeff', lastName: 'Doe', age: 22 }; |
| 103 | + this.set('content', [john, jane, jeff]); |
| 104 | + this.set('data-table', {}); |
| 105 | + this.set('data-table.parsedFields', ['firstName', 'lastName', 'age']); |
| 106 | + this.set('data-table.selection', []); |
| 107 | + |
| 108 | + await render( |
| 109 | + hbs`{{au-data-table-content-body content=this.content data-table=this.data-table enableLineNumbers=true}}`, |
| 110 | + ); |
| 111 | + |
| 112 | + assert |
| 113 | + .dom('tr:first-child td') |
| 114 | + .exists({ count: 4 }, 'displays 4 columns'); |
| 115 | + assert |
| 116 | + .dom('tr:first-child td:first-child') |
| 117 | + .hasText('1', 'displays offset 1 on the first row'); |
| 118 | + assert |
| 119 | + .dom('tr:nth-child(2) td:first-child') |
| 120 | + .hasText('2', 'displays offset 2 on the second row'); |
| 121 | + assert |
| 122 | + .dom('tr:nth-child(3) td:first-child') |
| 123 | + .hasText('3', 'displays offset 3 on the third row'); |
| 124 | + |
| 125 | + this.set('data-table.page', 2); |
| 126 | + this.set('data-table.size', 5); |
| 127 | + await render( |
| 128 | + hbs`{{au-data-table-content-body content=this.content data-table=this.data-table enableLineNumbers=true}}`, |
| 129 | + ); |
| 130 | + |
| 131 | + assert |
| 132 | + .dom('tr:first-child td') |
| 133 | + .exists({ count: 4 }, 'displays 4 columns on page 3'); |
| 134 | + assert |
| 135 | + .dom('tr:first-child td:first-child') |
| 136 | + .hasText('11', 'displays offset 11 on the first row on page 3'); |
| 137 | + assert |
| 138 | + .dom('tr:nth-child(2) td:first-child') |
| 139 | + .hasText('12', 'displays offset 12 on the second row on page 3'); |
| 140 | + assert |
| 141 | + .dom('tr:nth-child(3) td:first-child') |
| 142 | + .hasText('13', 'displays offset 13 on the third row of page 3'); |
| 143 | + }); |
| 144 | + |
| 145 | + test('displays no data message if there is no data', async function (assert) { |
| 146 | + // Set any properties with this.set('myProperty', 'value'); |
| 147 | + // Handle any actions with this.on('myAction', function(val) { ... }); |
| 148 | + this.set('noDataMessage', 'No data'); |
| 149 | + this.set('data-table', {}); |
| 150 | + this.set('data-table.parsedFields', ['firstName', 'lastName', 'age']); |
| 151 | + this.set('data-table.selection', []); |
| 152 | + |
| 153 | + await render( |
| 154 | + hbs`{{au-data-table-content-body noDataMessage=this.noDataMessage data-table=this.data-table}}`, |
| 155 | + ); |
| 156 | + assert |
| 157 | + .dom('td.au-c-data-table__message') |
| 158 | + .exists({ count: 1 }, 'displays a no data message if no content') |
| 159 | + .hasText('No data', 'displays message "No data" if no content'); |
| 160 | + |
| 161 | + this.set('content', []); |
| 162 | + await render( |
| 163 | + hbs`{{au-data-table-content-body content=this.content noDataMessage=this.noDataMessage data-table=this.data-table}}`, |
| 164 | + ); |
| 165 | + assert |
| 166 | + .dom('td.au-c-data-table__message') |
| 167 | + .exists({ count: 1 }, 'displays a no data message if empty content'); |
| 168 | + assert |
| 169 | + .dom('td.au-c-data-table__message') |
| 170 | + .hasText('No data', 'displays message "No data" if empty content'); |
| 171 | + |
| 172 | + this.set('content', ['foo', 'bar']); |
| 173 | + await render( |
| 174 | + hbs`{{au-data-table-content-body content=this.content noDataMessage=this.noDataMessage data-table=this.data-table}}`, |
| 175 | + ); |
| 176 | + assert |
| 177 | + .dom('td.au-c-data-table__message') |
| 178 | + .doesNotExist('displays no message when there is content'); |
| 179 | + }); |
| 180 | + }, |
| 181 | +); |
0 commit comments