index.spec.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. import * as Util from '../../../src/util/index.js'
  2. import { noop } from '../../../src/util/index.js'
  3. import { clearFixture, getFixture } from '../../helpers/fixture.js'
  4. describe('Util', () => {
  5. let fixtureEl
  6. beforeAll(() => {
  7. fixtureEl = getFixture()
  8. })
  9. afterEach(() => {
  10. clearFixture()
  11. })
  12. describe('getUID', () => {
  13. it('should generate uid', () => {
  14. const uid = Util.getUID('bs')
  15. const uid2 = Util.getUID('bs')
  16. expect(uid).not.toEqual(uid2)
  17. })
  18. })
  19. describe('getTransitionDurationFromElement', () => {
  20. it('should get transition from element', () => {
  21. fixtureEl.innerHTML = '<div style="transition: all 300ms ease-out;"></div>'
  22. expect(Util.getTransitionDurationFromElement(fixtureEl.querySelector('div'))).toEqual(300)
  23. })
  24. it('should return 0 if the element is undefined or null', () => {
  25. expect(Util.getTransitionDurationFromElement(null)).toEqual(0)
  26. expect(Util.getTransitionDurationFromElement(undefined)).toEqual(0)
  27. })
  28. it('should return 0 if the element do not possess transition', () => {
  29. fixtureEl.innerHTML = '<div></div>'
  30. expect(Util.getTransitionDurationFromElement(fixtureEl.querySelector('div'))).toEqual(0)
  31. })
  32. })
  33. describe('triggerTransitionEnd', () => {
  34. it('should trigger transitionend event', () => {
  35. return new Promise(resolve => {
  36. fixtureEl.innerHTML = '<div></div>'
  37. const el = fixtureEl.querySelector('div')
  38. const spy = spyOn(el, 'dispatchEvent').and.callThrough()
  39. el.addEventListener('transitionend', () => {
  40. expect(spy).toHaveBeenCalled()
  41. resolve()
  42. })
  43. Util.triggerTransitionEnd(el)
  44. })
  45. })
  46. })
  47. describe('isElement', () => {
  48. it('should detect if the parameter is an element or not and return Boolean', () => {
  49. fixtureEl.innerHTML = [
  50. '<div id="foo" class="test"></div>',
  51. '<div id="bar" class="test"></div>'
  52. ].join('')
  53. const el = fixtureEl.querySelector('#foo')
  54. expect(Util.isElement(el)).toBeTrue()
  55. expect(Util.isElement({})).toBeFalse()
  56. expect(Util.isElement(fixtureEl.querySelectorAll('.test'))).toBeFalse()
  57. })
  58. it('should detect jQuery element', () => {
  59. fixtureEl.innerHTML = '<div></div>'
  60. const el = fixtureEl.querySelector('div')
  61. const fakejQuery = {
  62. 0: el,
  63. jquery: 'foo'
  64. }
  65. expect(Util.isElement(fakejQuery)).toBeTrue()
  66. })
  67. })
  68. describe('getElement', () => {
  69. it('should try to parse element', () => {
  70. fixtureEl.innerHTML = [
  71. '<div id="foo" class="test"></div>',
  72. '<div id="bar" class="test"></div>'
  73. ].join('')
  74. const el = fixtureEl.querySelector('div')
  75. expect(Util.getElement(el)).toEqual(el)
  76. expect(Util.getElement('#foo')).toEqual(el)
  77. expect(Util.getElement('#fail')).toBeNull()
  78. expect(Util.getElement({})).toBeNull()
  79. expect(Util.getElement([])).toBeNull()
  80. expect(Util.getElement()).toBeNull()
  81. expect(Util.getElement(null)).toBeNull()
  82. expect(Util.getElement(fixtureEl.querySelectorAll('.test'))).toBeNull()
  83. const fakejQueryObject = {
  84. 0: el,
  85. jquery: 'foo'
  86. }
  87. expect(Util.getElement(fakejQueryObject)).toEqual(el)
  88. })
  89. })
  90. describe('isVisible', () => {
  91. it('should return false if the element is not defined', () => {
  92. expect(Util.isVisible(null)).toBeFalse()
  93. expect(Util.isVisible(undefined)).toBeFalse()
  94. })
  95. it('should return false if the element provided is not a dom element', () => {
  96. expect(Util.isVisible({})).toBeFalse()
  97. })
  98. it('should return false if the element is not visible with display none', () => {
  99. fixtureEl.innerHTML = '<div style="display: none;"></div>'
  100. const div = fixtureEl.querySelector('div')
  101. expect(Util.isVisible(div)).toBeFalse()
  102. })
  103. it('should return false if the element is not visible with visibility hidden', () => {
  104. fixtureEl.innerHTML = '<div style="visibility: hidden;"></div>'
  105. const div = fixtureEl.querySelector('div')
  106. expect(Util.isVisible(div)).toBeFalse()
  107. })
  108. it('should return false if an ancestor element is display none', () => {
  109. fixtureEl.innerHTML = [
  110. '<div style="display: none;">',
  111. ' <div>',
  112. ' <div>',
  113. ' <div class="content"></div>',
  114. ' </div>',
  115. ' </div>',
  116. '</div>'
  117. ].join('')
  118. const div = fixtureEl.querySelector('.content')
  119. expect(Util.isVisible(div)).toBeFalse()
  120. })
  121. it('should return false if an ancestor element is visibility hidden', () => {
  122. fixtureEl.innerHTML = [
  123. '<div style="visibility: hidden;">',
  124. ' <div>',
  125. ' <div>',
  126. ' <div class="content"></div>',
  127. ' </div>',
  128. ' </div>',
  129. '</div>'
  130. ].join('')
  131. const div = fixtureEl.querySelector('.content')
  132. expect(Util.isVisible(div)).toBeFalse()
  133. })
  134. it('should return true if an ancestor element is visibility hidden, but reverted', () => {
  135. fixtureEl.innerHTML = [
  136. '<div style="visibility: hidden;">',
  137. ' <div style="visibility: visible;">',
  138. ' <div>',
  139. ' <div class="content"></div>',
  140. ' </div>',
  141. ' </div>',
  142. '</div>'
  143. ].join('')
  144. const div = fixtureEl.querySelector('.content')
  145. expect(Util.isVisible(div)).toBeTrue()
  146. })
  147. it('should return true if the element is visible', () => {
  148. fixtureEl.innerHTML = [
  149. '<div>',
  150. ' <div id="element"></div>',
  151. '</div>'
  152. ].join('')
  153. const div = fixtureEl.querySelector('#element')
  154. expect(Util.isVisible(div)).toBeTrue()
  155. })
  156. it('should return false if the element is hidden, but not via display or visibility', () => {
  157. fixtureEl.innerHTML = [
  158. '<details>',
  159. ' <div id="element"></div>',
  160. '</details>'
  161. ].join('')
  162. const div = fixtureEl.querySelector('#element')
  163. expect(Util.isVisible(div)).toBeFalse()
  164. })
  165. it('should return true if its a closed details element', () => {
  166. fixtureEl.innerHTML = '<details id="element"></details>'
  167. const div = fixtureEl.querySelector('#element')
  168. expect(Util.isVisible(div)).toBeTrue()
  169. })
  170. it('should return true if the element is visible inside an open details element', () => {
  171. fixtureEl.innerHTML = [
  172. '<details open>',
  173. ' <div id="element"></div>',
  174. '</details>'
  175. ].join('')
  176. const div = fixtureEl.querySelector('#element')
  177. expect(Util.isVisible(div)).toBeTrue()
  178. })
  179. it('should return true if the element is a visible summary in a closed details element', () => {
  180. fixtureEl.innerHTML = [
  181. '<details>',
  182. ' <summary id="element-1">',
  183. ' <span id="element-2"></span>',
  184. ' </summary>',
  185. '</details>'
  186. ].join('')
  187. const element1 = fixtureEl.querySelector('#element-1')
  188. const element2 = fixtureEl.querySelector('#element-2')
  189. expect(Util.isVisible(element1)).toBeTrue()
  190. expect(Util.isVisible(element2)).toBeTrue()
  191. })
  192. })
  193. describe('isDisabled', () => {
  194. it('should return true if the element is not defined', () => {
  195. expect(Util.isDisabled(null)).toBeTrue()
  196. expect(Util.isDisabled(undefined)).toBeTrue()
  197. expect(Util.isDisabled()).toBeTrue()
  198. })
  199. it('should return true if the element provided is not a dom element', () => {
  200. expect(Util.isDisabled({})).toBeTrue()
  201. expect(Util.isDisabled('test')).toBeTrue()
  202. })
  203. it('should return true if the element has disabled attribute', () => {
  204. fixtureEl.innerHTML = [
  205. '<div>',
  206. ' <div id="element" disabled="disabled"></div>',
  207. ' <div id="element1" disabled="true"></div>',
  208. ' <div id="element2" disabled></div>',
  209. '</div>'
  210. ].join('')
  211. const div = fixtureEl.querySelector('#element')
  212. const div1 = fixtureEl.querySelector('#element1')
  213. const div2 = fixtureEl.querySelector('#element2')
  214. expect(Util.isDisabled(div)).toBeTrue()
  215. expect(Util.isDisabled(div1)).toBeTrue()
  216. expect(Util.isDisabled(div2)).toBeTrue()
  217. })
  218. it('should return false if the element has disabled attribute with "false" value, or doesn\'t have attribute', () => {
  219. fixtureEl.innerHTML = [
  220. '<div>',
  221. ' <div id="element" disabled="false"></div>',
  222. ' <div id="element1" ></div>',
  223. '</div>'
  224. ].join('')
  225. const div = fixtureEl.querySelector('#element')
  226. const div1 = fixtureEl.querySelector('#element1')
  227. expect(Util.isDisabled(div)).toBeFalse()
  228. expect(Util.isDisabled(div1)).toBeFalse()
  229. })
  230. it('should return false if the element is not disabled ', () => {
  231. fixtureEl.innerHTML = [
  232. '<div>',
  233. ' <button id="button"></button>',
  234. ' <select id="select"></select>',
  235. ' <select id="input"></select>',
  236. '</div>'
  237. ].join('')
  238. const el = selector => fixtureEl.querySelector(selector)
  239. expect(Util.isDisabled(el('#button'))).toBeFalse()
  240. expect(Util.isDisabled(el('#select'))).toBeFalse()
  241. expect(Util.isDisabled(el('#input'))).toBeFalse()
  242. })
  243. it('should return true if the element has disabled attribute', () => {
  244. fixtureEl.innerHTML = [
  245. '<div>',
  246. ' <input id="input" disabled="disabled">',
  247. ' <input id="input1" disabled="disabled">',
  248. ' <button id="button" disabled="true"></button>',
  249. ' <button id="button1" disabled="disabled"></button>',
  250. ' <button id="button2" disabled></button>',
  251. ' <select id="select" disabled></select>',
  252. ' <select id="input" disabled></select>',
  253. '</div>'
  254. ].join('')
  255. const el = selector => fixtureEl.querySelector(selector)
  256. expect(Util.isDisabled(el('#input'))).toBeTrue()
  257. expect(Util.isDisabled(el('#input1'))).toBeTrue()
  258. expect(Util.isDisabled(el('#button'))).toBeTrue()
  259. expect(Util.isDisabled(el('#button1'))).toBeTrue()
  260. expect(Util.isDisabled(el('#button2'))).toBeTrue()
  261. expect(Util.isDisabled(el('#input'))).toBeTrue()
  262. })
  263. it('should return true if the element has class "disabled"', () => {
  264. fixtureEl.innerHTML = [
  265. '<div>',
  266. ' <div id="element" class="disabled"></div>',
  267. '</div>'
  268. ].join('')
  269. const div = fixtureEl.querySelector('#element')
  270. expect(Util.isDisabled(div)).toBeTrue()
  271. })
  272. it('should return true if the element has class "disabled" but disabled attribute is false', () => {
  273. fixtureEl.innerHTML = [
  274. '<div>',
  275. ' <input id="input" class="disabled" disabled="false">',
  276. '</div>'
  277. ].join('')
  278. const div = fixtureEl.querySelector('#input')
  279. expect(Util.isDisabled(div)).toBeTrue()
  280. })
  281. })
  282. describe('findShadowRoot', () => {
  283. it('should return null if shadow dom is not available', () => {
  284. // Only for newer browsers
  285. if (!document.documentElement.attachShadow) {
  286. expect().nothing()
  287. return
  288. }
  289. fixtureEl.innerHTML = '<div></div>'
  290. const div = fixtureEl.querySelector('div')
  291. spyOn(document.documentElement, 'attachShadow').and.returnValue(null)
  292. expect(Util.findShadowRoot(div)).toBeNull()
  293. })
  294. it('should return null when we do not find a shadow root', () => {
  295. // Only for newer browsers
  296. if (!document.documentElement.attachShadow) {
  297. expect().nothing()
  298. return
  299. }
  300. spyOn(document, 'getRootNode').and.returnValue(undefined)
  301. expect(Util.findShadowRoot(document)).toBeNull()
  302. })
  303. it('should return the shadow root when found', () => {
  304. // Only for newer browsers
  305. if (!document.documentElement.attachShadow) {
  306. expect().nothing()
  307. return
  308. }
  309. fixtureEl.innerHTML = '<div></div>'
  310. const div = fixtureEl.querySelector('div')
  311. const shadowRoot = div.attachShadow({
  312. mode: 'open'
  313. })
  314. expect(Util.findShadowRoot(shadowRoot)).toEqual(shadowRoot)
  315. shadowRoot.innerHTML = '<button>Shadow Button</button>'
  316. expect(Util.findShadowRoot(shadowRoot.firstChild)).toEqual(shadowRoot)
  317. })
  318. })
  319. describe('noop', () => {
  320. it('should be a function', () => {
  321. expect(Util.noop).toEqual(jasmine.any(Function))
  322. })
  323. })
  324. describe('reflow', () => {
  325. it('should return element offset height to force the reflow', () => {
  326. fixtureEl.innerHTML = '<div></div>'
  327. const div = fixtureEl.querySelector('div')
  328. const spy = spyOnProperty(div, 'offsetHeight')
  329. Util.reflow(div)
  330. expect(spy).toHaveBeenCalled()
  331. })
  332. })
  333. describe('getjQuery', () => {
  334. const fakejQuery = { trigger() {} }
  335. beforeEach(() => {
  336. Object.defineProperty(window, 'jQuery', {
  337. value: fakejQuery,
  338. writable: true
  339. })
  340. })
  341. afterEach(() => {
  342. window.jQuery = undefined
  343. })
  344. it('should return jQuery object when present', () => {
  345. expect(Util.getjQuery()).toEqual(fakejQuery)
  346. })
  347. it('should not return jQuery object when present if data-bs-no-jquery', () => {
  348. document.body.setAttribute('data-bs-no-jquery', '')
  349. expect(window.jQuery).toEqual(fakejQuery)
  350. expect(Util.getjQuery()).toBeNull()
  351. document.body.removeAttribute('data-bs-no-jquery')
  352. })
  353. it('should not return jQuery if not present', () => {
  354. window.jQuery = undefined
  355. expect(Util.getjQuery()).toBeNull()
  356. })
  357. })
  358. describe('onDOMContentLoaded', () => {
  359. it('should execute callbacks when DOMContentLoaded is fired and should not add more than one listener', () => {
  360. const spy = jasmine.createSpy()
  361. const spy2 = jasmine.createSpy()
  362. const spyAdd = spyOn(document, 'addEventListener').and.callThrough()
  363. spyOnProperty(document, 'readyState').and.returnValue('loading')
  364. Util.onDOMContentLoaded(spy)
  365. Util.onDOMContentLoaded(spy2)
  366. document.dispatchEvent(new Event('DOMContentLoaded', {
  367. bubbles: true,
  368. cancelable: true
  369. }))
  370. expect(spy).toHaveBeenCalled()
  371. expect(spy2).toHaveBeenCalled()
  372. expect(spyAdd).toHaveBeenCalledTimes(1)
  373. })
  374. it('should execute callback if readyState is not "loading"', () => {
  375. const spy = jasmine.createSpy()
  376. Util.onDOMContentLoaded(spy)
  377. expect(spy).toHaveBeenCalled()
  378. })
  379. })
  380. describe('defineJQueryPlugin', () => {
  381. const fakejQuery = { fn: {} }
  382. beforeEach(() => {
  383. Object.defineProperty(window, 'jQuery', {
  384. value: fakejQuery,
  385. writable: true
  386. })
  387. })
  388. afterEach(() => {
  389. window.jQuery = undefined
  390. })
  391. it('should define a plugin on the jQuery instance', () => {
  392. const pluginMock = Util.noop
  393. pluginMock.NAME = 'test'
  394. pluginMock.jQueryInterface = Util.noop
  395. Util.defineJQueryPlugin(pluginMock)
  396. expect(fakejQuery.fn.test).toEqual(pluginMock.jQueryInterface)
  397. expect(fakejQuery.fn.test.Constructor).toEqual(pluginMock)
  398. expect(fakejQuery.fn.test.noConflict).toEqual(jasmine.any(Function))
  399. })
  400. })
  401. describe('execute', () => {
  402. it('should execute if arg is function', () => {
  403. const spy = jasmine.createSpy('spy')
  404. Util.execute(spy)
  405. expect(spy).toHaveBeenCalled()
  406. })
  407. it('should execute if arg is function & return the result', () => {
  408. const functionFoo = (num1, num2 = 10) => num1 + num2
  409. const resultFoo = Util.execute(functionFoo, [undefined, 4, 5])
  410. expect(resultFoo).toBe(9)
  411. const resultFoo1 = Util.execute(functionFoo, [undefined, 4])
  412. expect(resultFoo1).toBe(14)
  413. const functionBar = () => 'foo'
  414. const resultBar = Util.execute(functionBar)
  415. expect(resultBar).toBe('foo')
  416. })
  417. it('should not execute if arg is not function & return default argument', () => {
  418. const foo = 'bar'
  419. expect(Util.execute(foo)).toBe('bar')
  420. expect(Util.execute(foo, [], 4)).toBe(4)
  421. })
  422. })
  423. describe('executeAfterTransition', () => {
  424. it('should immediately execute a function when waitForTransition parameter is false', () => {
  425. const el = document.createElement('div')
  426. const callbackSpy = jasmine.createSpy('callback spy')
  427. const eventListenerSpy = spyOn(el, 'addEventListener')
  428. Util.executeAfterTransition(callbackSpy, el, false)
  429. expect(callbackSpy).toHaveBeenCalled()
  430. expect(eventListenerSpy).not.toHaveBeenCalled()
  431. })
  432. it('should execute a function when a transitionend event is dispatched', () => {
  433. const el = document.createElement('div')
  434. const callbackSpy = jasmine.createSpy('callback spy')
  435. spyOn(window, 'getComputedStyle').and.returnValue({
  436. transitionDuration: '0.05s',
  437. transitionDelay: '0s'
  438. })
  439. Util.executeAfterTransition(callbackSpy, el)
  440. el.dispatchEvent(new TransitionEvent('transitionend'))
  441. expect(callbackSpy).toHaveBeenCalled()
  442. })
  443. it('should execute a function after a computed CSS transition duration and there was no transitionend event dispatched', () => {
  444. return new Promise(resolve => {
  445. const el = document.createElement('div')
  446. const callbackSpy = jasmine.createSpy('callback spy')
  447. spyOn(window, 'getComputedStyle').and.returnValue({
  448. transitionDuration: '0.05s',
  449. transitionDelay: '0s'
  450. })
  451. Util.executeAfterTransition(callbackSpy, el)
  452. setTimeout(() => {
  453. expect(callbackSpy).toHaveBeenCalled()
  454. resolve()
  455. }, 70)
  456. })
  457. })
  458. it('should not execute a function a second time after a computed CSS transition duration and if a transitionend event has already been dispatched', () => {
  459. return new Promise(resolve => {
  460. const el = document.createElement('div')
  461. const callbackSpy = jasmine.createSpy('callback spy')
  462. spyOn(window, 'getComputedStyle').and.returnValue({
  463. transitionDuration: '0.05s',
  464. transitionDelay: '0s'
  465. })
  466. Util.executeAfterTransition(callbackSpy, el)
  467. setTimeout(() => {
  468. el.dispatchEvent(new TransitionEvent('transitionend'))
  469. }, 50)
  470. setTimeout(() => {
  471. expect(callbackSpy).toHaveBeenCalledTimes(1)
  472. resolve()
  473. }, 70)
  474. })
  475. })
  476. it('should not trigger a transitionend event if another transitionend event had already happened', () => {
  477. return new Promise(resolve => {
  478. const el = document.createElement('div')
  479. spyOn(window, 'getComputedStyle').and.returnValue({
  480. transitionDuration: '0.05s',
  481. transitionDelay: '0s'
  482. })
  483. Util.executeAfterTransition(noop, el)
  484. // simulate a event dispatched by the browser
  485. el.dispatchEvent(new TransitionEvent('transitionend'))
  486. const dispatchSpy = spyOn(el, 'dispatchEvent').and.callThrough()
  487. setTimeout(() => {
  488. // setTimeout should not have triggered another transitionend event.
  489. expect(dispatchSpy).not.toHaveBeenCalled()
  490. resolve()
  491. }, 70)
  492. })
  493. })
  494. it('should ignore transitionend events from nested elements', () => {
  495. return new Promise(resolve => {
  496. fixtureEl.innerHTML = [
  497. '<div class="outer">',
  498. ' <div class="nested"></div>',
  499. '</div>'
  500. ].join('')
  501. const outer = fixtureEl.querySelector('.outer')
  502. const nested = fixtureEl.querySelector('.nested')
  503. const callbackSpy = jasmine.createSpy('callback spy')
  504. spyOn(window, 'getComputedStyle').and.returnValue({
  505. transitionDuration: '0.05s',
  506. transitionDelay: '0s'
  507. })
  508. Util.executeAfterTransition(callbackSpy, outer)
  509. nested.dispatchEvent(new TransitionEvent('transitionend', {
  510. bubbles: true
  511. }))
  512. setTimeout(() => {
  513. expect(callbackSpy).not.toHaveBeenCalled()
  514. }, 20)
  515. setTimeout(() => {
  516. expect(callbackSpy).toHaveBeenCalled()
  517. resolve()
  518. }, 70)
  519. })
  520. })
  521. })
  522. describe('getNextActiveElement', () => {
  523. it('should return first element if active not exists or not given and shouldGetNext is either true, or false with cycling being disabled', () => {
  524. const array = ['a', 'b', 'c', 'd']
  525. expect(Util.getNextActiveElement(array, '', true, true)).toEqual('a')
  526. expect(Util.getNextActiveElement(array, 'g', true, true)).toEqual('a')
  527. expect(Util.getNextActiveElement(array, '', true, false)).toEqual('a')
  528. expect(Util.getNextActiveElement(array, 'g', true, false)).toEqual('a')
  529. expect(Util.getNextActiveElement(array, '', false, false)).toEqual('a')
  530. expect(Util.getNextActiveElement(array, 'g', false, false)).toEqual('a')
  531. })
  532. it('should return last element if active not exists or not given and shouldGetNext is false but cycling is enabled', () => {
  533. const array = ['a', 'b', 'c', 'd']
  534. expect(Util.getNextActiveElement(array, '', false, true)).toEqual('d')
  535. expect(Util.getNextActiveElement(array, 'g', false, true)).toEqual('d')
  536. })
  537. it('should return next element or same if is last', () => {
  538. const array = ['a', 'b', 'c', 'd']
  539. expect(Util.getNextActiveElement(array, 'a', true, true)).toEqual('b')
  540. expect(Util.getNextActiveElement(array, 'b', true, true)).toEqual('c')
  541. expect(Util.getNextActiveElement(array, 'd', true, false)).toEqual('d')
  542. })
  543. it('should return next element or first, if is last and "isCycleAllowed = true"', () => {
  544. const array = ['a', 'b', 'c', 'd']
  545. expect(Util.getNextActiveElement(array, 'c', true, true)).toEqual('d')
  546. expect(Util.getNextActiveElement(array, 'd', true, true)).toEqual('a')
  547. })
  548. it('should return previous element or same if is first', () => {
  549. const array = ['a', 'b', 'c', 'd']
  550. expect(Util.getNextActiveElement(array, 'b', false, true)).toEqual('a')
  551. expect(Util.getNextActiveElement(array, 'd', false, true)).toEqual('c')
  552. expect(Util.getNextActiveElement(array, 'a', false, false)).toEqual('a')
  553. })
  554. it('should return next element or first, if is last and "isCycleAllowed = true"', () => {
  555. const array = ['a', 'b', 'c', 'd']
  556. expect(Util.getNextActiveElement(array, 'd', false, true)).toEqual('c')
  557. expect(Util.getNextActiveElement(array, 'a', false, true)).toEqual('d')
  558. })
  559. })
  560. })