From 233f3fb1ce766d73273276e59a75ccec08673573 Mon Sep 17 00:00:00 2001 From: Johann-S Date: Mon, 22 Jul 2019 15:24:17 +0200 Subject: [PATCH] rewrite tab unit tests --- build/build-plugins.js | 2 +- js/index.esm.js | 2 +- js/index.umd.js | 2 +- js/src/{ => tab}/tab.js | 10 +- js/src/tab/tab.spec.js | 593 +++++++++++++++++++++++++++++++ js/tests/unit/.eslintrc.json | 62 ---- js/tests/unit/tab.js | 530 --------------------------- js/tests/unit/tests-polyfills.js | 28 -- 8 files changed, 601 insertions(+), 628 deletions(-) rename js/src/{ => tab}/tab.js (97%) create mode 100644 js/src/tab/tab.spec.js delete mode 100644 js/tests/unit/.eslintrc.json delete mode 100644 js/tests/unit/tab.js delete mode 100644 js/tests/unit/tests-polyfills.js diff --git a/build/build-plugins.js b/build/build-plugins.js index febe575c94..7aa2a4e529 100644 --- a/build/build-plugins.js +++ b/build/build-plugins.js @@ -40,7 +40,7 @@ const bsPlugins = { Modal: path.resolve(__dirname, '../js/src/modal/modal.js'), Popover: path.resolve(__dirname, '../js/src/popover/popover.js'), ScrollSpy: path.resolve(__dirname, '../js/src/scrollspy/scrollspy.js'), - Tab: path.resolve(__dirname, '../js/src/tab.js'), + Tab: path.resolve(__dirname, '../js/src/tab/tab.js'), Toast: path.resolve(__dirname, '../js/src/toast/toast.js'), Tooltip: path.resolve(__dirname, '../js/src/tooltip/tooltip.js') } diff --git a/js/index.esm.js b/js/index.esm.js index e8c9ab6391..18b12a454f 100644 --- a/js/index.esm.js +++ b/js/index.esm.js @@ -13,7 +13,7 @@ import Dropdown from './src/dropdown/dropdown' import Modal from './src/modal/modal' import Popover from './src/popover/popover' import ScrollSpy from './src/scrollspy/scrollspy' -import Tab from './src/tab' +import Tab from './src/tab/tab' import Toast from './src/toast/toast' import Tooltip from './src/tooltip/tooltip' diff --git a/js/index.umd.js b/js/index.umd.js index 207b952ca5..17657f38f8 100644 --- a/js/index.umd.js +++ b/js/index.umd.js @@ -13,7 +13,7 @@ import Dropdown from './src/dropdown/dropdown' import Modal from './src/modal/modal' import Popover from './src/popover/popover' import ScrollSpy from './src/scrollspy/scrollspy' -import Tab from './src/tab' +import Tab from './src/tab/tab' import Toast from './src/toast/toast' import Tooltip from './src/tooltip/tooltip' diff --git a/js/src/tab.js b/js/src/tab/tab.js similarity index 97% rename from js/src/tab.js rename to js/src/tab/tab.js index b9db64baa4..e882ff1d2a 100644 --- a/js/src/tab.js +++ b/js/src/tab/tab.js @@ -13,10 +13,10 @@ import { getTransitionDurationFromElement, makeArray, reflow -} from './util/index' -import Data from './dom/data' -import EventHandler from './dom/event-handler' -import SelectorEngine from './dom/selector-engine' +} from '../util/index' +import Data from '../dom/data' +import EventHandler from '../dom/event-handler' +import SelectorEngine from '../dom/selector-engine' /** * ------------------------------------------------------------------------ @@ -253,7 +253,7 @@ EventHandler.on(document, Event.CLICK_DATA_API, Selector.DATA_TOGGLE, function ( * ------------------------------------------------------------------------ * add .tab to jQuery only if jQuery is present */ - +/* istanbul ignore if */ if (typeof $ !== 'undefined') { const JQUERY_NO_CONFLICT = $.fn[NAME] $.fn[NAME] = Tab._jQueryInterface diff --git a/js/src/tab/tab.spec.js b/js/src/tab/tab.spec.js new file mode 100644 index 0000000000..3fae366d19 --- /dev/null +++ b/js/src/tab/tab.spec.js @@ -0,0 +1,593 @@ +import Tab from './tab' + +/** Test helpers */ +import { getFixture, clearFixture, jQueryMock } from '../../tests/helpers/fixture' + +describe('Tab', () => { + let fixtureEl + + beforeAll(() => { + fixtureEl = getFixture() + }) + + afterEach(() => { + clearFixture() + }) + + describe('VERSION', () => { + it('should return plugin version', () => { + expect(Tab.VERSION).toEqual(jasmine.any(String)) + }) + }) + + describe('show', () => { + it('should activate element by tab id', done => { + fixtureEl.innerHTML = [ + '', + '' + ].join('') + + const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') + const tab = new Tab(profileTriggerEl) + + profileTriggerEl.addEventListener('shown.bs.tab', () => { + expect(fixtureEl.querySelector('#profile').classList.contains('active')).toEqual(true) + expect(profileTriggerEl.getAttribute('aria-selected')).toEqual('true') + done() + }) + + tab.show() + }) + + it('should activate element by tab id in ordered list', done => { + fixtureEl.innerHTML = [ + '', + '
' + ].join('') + + const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') + const tab = new Tab(profileTriggerEl) + + profileTriggerEl.addEventListener('shown.bs.tab', () => { + expect(fixtureEl.querySelector('#profile').classList.contains('active')).toEqual(true) + done() + }) + + tab.show() + }) + + it('should activate element by tab id in nav list', done => { + fixtureEl.innerHTML = [ + '', + '' + ].join('') + + const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') + const tab = new Tab(profileTriggerEl) + + profileTriggerEl.addEventListener('shown.bs.tab', () => { + expect(fixtureEl.querySelector('#profile').classList.contains('active')).toEqual(true) + done() + }) + + tab.show() + }) + + it('should activate element by tab id in list group', done => { + fixtureEl.innerHTML = [ + '
', + ' Home', + ' Profile', + '
', + '' + ].join('') + + const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') + const tab = new Tab(profileTriggerEl) + + profileTriggerEl.addEventListener('shown.bs.tab', () => { + expect(fixtureEl.querySelector('#profile').classList.contains('active')).toEqual(true) + done() + }) + + tab.show() + }) + + it('should not fire shown when show is prevented', done => { + fixtureEl.innerHTML = '' + + const navEl = fixtureEl.querySelector('div') + const tab = new Tab(navEl) + const expectDone = () => { + setTimeout(() => { + expect().nothing() + done() + }, 30) + } + + navEl.addEventListener('show.bs.tab', ev => { + ev.preventDefault() + expectDone() + }) + + navEl.addEventListener('shown.bs.tab', () => { + throw new Error('should not trigger shown event') + }) + + tab.show() + }) + + it('should not fire shown when tab is already active', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
', + '
', + '
' + ].join('') + + const triggerActive = fixtureEl.querySelector('a.active') + const tab = new Tab(triggerActive) + + triggerActive.addEventListener('shown.bs.tab', () => { + throw new Error('should not trigger shown event') + }) + + tab.show() + setTimeout(() => { + expect().nothing() + done() + }, 30) + }) + + it('should not fire shown when tab is disabled', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
', + '
', + '
' + ].join('') + + const triggerDisabled = fixtureEl.querySelector('a.disabled') + const tab = new Tab(triggerDisabled) + + triggerDisabled.addEventListener('shown.bs.tab', () => { + throw new Error('should not trigger shown event') + }) + + tab.show() + setTimeout(() => { + expect().nothing() + done() + }, 30) + }) + + it('show and shown events should reference correct relatedTarget', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
', + '
', + '
' + ].join('') + + const secondTabTrigger = fixtureEl.querySelector('#triggerProfile') + const secondTab = new Tab(secondTabTrigger) + + secondTabTrigger.addEventListener('show.bs.tab', ev => { + expect(ev.relatedTarget.hash).toEqual('#home') + }) + + secondTabTrigger.addEventListener('shown.bs.tab', ev => { + expect(ev.relatedTarget.hash).toEqual('#home') + expect(secondTabTrigger.getAttribute('aria-selected')).toEqual('true') + expect(fixtureEl.querySelector('a:not(.active)').getAttribute('aria-selected')).toEqual('false') + done() + }) + + secondTab.show() + }) + + it('should fire hide and hidden events', done => { + fixtureEl.innerHTML = [ + '' + ].join('') + + const triggerList = fixtureEl.querySelectorAll('a') + const firstTab = new Tab(triggerList[0]) + const secondTab = new Tab(triggerList[1]) + + let hideCalled = false + triggerList[0].addEventListener('shown.bs.tab', () => { + secondTab.show() + }) + + triggerList[0].addEventListener('hide.bs.tab', ev => { + hideCalled = true + expect(ev.relatedTarget.hash).toEqual('#profile') + }) + + triggerList[0].addEventListener('hidden.bs.tab', ev => { + expect(hideCalled).toEqual(true) + expect(ev.relatedTarget.hash).toEqual('#profile') + done() + }) + + firstTab.show() + }) + + it('should not fire hidden when hide is prevented', done => { + fixtureEl.innerHTML = [ + '' + ].join('') + + const triggerList = fixtureEl.querySelectorAll('a') + const firstTab = new Tab(triggerList[0]) + const secondTab = new Tab(triggerList[1]) + const expectDone = () => { + setTimeout(() => { + expect().nothing() + done() + }, 30) + } + + triggerList[0].addEventListener('shown.bs.tab', () => { + secondTab.show() + }) + + triggerList[0].addEventListener('hide.bs.tab', ev => { + ev.preventDefault() + expectDone() + }) + + triggerList[0].addEventListener('hidden.bs.tab', () => { + throw new Error('should not trigger hidden') + }) + + firstTab.show() + }) + + it('should handle removed tabs', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
test 1
', + '
test 2
', + '
test 3
', + '
' + ].join('') + + const secondNavEl = fixtureEl.querySelector('#secondNav') + const btnCloseEl = fixtureEl.querySelector('#btnClose') + const secondNavTab = new Tab(secondNavEl) + + secondNavEl.addEventListener('shown.bs.tab', () => { + expect(fixtureEl.querySelectorAll('.nav-tab').length).toEqual(2) + done() + }) + + btnCloseEl.addEventListener('click', () => { + const linkEl = btnCloseEl.parentNode + const liEl = linkEl.parentNode + const tabId = linkEl.getAttribute('href') + const tabIdEl = fixtureEl.querySelector(tabId) + + liEl.parentNode.removeChild(liEl) + tabIdEl.parentNode.removeChild(tabIdEl) + secondNavTab.show() + }) + + btnCloseEl.click() + }) + }) + + describe('dispose', () => { + it('should dispose a tab', () => { + fixtureEl.innerHTML = '
' + + const el = fixtureEl.querySelector('div') + const tab = new Tab(fixtureEl.querySelector('div')) + + expect(Tab._getInstance(el)).not.toBeNull() + + tab.dispose() + + expect(Tab._getInstance(el)).toBeNull() + }) + }) + + describe('_jQueryInterface', () => { + it('should create a tab', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + + jQueryMock.fn.tab = Tab._jQueryInterface + jQueryMock.elements = [div] + + jQueryMock.fn.tab.call(jQueryMock) + + expect(Tab._getInstance(div)).toBeDefined() + }) + + it('should not re create a tab', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + const tab = new Tab(div) + + jQueryMock.fn.tab = Tab._jQueryInterface + jQueryMock.elements = [div] + + jQueryMock.fn.tab.call(jQueryMock) + + expect(Tab._getInstance(div)).toEqual(tab) + }) + + it('should call a tab method', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + const tab = new Tab(div) + + spyOn(tab, 'show') + + jQueryMock.fn.tab = Tab._jQueryInterface + jQueryMock.elements = [div] + + jQueryMock.fn.tab.call(jQueryMock, 'show') + + expect(Tab._getInstance(div)).toEqual(tab) + expect(tab.show).toHaveBeenCalled() + }) + + it('should throw error on undefined method', () => { + fixtureEl.innerHTML = '
' + + const div = fixtureEl.querySelector('div') + const action = 'undefinedMethod' + + jQueryMock.fn.tab = Tab._jQueryInterface + jQueryMock.elements = [div] + + try { + jQueryMock.fn.tab.call(jQueryMock, action) + } catch (error) { + expect(error.message).toEqual(`No method named "${action}"`) + } + }) + }) + + describe('_getInstance', () => { + it('should return null if there is no instance', () => { + expect(Tab._getInstance(fixtureEl)).toEqual(null) + }) + + it('should return this instance', () => { + fixtureEl.innerHTML = '
' + + const divEl = fixtureEl.querySelector('div') + const tab = new Tab(divEl) + + expect(Tab._getInstance(divEl)).toEqual(tab) + }) + }) + + describe('data-api', () => { + it('should create dynamicaly a tab', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
', + '
', + '
' + ].join('') + + const secondTabTrigger = fixtureEl.querySelector('#triggerProfile') + + secondTabTrigger.addEventListener('shown.bs.tab', () => { + expect(secondTabTrigger.classList.contains('active')).toEqual(true) + expect(fixtureEl.querySelector('#profile').classList.contains('active')).toEqual(true) + done() + }) + + secondTabTrigger.click() + }) + + it('selected tab should deactivate previous selected link in dropdown', () => { + fixtureEl.innerHTML = [ + '' + ].join('') + + const firstLiLinkEl = fixtureEl.querySelector('li:first-child a') + + firstLiLinkEl.click() + expect(firstLiLinkEl.classList.contains('active')).toEqual(true) + expect(fixtureEl.querySelector('li:last-child a').classList.contains('active')).toEqual(false) + expect(fixtureEl.querySelector('li:last-child .dropdown-menu a:first-child').classList.contains('active')).toEqual(false) + }) + + it('should handle nested tabs', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
', + ' ', + '
', + '
Nested Tab1 Content
', + '
Nested Tab2 Content
', + '
', + '
', + '
Tab2 Content
', + '
Tab3 Content
', + '
' + ].join('') + + const tab1El = fixtureEl.querySelector('#tab1') + const tabNested2El = fixtureEl.querySelector('#tabNested2') + const xTab1El = fixtureEl.querySelector('#x-tab1') + + tabNested2El.addEventListener('shown.bs.tab', () => { + expect(xTab1El.classList.contains('active')).toEqual(true) + done() + }) + + tab1El.addEventListener('shown.bs.tab', () => { + expect(xTab1El.classList.contains('active')).toEqual(true) + tabNested2El.click() + }) + + tab1El.click() + }) + + it('should not remove fade class if no active pane is present', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
', + '
', + '
' + ].join('') + + const triggerTabProfileEl = fixtureEl.querySelector('#tab-profile') + const triggerTabHomeEl = fixtureEl.querySelector('#tab-home') + const tabProfileEl = fixtureEl.querySelector('#profile') + const tabHomeEl = fixtureEl.querySelector('#home') + + triggerTabProfileEl.addEventListener('shown.bs.tab', () => { + expect(tabProfileEl.classList.contains('fade')).toEqual(true) + expect(tabProfileEl.classList.contains('show')).toEqual(true) + + triggerTabHomeEl.addEventListener('shown.bs.tab', () => { + expect(tabProfileEl.classList.contains('fade')).toEqual(true) + expect(tabProfileEl.classList.contains('show')).toEqual(false) + + expect(tabHomeEl.classList.contains('fade')).toEqual(true) + expect(tabHomeEl.classList.contains('show')).toEqual(true) + + done() + }) + + triggerTabHomeEl.click() + }) + + triggerTabProfileEl.click() + }) + + it('should not add show class to tab panes if there is no `.fade` class', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
test 1
', + '
test 2
', + '
' + ].join('') + + const secondNavEl = fixtureEl.querySelector('#secondNav') + + secondNavEl.addEventListener('shown.bs.tab', () => { + expect(fixtureEl.querySelectorAll('.show').length).toEqual(0) + done() + }) + + secondNavEl.click() + }) + + it('should add show class to tab panes if there is a `.fade` class', done => { + fixtureEl.innerHTML = [ + '', + '
', + '
test 1
', + '
test 2
', + '
' + ].join('') + + const secondNavEl = fixtureEl.querySelector('#secondNav') + + secondNavEl.addEventListener('shown.bs.tab', () => { + expect(fixtureEl.querySelectorAll('.show').length).toEqual(1) + done() + }) + + secondNavEl.click() + }) + }) +}) diff --git a/js/tests/unit/.eslintrc.json b/js/tests/unit/.eslintrc.json deleted file mode 100644 index b4b9ac7e8d..0000000000 --- a/js/tests/unit/.eslintrc.json +++ /dev/null @@ -1,62 +0,0 @@ -{ - "root": true, - "env": { - "jquery": true, - "qunit": true - }, - "globals": { - "bootstrap": false, - "sinon": false, - "Util": false, - "Sanitizer": false, - "Data": false, - "Alert": false, - "Button": false, - "Carousel": false, - "Simulator": false, - "Toast": false, - "EventHandler": false, - "Manipulator": false, - "SelectorEngine": false - }, - "parserOptions": { - "ecmaVersion": 5, - "sourceType": "script" - }, - "extends": [ - "plugin:unicorn/recommended", - "xo", - "xo/browser" - ], - "rules": { - "capitalized-comments": "off", - "indent": [ - "error", - 2, - { - "MemberExpression": "off", - "SwitchCase": 1 - } - ], - "multiline-ternary": [ - "error", - "always-multiline" - ], - "new-cap": "off", - "object-curly-spacing": [ - "error", - "always" - ], - "semi": [ - "error", - "never" - ], - "strict": "error", - "unicorn/no-unused-properties": "error", - "unicorn/prefer-includes": "off", - "unicorn/prefer-node-append": "off", - "unicorn/prefer-node-remove": "off", - "unicorn/prefer-query-selector": "off", - "unicorn/prevent-abbreviations": "off" - } -} diff --git a/js/tests/unit/tab.js b/js/tests/unit/tab.js deleted file mode 100644 index 827fb707c1..0000000000 --- a/js/tests/unit/tab.js +++ /dev/null @@ -1,530 +0,0 @@ -$(function () { - 'use strict' - - var Tab = typeof window.bootstrap === 'undefined' ? window.Tab : window.bootstrap.Tab - - QUnit.module('tabs plugin') - - QUnit.test('should be defined on jquery object', function (assert) { - assert.expect(1) - assert.ok($(document.body).tab, 'tabs method is defined') - }) - - QUnit.module('tabs', { - beforeEach: function () { - // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode - $.fn.bootstrapTab = $.fn.tab.noConflict() - }, - afterEach: function () { - $.fn.tab = $.fn.bootstrapTab - delete $.fn.bootstrapTab - $('#qunit-fixture').html('') - } - }) - - QUnit.test('should provide no conflict', function (assert) { - assert.expect(1) - assert.strictEqual(typeof $.fn.tab, 'undefined', 'tab was set back to undefined (org value)') - }) - - QUnit.test('should throw explicit error on undefined method', function (assert) { - assert.expect(1) - var $el = $('
') - $el.bootstrapTab() - try { - $el.bootstrapTab('noMethod') - } catch (error) { - assert.strictEqual(error.message, 'No method named "noMethod"') - } - }) - - QUnit.test('should return jquery collection containing the element', function (assert) { - assert.expect(2) - var $el = $('
') - var $tab = $el.bootstrapTab() - assert.ok($tab instanceof $, 'returns jquery collection') - assert.strictEqual($tab[0], $el[0], 'collection contains element') - }) - - QUnit.test('should activate element by tab id', function (assert) { - assert.expect(2) - var tabsHTML = '' - - $('
').appendTo('#qunit-fixture') - - $(tabsHTML).find('li:last-child a').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile') - - $(tabsHTML).find('li:first-child a').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home') - }) - - QUnit.test('should activate element by tab id', function (assert) { - assert.expect(2) - var pillsHTML = '' - - $('
').appendTo('#qunit-fixture') - - $(pillsHTML).find('li:last-child a').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile') - - $(pillsHTML).find('li:first-child a').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home') - }) - - QUnit.test('should activate element by tab id in ordered list', function (assert) { - assert.expect(2) - var pillsHTML = '' - - $('
').appendTo('#qunit-fixture') - - $(pillsHTML).find('li:last-child a').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile') - - $(pillsHTML).find('li:first-child a').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home') - }) - - QUnit.test('should activate element by tab id in nav list', function (assert) { - assert.expect(2) - var tabsHTML = '' - - $('').appendTo('#qunit-fixture') - - $(tabsHTML).find('a:last-child').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile') - - $(tabsHTML).find('a:first-child').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home') - }) - - QUnit.test('should activate element by tab id in list group', function (assert) { - assert.expect(2) - var tabsHTML = '
' + - 'Home' + - 'Profile' + - '
' - - $('').appendTo('#qunit-fixture') - - $(tabsHTML).find('a:last-child').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'profile') - - $(tabsHTML).find('a:first-child').bootstrapTab('show') - assert.strictEqual($('#qunit-fixture').find('.active').attr('id'), 'home') - }) - - QUnit.test('should not fire shown when show is prevented', function (assert) { - assert.expect(1) - var done = assert.async() - - $('