Skip to content

Commit

Permalink
Merge pull request #2266 from abpframework/test/theme-shared
Browse files Browse the repository at this point in the history
test(theme-shared): add sort-order-icon component test
  • Loading branch information
mehmet-erim authored Nov 27, 2019
2 parents c1facb2 + ad7f0b2 commit 075bfe4
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,53 @@ import { Component, EventEmitter, Input, Output } from '@angular/core';
templateUrl: './sort-order-icon.component.html',
})
export class SortOrderIconComponent {
private _order: string;
private _selectedKey: string;
private _order: 'asc' | 'desc' | '';
private _selectedSortKey: string;

/**
* @deprecated use selectedSortKey instead.
*/
@Input()
set selectedKey(value: string) {
this._selectedKey = value;
this.selectedSortKey = value;
this.selectedKeyChange.emit(value);
}
get selectedKey(): string {
return this._selectedKey;
return this._selectedSortKey;
}

@Input()
set selectedSortKey(value: string) {
this._selectedSortKey = value;
this.selectedSortKeyChange.emit(value);
}
get selectedSortKey(): string {
return this._selectedSortKey;
}

@Output() readonly selectedKeyChange = new EventEmitter<string>();
@Output() readonly selectedSortKeyChange = new EventEmitter<string>();

/**
* @deprecated use sortKey instead.
*/
@Input()
get key(): string {
return this.sortKey;
}
set key(value: string) {
this.sortKey = value;
}

@Input()
key: string;
sortKey: string;

@Input()
set order(value: string) {
set order(value: 'asc' | 'desc' | '') {
this._order = value;
this.orderChange.emit(value);
}
get order(): string {
get order(): 'asc' | 'desc' | '' {
return this._order;
}

Expand All @@ -37,24 +61,27 @@ export class SortOrderIconComponent {
iconClass: string;

get icon(): string {
if (!this.selectedKey) return 'fa-sort';
if (this.selectedKey === this.key) return `fa-sort-${this.order}`;
if (!this.selectedSortKey) return 'fa-sort';
if (this.selectedSortKey === this.sortKey) return `fa-sort-${this.order}`;
else return '';
}

sort(key: string) {
this.selectedKey = key;
this.selectedKey = key; // TODO: To be removed
this.selectedSortKey = key;
switch (this.order) {
case '':
this.order = 'asc';
this.orderChange.emit('asc');
break;
case 'asc':
this.order = 'desc';
this.orderChange.emit('desc');
break;
case 'desc':
this.order = '';
this.selectedKey = '';
this.selectedKey = ''; // TODO: To be removed
this.orderChange.emit('');
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createHostFactory, SpectatorHost } from '@ngneat/spectator/jest';
import { SortOrderIconComponent } from '../components/sort-order-icon/sort-order-icon.component';

describe('SortOrderIconComponent', () => {
let spectator: SpectatorHost<SortOrderIconComponent>;
let component: SortOrderIconComponent;
const createHost = createHostFactory(SortOrderIconComponent);

beforeEach(() => {
spectator = createHost(
'<abp-sort-order-icon key="testKey" [(selectedSortKey)]="selectedSortKey" [(order)]="order"></abp-sort-order-icon>',
{
hostProps: {
selectedSortKey: '',
order: '',
},
},
);
component = spectator.component;
});

test('should have correct icon class when selectedSortKey and sortKey are the same', () => {
const newKey = 'testKey';
component.sort(newKey);
expect(component.selectedSortKey).toBe(newKey);
expect(component.order).toBe('asc');
expect(component.icon).toBe('fa-sort-asc');
});

test("shouldn't have any icon class when sortKey and selectedSortKey are different", () => {
const newKey = 'otherKey';
component.sort(newKey);
expect(component.selectedSortKey).toBe(newKey);
expect(component.order).toBe('asc');
expect(component.icon).toBe('');
});

test('should change order correctly when sort function called', () => {
component.sort('testKey');
expect(component.order).toBe('asc');
component.sort('testKey');
expect(component.order).toBe('desc');
component.sort('testKey');
expect(component.order).toBe('');
});
});

0 comments on commit 075bfe4

Please sign in to comment.