import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { NgTabsComponent } from './ng-tabs.component'; // TODO: Consolidate test between this component and Element testing. // Two functions won't exist in this file, slotChange() and HTMLElement[] from ta-tab web-component describe('NgTabsComponent', () => { let component: NgTabsComponent; let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [NgTabsComponent], }) .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(NgTabsComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('creates the component', () => { expect(component).toBeTruthy(); }); describe('tabClicked()', () => { it('emits the selected tab element', () => { const element: HTMLElement = document.createElement('div'); const spy: any = spyOn(component, 'tabClicked'); component.tabClicked(element); expect(spy).toHaveBeenCalledWith(element); }) }); describe('expect @hostlistner properties to be defined', () => { it('defines screenWidth', () => { expect(component.screenWidth).toBeDefined(); expect(component.screenWidth).not.toBeNull(); }) it('should trigger onResize method when window is resized', () => { const onResize: any = component.onResize() const spy: any = spyOn(component, 'onResize'); window.dispatchEvent(new Event('resize')); expect(spy).toHaveBeenCalled(); }); it('keyup.tab Listener', () => { const tabKeyPressed: any = new KeyboardEvent('keypress', { key: 'tab', }); const spy: any = spyOn(component, 'tabKeyEvent'); const tabEvent: any = component.tabKeyEvent(tabKeyPressed); document.dispatchEvent(tabKeyPressed); expect(spy).toHaveBeenCalled(); }); it('User is Tabbing', () => { const tabKeyPressed: any = new KeyboardEvent('keypress', { key: '9', }); const spy: any = spyOn(component, 'userTabbing'); component.tabKeyEvent(tabKeyPressed); document.dispatchEvent(tabKeyPressed); (component.userTabbing = true) ? (tabKeyPressed.key === component.userTabbing.toString()) : (component.userTabbing = false); console.log(component.userTabbing) expect(component.userTabbing).toBeTruthy(); }); }); describe('viewTab()', () => { const leftScrollRemainder: number = 200; it('displays right arrow if scrollWidth > clientWidth', () => { const scrollLeft: number = 0; component.viewTab(800, 600, scrollLeft, leftScrollRemainder); expect(component.showRightArrow).toBe(true); }) it('displays left arrow if scrollLeft > 0', () => { const scrollLeft: number = 0; component.viewTab(800, 600, leftScrollRemainder, scrollLeft); expect(component.showLeftArrow).toBe(true); }) it('proves false when scrollLeft >= leftScrollRemainder', () => { const scrollLeft: number = 100; component.viewTab(800, 600, leftScrollRemainder, scrollLeft); expect(component.showRightArrow).toBe(false); }) it('proves true when scrollLeft < leftScrollRemainder', () => { const scrollLeft: number = 500; component.viewTab(800, 600, leftScrollRemainder, scrollLeft); expect(component.showRightArrow).toBe(true); }) it('expect to display both left and right arrows', () => { const leftScrollRemainder: number = 300; const scrollLeft: number = 500; component.viewTab(900, 600, leftScrollRemainder, scrollLeft); expect(component.showRightArrow).toBe(true); }) }); describe('scrollRight()', () => { it('should be called', () => { component.showRightArrow = true; component.showLeftArrow = true; const spy: any = spyOn(component, 'scrollRight'); expect(spy).toBeDefined(); component.scrollRight(); expect(component.scrollTabs.nativeElement.offsetParent.offsetLeft).toBeDefined(); expect(component.scrollTabs.nativeElement.scrollLeft).toBeDefined(); expect(spy).toHaveBeenCalled(); }); it('Offset should be defined', () => { component.showRightArrow = true; component.showLeftArrow = true; component.scrollRight(); expect(component.scrollTabs.nativeElement.offsetParent.offsetLeft).toBeDefined(); }); }); describe('scrollLeft()', () => { it('works', () => { spyOn(component, 'scrollLeft'); component.scrollLeft(); expect(component.scrollLeft).toHaveBeenCalled(); }) it('scroll left client width is defined', () => { component.showRightArrow = true; component.showLeftArrow = true; component.scrollLeft(); expect(component.scrollTabs.nativeElement.clientWidth).toBeDefined(); }) }); describe('getVariant()', () => { it('returns \'-tertiaryTabs\' class name for the input variant = \'tertiary\'', () => { component.variant = 'tertiary'; expect(component.getVariant()).toBe('-tertiaryTabs'); }) it('returns \'-secondaryTabs\' class name for the input variant = \'secondary\'', () => { component.variant = 'secondary'; expect(component.getVariant()).toBe('-secondaryTabs'); }) it('returns \'\' for invalid or unspecified variant input', () => { expect(component.getVariant()).toBe(''); component.variant = 'sflfjdnb'; expect(component.getVariant()).toBe(''); }) }); });