1
0
Fork 0

Select all works as requirements specify

This commit is contained in:
Joe Wroten 2018-12-16 14:47:34 -06:00
parent 0fb5d4668b
commit d6d9569e8d
3 changed files with 41 additions and 3 deletions

View file

@ -1,4 +1,5 @@
import Component from '@ember/component'; import Component from '@ember/component';
import { set } from '@ember/object';
const defaultFiles = []; const defaultFiles = [];
const defaultSelectedFilePaths = []; const defaultSelectedFilePaths = [];
@ -8,6 +9,13 @@ export default Component.extend({
selectedFilePaths: defaultSelectedFilePaths, selectedFilePaths: defaultSelectedFilePaths,
actions: { actions: {
toggleSelectAll() {
if (this.selectedFilePaths.length === this.files.length) {
set(this, 'selectedFilePaths', defaultSelectedFilePaths);
} else {
set(this, 'selectedFilePaths', this.files.map(file => file.path));
}
},
toggleSelectedPath(path) { toggleSelectedPath(path) {
if (!this.selectedFilePaths.includes(path)) { if (!this.selectedFilePaths.includes(path)) {
this.selectedFilePaths.pushObject(path); this.selectedFilePaths.pushObject(path);

View file

@ -1,7 +1,10 @@
<header data-test-id="file-table-action-bar"> <header data-test-id="file-table-action-bar">
<label data-test-action="file-table-select-all"> <button
data-test-action="file-table-select-all"
{{action "toggleSelectAll"}}
>
Selected {{selectedFilePaths.length}} Selected {{selectedFilePaths.length}}
</label> </button>
<button data-test-action="file-table-download-selected">Download Selected</button> <button data-test-action="file-table-download-selected">Download Selected</button>
</header> </header>

View file

@ -3,11 +3,12 @@ import { setupRenderingTest } from 'ember-qunit';
import { render, find, findAll, click } from '@ember/test-helpers'; import { render, find, findAll, click } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile'; import hbs from 'htmlbars-inline-precompile';
const randomNumber = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const genericFile = () => { const genericFile = () => {
return { return {
name: 'foo.bar', name: 'foo.bar',
device: 'Baz', device: 'Baz',
path: '~/foo.bar', path: `~/foo{${randomNumber(1,9001)}.bar`,
status: 'available', status: 'available',
}; };
}; };
@ -47,4 +48,30 @@ module('Integration | Component | file-table', function(hooks) {
assert.equal(find('[data-test-action=file-table-select-all]').textContent.trim(), 'Selected 0'); assert.equal(find('[data-test-action=file-table-select-all]').textContent.trim(), 'Selected 0');
assert.equal(find('[data-test-id=file-table-item-selected]').textContent.trim(), '', 'Unselected after second click'); assert.equal(find('[data-test-id=file-table-item-selected]').textContent.trim(), '', 'Unselected after second click');
}); });
test('select all, with none selected', async function(assert) {
this.set('files', [genericFile(), genericFile()]);
await render(hbs`{{file-table files=files}}`);
await click('[data-test-action=file-table-select-all]');
assert.equal(find('[data-test-action=file-table-select-all]').textContent.trim(), 'Selected ' + this.files.length);
});
test('select all, with everything selected', async function(assert) {
this.set('files', [genericFile(), genericFile()]);
this.set('selectedFilePaths', this.files.map(file => file.path));
await render(hbs`{{file-table files=files selectedFilePaths=selectedFilePaths}}`);
await click('[data-test-action=file-table-select-all]');
assert.equal(find('[data-test-action=file-table-select-all]').textContent.trim(), 'Selected 0');
});
test('select all, with some selected', async function(assert) {
this.set('files', [genericFile(), genericFile()]);
this.set('selectedFilePaths', [this.files[0].path]);
await render(hbs`{{file-table files=files selectedFilePaths=selectedFilePaths}}`);
await click('[data-test-action=file-table-select-all]');
assert.equal(find('[data-test-action=file-table-select-all]').textContent.trim(), 'Selected ' + this.files.length);
});
}); });