Skip to content

Commit

Permalink
Orderlist Unit Test Improved
Browse files Browse the repository at this point in the history
  • Loading branch information
yigitfindikli committed Dec 17, 2018
1 parent 6f9d826 commit d9880fe
Showing 1 changed file with 131 additions and 4 deletions.
135 changes: 131 additions & 4 deletions src/app/components/orderlist/orderlist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ describe('OrderList', () => {
expect(itemListEl.children.length).toEqual(10);
});

it('should have selections by default', () => {
orderlist.selection = [{"brand": "BMW", "year": 2003, "color": "Blue", "vin": "j6w54qgh"}];
fixture.detectChanges();

const itemListEl = fixture.debugElement.query(By.css('ul'));
const bmwEl = itemListEl.queryAll(By.css('.ui-orderlist-item'))[3];
fixture.detectChanges();

expect(orderlist.selection.length).toEqual(1);
expect(orderlist.selection[0].brand).toEqual("BMW");
});

it('should call onItem click and select a item', () => {
const onItemClickSpy = spyOn(orderlist, 'onItemClick').and.callThrough();
const onItemTouchEndSpy = spyOn(orderlist, 'onItemTouchEnd').and.callThrough();
Expand Down Expand Up @@ -271,7 +283,27 @@ describe('OrderList', () => {
expect(itemListEl.queryAll(By.css('.ui-orderlist-item'))[9].context.$implicit.brand).toEqual("BMW");
});

it('should filter items', () => {
it('should show filter items by default', () => {
orderlist.filterBy = "brand";
orderlist.filterValue = "v";
fixture.detectChanges();

const filterEl = fixture.debugElement.query(By.css('input'));
fixture.detectChanges();

const itemsEl = fixture.debugElement.queryAll(By.css(".ui-orderlist-item"));
expect(orderlist.visibleOptions.length).toEqual(2);
expect(orderlist.visibleOptions[0].brand).toEqual("VW");
expect(orderlist.visibleOptions[1].brand).toEqual("Volvo");
for(let i =0; i<itemsEl.length;i++){
if(i==0 || i==5)
expect(itemsEl[i].nativeElement.style.display).toEqual("block");
else
expect(itemsEl[i].nativeElement.style.display).not.toEqual("block");
}
});

it('should show filter items', () => {
orderlist.filterBy = "brand";
fixture.detectChanges();

Expand Down Expand Up @@ -342,7 +374,41 @@ describe('OrderList', () => {
fixture.detectChanges();

expect(data).toBeTruthy();
});
});

it('should not onReorder first item to top', () => {
fixture.detectChanges();

let data;
orderlist.onReorder.subscribe(value => data = value);
const itemListEl = fixture.debugElement.query(By.css('ul'));
const vwEl = itemListEl.queryAll(By.css('.ui-orderlist-item'))[0];
const buttonsEl = fixture.debugElement.queryAll(By.css('button'));
const moveTopButtonEl = buttonsEl[1];
vwEl.nativeElement.click();
moveTopButtonEl.nativeElement.click();
fixture.detectChanges();

expect(data).toBeTruthy();
expect(orderlist.value).toBeTruthy();
});

it('should not onReorder first item to up', () => {
fixture.detectChanges();

let data;
orderlist.onReorder.subscribe(value => data = value);
const itemListEl = fixture.debugElement.query(By.css('ul'));
const vwEl = itemListEl.queryAll(By.css('.ui-orderlist-item'))[0];
const buttonsEl = fixture.debugElement.queryAll(By.css('button'));
const moveTopButtonEl = buttonsEl[0];
vwEl.nativeElement.click();
moveTopButtonEl.nativeElement.click();
fixture.detectChanges();

expect(data).toBeTruthy();
expect(orderlist.value).toBeTruthy();
});

it('should listen onReorder in moveBottom', () => {
const moveBottomSpy = spyOn(orderlist, 'moveBottom').and.callThrough();
Expand All @@ -359,7 +425,41 @@ describe('OrderList', () => {
fixture.detectChanges();

expect(data).toBeTruthy();
});
});

it('should not onReorder first item to bottom', () => {
fixture.detectChanges();

let data;
orderlist.onReorder.subscribe(value => data = value);
const itemListEl = fixture.debugElement.query(By.css('ul'));
const bmwEl = itemListEl.queryAll(By.css('.ui-orderlist-item'))[9];
const buttonsEl = fixture.debugElement.queryAll(By.css('button'));
const moveBottomButtonEl = buttonsEl[3];
bmwEl.nativeElement.click();
moveBottomButtonEl.nativeElement.click();
fixture.detectChanges();

expect(data).toBeTruthy();
expect(orderlist.value).toBeTruthy();
});

it('should not onReorder first item to down', () => {
fixture.detectChanges();

let data;
orderlist.onReorder.subscribe(value => data = value);
const itemListEl = fixture.debugElement.query(By.css('ul'));
const bmwEl = itemListEl.queryAll(By.css('.ui-orderlist-item'))[9];
const buttonsEl = fixture.debugElement.queryAll(By.css('button'));
const moveBottomButtonEl = buttonsEl[2];
bmwEl.nativeElement.click();
moveBottomButtonEl.nativeElement.click();
fixture.detectChanges();

expect(data).toBeTruthy();
expect(orderlist.value).toBeTruthy();
});

it('should listen onSelectionChange in onItem', () => {
fixture.detectChanges();
Expand Down Expand Up @@ -393,4 +493,31 @@ describe('OrderList', () => {
expect(data.value[1].brand).toEqual("Volvo");
});

});
it('should select item with keyboard navigation', () => {
const findNextItemSpy = spyOn(orderlist,"findNextItem").and.callThrough();
const findPrevItemSpy = spyOn(orderlist,"findPrevItem").and.callThrough();
fixture.detectChanges();

const itemListEl = fixture.debugElement.query(By.css('ul'));
const bmwEl = itemListEl.queryAll(By.css('.ui-orderlist-item'))[3].nativeElement;
const event: any = document.createEvent('CustomEvent');
event.which = 40;
event.initEvent('keydown');
bmwEl.dispatchEvent(event);
fixture.detectChanges();

event.which = 38;
bmwEl.dispatchEvent(event);
fixture.detectChanges();

event.which = 13;
bmwEl.dispatchEvent(event);
fixture.detectChanges();

expect(orderlist.selection.length).toEqual(1);
expect(orderlist.selection[0].brand).toEqual("BMW");
expect(findNextItemSpy).toHaveBeenCalled();
expect(findPrevItemSpy).toHaveBeenCalled();
expect(bmwEl.className).toContain('ui-state-highlight');
});
});

0 comments on commit d9880fe

Please sign in to comment.