Skip to content

Commit

Permalink
fix: tables are sorted on default by first column
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSlimvReal authored Feb 21, 2022
1 parent 48d9e1c commit 012b8ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import { RouterTestingModule } from "@angular/router/testing";
import { EntitySubrecordModule } from "../entity-subrecord.module";
import { Entity } from "../../../entity/model/entity";
import { SimpleChange } from "@angular/core";
import { NoopAnimationsModule } from "@angular/platform-browser/animations";
import { MatNativeDateModule } from "@angular/material/core";
import { DatePipe, PercentPipe } from "@angular/common";
Expand Down Expand Up @@ -81,10 +80,7 @@ describe("EntitySubrecordComponent", () => {
view: "DisplayConfigurableEnum",
},
];
component.ngOnChanges({
records: new SimpleChange(undefined, component.records, true),
columns: new SimpleChange(undefined, component.columns, true),
});
component.ngOnChanges({ records: undefined, columns: undefined });
fixture.detectChanges();

component.recordsDataSource.sort.sort({
Expand All @@ -103,18 +99,10 @@ describe("EntitySubrecordComponent", () => {
const children = [Child.create("C"), Child.create("A"), Child.create("B")];
component.columnsToDisplay = ["name", "projectNumber"];
component.records = children;
// trigger ngOnChanges for manually updated property
component.ngOnChanges({
records: new SimpleChange(undefined, children, true),
});
component.ngOnChanges({ records: undefined });

const sortedChildren = component.recordsDataSource
.sortData(
children.map((child) => {
return { record: child };
}),
component.sort
)
.sortData(component.recordsDataSource.data, component.sort)
.map((c) => c.record["name"]);

expect(sortedChildren).toEqual(["A", "B", "C"]);
Expand Down Expand Up @@ -142,17 +130,11 @@ describe("EntitySubrecordComponent", () => {
},
];

component.ngOnChanges({
records: new SimpleChange(undefined, children, true),
});
component.ngOnChanges({ records: undefined });
fixture.detectChanges();

const sortedChildren = component.recordsDataSource
._orderData(
children.map((child) => {
return { record: child };
})
)
.sortData(component.recordsDataSource.data, component.sort)
.map((c) => c.record["name"]);

expect(sortedChildren).toEqual(["2", "1", "0"]);
Expand All @@ -169,16 +151,12 @@ describe("EntitySubrecordComponent", () => {
children[3].name = "AB";
children[2].name = "Z";
children[1].name = "C";
component.ngOnChanges({ records: null });
component.sort.sort({ id: "name", start: "asc", disableClear: false });
component.records = children;
component.ngOnChanges({ records: undefined });

component.sort.sort({ id: "name", start: "asc", disableClear: false });
const sortedIds = component.recordsDataSource
.sortData(
children.map((child) => {
return { record: child };
}),
component.sort
)
.sortData(component.recordsDataSource.data, component.sort)
.map((c) => c.record.getId());

expect(sortedIds).toEqual(["0", "3", "1", "2"]);
Expand All @@ -190,16 +168,12 @@ describe("EntitySubrecordComponent", () => {
notes[3].category = { id: "1", label: "AB" };
notes[2].category = { id: "2", label: "Z" };
notes[1].category = { id: "3", label: "C" };
component.records = notes;
component.ngOnChanges({ records: null });

component.sort.sort({ id: "category", start: "asc", disableClear: false });
const sortedIds = component.recordsDataSource
.sortData(
notes.map((note) => {
return { record: note };
}),
component.sort
)
.sortData(component.recordsDataSource.data, component.sort)
.map((note) => note.record.getId());

expect(sortedIds).toEqual(["0", "3", "1", "2"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ export class EntitySubrecordComponent<T extends Entity>
* @param changes
*/
ngOnChanges(changes: SimpleChanges) {
if (
changes.hasOwnProperty("columns") ||
changes.hasOwnProperty("records")
) {
if (changes.hasOwnProperty("columns")) {
this.initFormGroups();
}
if (changes.hasOwnProperty("records") && this.records.length > 0) {
this.initFormGroups();
this.initDefaultSort();
if (this.columnsToDisplay.length < 2) {
Expand Down Expand Up @@ -179,8 +179,11 @@ export class EntitySubrecordComponent<T extends Entity>
return;
}

// initial sorting by first column
const sortBy = this.columnsToDisplay[0];
// initial sorting by first column, ensure that not the 'action' column is used
const sortBy =
this.columnsToDisplay[0] === "actions"
? this.columnsToDisplay[1]
: this.columnsToDisplay[0];
const sortByColumn = this._columns.find((c) => c.id === sortBy);
let sortDirection = "asc";
if (
Expand Down

0 comments on commit 012b8ab

Please sign in to comment.