|
| 1 | +import {QueryList, Renderer, AnimationPlayer} from '@angular/core'; |
| 2 | +import {async, TestBed} from '@angular/core/testing'; |
| 3 | +import {MdBasicChip} from './index'; |
| 4 | +import {ChipListKeyManager} from './chip-list-key-manager'; |
| 5 | +import {RenderDebugInfo} from '@angular/core/src/render/api'; |
| 6 | +import {AnimationStyles} from '@angular/core/src/animation/animation_styles'; |
| 7 | +import {AnimationKeyframe} from '@angular/core/src/animation/animation_keyframe'; |
| 8 | + |
| 9 | +class FakeRenderer extends Renderer { |
| 10 | + selectRootElement(selectorOrNode: string|any, debugInfo?: RenderDebugInfo): any { |
| 11 | + return null; |
| 12 | + } |
| 13 | + |
| 14 | + createElement(parentElement: any, name: string, debugInfo?: RenderDebugInfo): any { |
| 15 | + return null; |
| 16 | + } |
| 17 | + |
| 18 | + createViewRoot(hostElement: any): any { |
| 19 | + return null; |
| 20 | + } |
| 21 | + |
| 22 | + createTemplateAnchor(parentElement: any, debugInfo?: RenderDebugInfo): any { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + createText(parentElement: any, value: string, debugInfo?: RenderDebugInfo): any { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + projectNodes(parentElement: any, nodes: any[]): void { |
| 31 | + } |
| 32 | + |
| 33 | + attachViewAfter(node: any, viewRootNodes: any[]): void { |
| 34 | + } |
| 35 | + |
| 36 | + detachView(viewRootNodes: any[]): void { |
| 37 | + } |
| 38 | + |
| 39 | + destroyView(hostElement: any, viewAllNodes: any[]): void { |
| 40 | + } |
| 41 | + |
| 42 | + listen(renderElement: any, name: string, callback: Function): Function { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + listenGlobal(target: string, name: string, callback: Function): Function { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + setElementProperty(renderElement: any, propertyName: string, propertyValue: any): void { |
| 51 | + } |
| 52 | + |
| 53 | + setElementAttribute(renderElement: any, attributeName: string, attributeValue: string): void { |
| 54 | + } |
| 55 | + |
| 56 | + setBindingDebugInfo(renderElement: any, propertyName: string, propertyValue: string): void { |
| 57 | + } |
| 58 | + |
| 59 | + setElementClass(renderElement: any, className: string, isAdd: boolean): void { |
| 60 | + } |
| 61 | + |
| 62 | + setElementStyle(renderElement: any, styleName: string, styleValue: string): void { |
| 63 | + } |
| 64 | + |
| 65 | + invokeElementMethod(renderElement: any, methodName: string, args?: any[]): void { |
| 66 | + } |
| 67 | + |
| 68 | + setText(renderNode: any, text: string): void { |
| 69 | + } |
| 70 | + |
| 71 | + animate(element: any, startingStyles: AnimationStyles, keyframes: AnimationKeyframe[], |
| 72 | + duration: number, delay: number, easing: string, |
| 73 | + previousPlayers?: AnimationPlayer[]): AnimationPlayer { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +class FakeElement { |
| 80 | + nativeElement: Element; |
| 81 | +} |
| 82 | + |
| 83 | +/* |
| 84 | + * Create a fake Chip class so we don't have to test actual HTML elements. |
| 85 | + */ |
| 86 | +class FakeChip extends MdBasicChip { |
| 87 | + |
| 88 | + constructor() { |
| 89 | + // Pass in null for the renderer/elementRef |
| 90 | + super(new FakeRenderer(), new FakeElement()); |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +describe('ChipListKeyManager', () => { |
| 96 | + let items: QueryList<MdBasicChip>; |
| 97 | + let manager: ChipListKeyManager; |
| 98 | + |
| 99 | + beforeEach(async(() => { |
| 100 | + items = new QueryList<MdBasicChip>(); |
| 101 | + items.reset([ |
| 102 | + new FakeChip(), |
| 103 | + new FakeChip(), |
| 104 | + new FakeChip(), |
| 105 | + new FakeChip(), |
| 106 | + new FakeChip() |
| 107 | + ]); |
| 108 | + |
| 109 | + manager = new ChipListKeyManager(items); |
| 110 | + |
| 111 | + TestBed.compileComponents(); |
| 112 | + })); |
| 113 | + |
| 114 | + describe('basic behaviors', () => { |
| 115 | + it('watches for chip focus', () => { |
| 116 | + let array = items.toArray(); |
| 117 | + let lastIndex = array.length - 1; |
| 118 | + let lastItem = array[lastIndex]; |
| 119 | + |
| 120 | + lastItem.focus(); |
| 121 | + |
| 122 | + expect(manager.focusedItemIndex).toBe(lastIndex); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('on chip destroy', () => { |
| 126 | + it('focuses the next item', () => { |
| 127 | + let array = items.toArray(); |
| 128 | + let midItem = array[2]; |
| 129 | + |
| 130 | + // Focus the middle item |
| 131 | + midItem.focus(); |
| 132 | + |
| 133 | + // Destroy the middle item |
| 134 | + midItem.destroy.emit(); |
| 135 | + |
| 136 | + // It focuses the 4th item (now at index 2) |
| 137 | + expect(manager.focusedItemIndex).toEqual(2); |
| 138 | + }); |
| 139 | + |
| 140 | + it('focuses the previous item', () => { |
| 141 | + let array = items.toArray(); |
| 142 | + let lastIndex = array.length - 1; |
| 143 | + let lastItem = array[lastIndex]; |
| 144 | + |
| 145 | + // Focus the last item |
| 146 | + lastItem.focus(); |
| 147 | + |
| 148 | + // Destroy the last item |
| 149 | + lastItem.destroy.emit(); |
| 150 | + |
| 151 | + // It focuses the next-to-last item |
| 152 | + expect(manager.focusedItemIndex).toEqual(lastIndex - 1); |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments