Skip to content

Commit

Permalink
Resolve the action helper/modifier deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
Windvis committed Nov 12, 2024
1 parent 2ee7e30 commit afd11a4
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 33 deletions.
4 changes: 2 additions & 2 deletions addon/components/au-data-table/number-pagination.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<AuButton
@skin="link"
@icon={{this.NavLeftIcon}}
{{action "changePage" this.links.prev}}
{{on "click" (fn this.changePage this.links.prev)}}
>
vorige
<span class="au-u-hidden-visually"> {{this.pageSize}} rijen</span>
Expand All @@ -26,7 +26,7 @@
@skin="link"
@icon={{this.NavRightIcon}}
@iconAlignment="right"
{{action "changePage" this.links.next}}
{{on "click" (fn this.changePage this.links.next)}}
>
volgende
<span class="au-u-hidden-visually"> {{this.pageSize}} rijen</span>
Expand Down
12 changes: 5 additions & 7 deletions addon/components/au-data-table/number-pagination.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable ember/no-actions-hash, ember/no-classic-classes, ember/no-classic-components, ember/no-get, ember/require-tagless-components */
/* eslint-disable ember/no-classic-classes, ember/no-classic-components, ember/no-get, ember/require-tagless-components */
import Component from '@ember/component';
import { computed } from '@ember/object';
import { action, computed } from '@ember/object';
import { gt } from '@ember/object/computed';
import { NavLeftIcon } from '../icons/nav-left';
import { NavRightIcon } from '../icons/nav-right';
Expand Down Expand Up @@ -44,11 +44,9 @@ const NumberPagination = Component.extend({
(val, index) => this.firstPage + index,
);
}),
actions: {
changePage(link) {
this.set('page', link['number'] || 0);
},
},
changePage: action(function (link) {
this.set('page', link['number'] || 0);
}),
});

export default NumberPagination.extend({
Expand Down
6 changes: 3 additions & 3 deletions addon/components/au-data-table/th-sortable.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{#if (eq this.order "desc")}}
<button
role="button"
{{action "inverseSorting"}}
{{on "click" this.inverseSorting}}
class="au-c-data-table__sort"
>
<span class="au-u-hidden-visually">Oplopend sorteren</span>
Expand All @@ -15,7 +15,7 @@
{{else if (eq this.order "asc")}}
<button
role="button"
{{action "inverseSorting"}}
{{on "click" this.inverseSorting}}
class="au-c-data-table__sort"
>
<span class="au-u-hidden-visually">Aflopend sorteren</span>
Expand All @@ -24,7 +24,7 @@
{{else}}
<button
role="button"
{{action "inverseSorting"}}
{{on "click" this.inverseSorting}}
class="au-c-data-table__sort"
>
<span class="au-u-hidden-visually">Sorteren</span>
Expand Down
40 changes: 19 additions & 21 deletions addon/components/au-data-table/th-sortable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable ember/no-actions-hash, ember/no-classic-classes, ember/no-classic-components, ember/require-tagless-components */
/* eslint-disable ember/no-classic-classes, ember/no-classic-components, ember/require-tagless-components */
import Component from '@ember/component';
import { computed } from '@ember/object';
import { action, computed } from '@ember/object';
import { NavDownIcon } from '../icons/nav-down';
import { NavUpIcon } from '../icons/nav-up';
import { NavUpDownIcon } from '../icons/nav-up-down';
Expand Down Expand Up @@ -41,25 +41,23 @@ const ThSortable = Component.extend({
}
}),

actions: {
/**
Sets the current sorting parameter.
Note: the current sorting parameter may contain another field than the given field.
In case the given field is currently sorted ascending, change to descending.
In case the given field is currently sorted descending, clean the sorting.
Else, set the sorting to ascending on the given field.
*/
inverseSorting() {
if (this.order === 'asc') {
this.set('currentSorting', this._inverseSorting(this.currentSorting));
} else if (this.order === 'desc') {
this.set('currentSorting', '');
} else {
// if currentSorting is not set to this field
this.set('currentSorting', this.dasherizedField);
}
},
},
/**
Sets the current sorting parameter.
Note: the current sorting parameter may contain another field than the given field.
In case the given field is currently sorted ascending, change to descending.
In case the given field is currently sorted descending, clean the sorting.
Else, set the sorting to ascending on the given field.
*/
inverseSorting: action(function () {
if (this.order === 'asc') {
this.set('currentSorting', this._inverseSorting(this.currentSorting));
} else if (this.order === 'desc') {
this.set('currentSorting', '');
} else {
// if currentSorting is not set to this field
this.set('currentSorting', this.dasherizedField);
}
}),
});

