Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(theme-shared): add sort-order-icon component test #2266

Merged
merged 1 commit into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('');
});
});