From 183b275526c54f6b010efb7e828fa24ab2b2baed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Rold=C3=A3o?= Date: Tue, 28 Feb 2017 21:17:14 +0000 Subject: [PATCH] fix(tabs): Use [ngClass] to avoid conflicts with [class.x] bindings (#1651) * fix(tab.component): Use [ngClass] to avoid conflicts with [class.x] bindings * Added tests, handle undefined customClass --- src/spec/tabset.component.spec.ts | 40 +++++++++++++++++++++++++++---- src/tabs/tabset.component.ts | 2 +- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/spec/tabset.component.spec.ts b/src/spec/tabset.component.spec.ts index 4b5b6fd518..4ee77bf281 100644 --- a/src/spec/tabset.component.spec.ts +++ b/src/spec/tabset.component.spec.ts @@ -10,6 +10,7 @@ const html = ` tab0 content `; +function getTabItems(nativeEl: HTMLElement): NodeListOf { + return nativeEl.querySelectorAll('.nav-item'); +} + function getTabTitles(nativeEl: HTMLElement): NodeListOf { return nativeEl.querySelectorAll('.nav-link'); } @@ -28,18 +33,18 @@ function getTabContent(nativeEl: HTMLElement): NodeListOf { } function expectActiveTabs(nativeEl: HTMLElement, active: boolean[]): void { - const tabTitles = getTabTitles(nativeEl); + const tabItems = getTabItems(nativeEl); const tabContent = getTabContent(nativeEl); - expect(tabTitles.length).toBe(active.length); + expect(tabItems.length).toBe(active.length); expect(tabContent.length).toBe(active.length); for (let i = 0; i < active.length; i++) { if (active[i]) { - expect(tabTitles[i].classList).toContain('active'); + expect(tabItems[i].classList).toContain('active'); expect(tabContent[i].classList).toContain('active'); } else { - expect(tabTitles[i].classList).not.toContain('active'); + expect(tabItems[i].classList).not.toContain('active'); expect(tabContent[i].classList).not.toContain('active'); } } @@ -175,6 +180,31 @@ describe('Component: Tabs', () => { heading: 'tab3' })); }); + + it('should set class on a tab item through [customClass]', () => { + const tabItems = getTabItems(element); + + expect(tabItems[1].classList).toContain('testCustomClass'); + }); + + it('should prevent interference of [customClass] and state classes', () => { + const tabItems = getTabItems(element); + const tabTitles = getTabTitles(element); + + expect(tabItems[1].classList).toContain('testCustomClass'); + + (tabTitles[1] as HTMLAnchorElement).click(); + fixture.detectChanges(); + expect(tabItems[1].classList).toContain('testCustomClass'); + expectActiveTabs(element, [false, true, false, false]); + + context.tabs[0].customClass = 'otherCustomClass'; + fixture.detectChanges(); + + expect(tabItems[1].classList).not.toContain('testCustomClass'); + expect(tabItems[1].classList).toContain('otherCustomClass'); + expectActiveTabs(element, [false, true, false, false]); + }); }); @Component({ @@ -186,7 +216,7 @@ class TestTabsetComponent { public isVertical:Boolean = false; public isJustified:Boolean = false; public tabs:any[] = [ - {title: 'tab1', content: 'tab1 content'}, + {title: 'tab1', content: 'tab1 content', customClass:'testCustomClass'}, {title: 'tab2', content: 'tab2 content', disabled: true}, {title: 'tab3', content: 'tab3 content', removable: true} ]; diff --git a/src/tabs/tabset.component.ts b/src/tabs/tabset.component.ts index a6d4ba2432..525f0bbeff 100644 --- a/src/tabs/tabset.component.ts +++ b/src/tabs/tabset.component.ts @@ -8,7 +8,7 @@ import { TabsetConfig } from './tabset.config'; selector: 'tabset', template: `