export default ThSortable.extend({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { getRootElement, click, render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { queryByText } from '@testing-library/dom';

module(
'Integration | Component | au-data-table-number-pagination',
function (hooks) {
setupRenderingTest(hooks);

test('it conditionally shows buttons to change the page', async function (assert) {
const baseLinks = {
first: {
number: 0,
},
last: {
number: 9,
},
};

this.set('page', 1);
this.set('links', {
...baseLinks,
prev: {
number: 0,
},
next: {
number: 2,
},
});

await render(
hbs`
<AuDataTableNumberPagination
@page={{this.page}}
@links={{this.links}}
@size={{10}}
@nbOfItems={{10}}
@total={{100}}
/>
`,
);

const root = getRootElement();
let prevButton = queryByText(root, 'vorige');
let nextButton = queryByText(root, 'volgende');

assert.ok(
prevButton,
"It shows a button to go to the previous page when we aren't on the first page",
);
assert.ok(
nextButton,
"It shows a button to go to the next page when we aren't on the last page",
);

await click(prevButton);
assert.equal(
this.page,
0,
'It updates the @page value with 2-way-binding',
);

this.set('links', {
...baseLinks,
next: {
number: 1,
},
});

prevButton = queryByText(root, 'vorige');
assert.notOk(
prevButton,
'It hides the button to go to the previous page when we are on the first page',
);

nextButton = queryByText(root, 'volgende');
assert.ok(nextButton);

await click(nextButton);
assert.equal(
this.page,
1,
'It updates the @page value with 2-way-binding',
);
this.set('links', {
...baseLinks,
prev: {
number: 0,
},
next: {
number: 2,
},
});

prevButton = queryByText(root, 'vorige');
assert.ok(prevButton);

this.set('page', 9);
this.set('links', {
...baseLinks,
prev: {
number: 8,
},
});

nextButton = queryByText(root, 'volgende');
assert.notOk(
nextButton,
'It hides the button to go to the next page when we are on the last page',
);
});
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { getRootElement, click, render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { queryByText } from '@testing-library/dom';

module('Integration | Component | au-data-table-th-sortable', function (hooks) {
setupRenderingTest(hooks);

test('it switches between the different sorting states when the button is clicked', async function (assert) {
this.set('sort', '');

await render(
hbs`
<AuDataTableThSortable
@field="foo"
@label="Foo"
@currentSorting={{this.sort}}
/>
`,
);

const root = getRootElement();
let sortButton = queryByText(root, 'Sorteren');

assert.ok(
sortButton,
"It shows a sort button if we aren't currently sorting on the field that matches the component",
);

await click(sortButton);
assert.equal(
this.sort,
'foo',
'It updates the sort value with 2-way-binding. It initially sorts ascending.',
);

sortButton = queryByText(root, 'Sorteren');
assert.notOk(
sortButton,
'It hides the default sort button when we are sorting on the field that matches the component',
);

sortButton = queryByText(root, 'Aflopend sorteren');
assert.ok(
sortButton,
'It shows the sort descending button when we are currently sorting ascending on the field that matches the component',
);

await click(sortButton);
assert.equal(
this.sort,
'-foo',
'It switches to a descending sort for the provided field',
);

sortButton = queryByText(root, 'Aflopend sorteren');
assert.notOk(sortButton);

// TODO: this logic isn't correct I think since we stop sorting when we click again?
sortButton = queryByText(root, 'Oplopend sorteren');
assert.ok(sortButton);

await click(sortButton);
assert.equal(this.sort, '', 'It stops sorting on the provided field');
});
});

0 comments on commit afd11a4

Please sign in to comment.