mirror of
https://github.com/twbs/bootstrap.git
synced 2025-02-14 11:54:23 +01:00
* Add a new offcanvas component * offcanvas.js: switch to string constants and `event.key` * Remove unneeded code * Sass optimizations * Fixes Make sure the element is hidden and not offscreen when inactive fix close icon negative margins Add content in right & bottom examples Re-fix bottom offcanvas height not to cover all viewport * Wording tweaks * update tests and offcanvas class * separate scrollbar functionality and use it in offcanvas * Update .bundlewatch.config.json * fix focus * update btn-close / fix focus on close * add aria-modal and role return focus on trigger when offcanvas is closed change body scrolling timings * move common code to reusable functions * add aria-labelledby * Replace lorem ipsum text * fix focus when offcanvas is closed * updates * revert modal, add tests for scrollbar * show backdrop by default * Update offcanvas.md * Update offcanvas CSS to better match modals - Add background-clip for borders - Move from outline to border (less clever, more consistent) - Add scss-docs in vars * Revamp offcanvas docs - Add static example to show and explain the components - Split live examples and rename them - Simplify example content - Expand docs notes elsewhere - Add sass docs * Add .offcanvas-title instead of .modal-title * Rename offcanvas example to offcanvas-navbar to reflect it's purpose * labelledby references title and not header * Add default shadow to offcanvas * enable offcanvas-body to fill all the remaining wrapper area * Be more descriptive, on Accessibility area * remove redundant classes * ensure in case of an already open offcanvas, not to open another one * bring back backdrop|scroll combinations * bring back toggling class * refactor scrollbar method, plus tests * add check if element is not full-width, according to #30621 * revert all in modal * use documentElement innerWidth * Rename classes to -start and -end Also copyedit some docs wording * omit some things on scrollbar * PASS BrowserStack tests -- IOS devices, Android devices and Browsers on Mac, hide scrollbar by default and appear it, only while scrolling. * Rename '_handleClosing' to '_addEventListeners' * change pipe usage to comma * change Data.getData to Data.get Co-authored-by: XhmikosR <xhmikosr@gmail.com> Co-authored-by: Martijn Cuppens <martijn.cuppens@gmail.com> Co-authored-by: Mark Otto <markdotto@gmail.com>
325 lines
8.8 KiB
JavaScript
325 lines
8.8 KiB
JavaScript
import OffCanvas from '../../src/offcanvas'
|
|
import EventHandler from '../../src/dom/event-handler'
|
|
|
|
/** Test helpers */
|
|
import { clearFixture, getFixture, jQueryMock, createEvent } from '../helpers/fixture'
|
|
|
|
describe('OffCanvas', () => {
|
|
let fixtureEl
|
|
|
|
beforeAll(() => {
|
|
fixtureEl = getFixture()
|
|
})
|
|
|
|
afterEach(() => {
|
|
clearFixture()
|
|
document.body.classList.remove('offcanvas-open')
|
|
})
|
|
|
|
describe('VERSION', () => {
|
|
it('should return plugin version', () => {
|
|
expect(OffCanvas.VERSION).toEqual(jasmine.any(String))
|
|
})
|
|
})
|
|
|
|
describe('constructor', () => {
|
|
it('should call hide when a element with data-bs-dismiss="offcanvas" is clicked', () => {
|
|
fixtureEl.innerHTML = [
|
|
'<div class="offcanvas">',
|
|
' <a href="#" data-bs-dismiss="offcanvas">Close</a>',
|
|
'</div>'
|
|
].join('')
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('.offcanvas')
|
|
const closeEl = fixtureEl.querySelector('a')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
spyOn(offCanvas, 'hide')
|
|
|
|
closeEl.click()
|
|
|
|
expect(offCanvas.hide).toHaveBeenCalled()
|
|
})
|
|
|
|
it('should hide if esc is pressed', () => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('.offcanvas')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
const keyDownEsc = createEvent('keydown')
|
|
keyDownEsc.key = 'Escape'
|
|
|
|
spyOn(offCanvas, 'hide')
|
|
|
|
document.dispatchEvent(keyDownEsc)
|
|
|
|
expect(offCanvas.hide).toHaveBeenCalled()
|
|
})
|
|
|
|
it('should not hide if esc is not pressed', () => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('.offcanvas')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
const keydownTab = createEvent('keydown')
|
|
keydownTab.key = 'Tab'
|
|
|
|
spyOn(offCanvas, 'hide')
|
|
|
|
document.dispatchEvent(keydownTab)
|
|
|
|
expect(offCanvas.hide).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('toggle', () => {
|
|
it('should call show method if show class is not present', () => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('.offcanvas')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
spyOn(offCanvas, 'show')
|
|
|
|
offCanvas.toggle()
|
|
|
|
expect(offCanvas.show).toHaveBeenCalled()
|
|
})
|
|
|
|
it('should call hide method if show class is present', () => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas show"></div>'
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('.show')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
spyOn(offCanvas, 'hide')
|
|
|
|
offCanvas.toggle()
|
|
|
|
expect(offCanvas.hide).toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('show', () => {
|
|
it('should do nothing if already shown', () => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas show"></div>'
|
|
|
|
spyOn(EventHandler, 'trigger')
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('div')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
offCanvas.show()
|
|
|
|
expect(EventHandler.trigger).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should show a hidden element', done => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('div')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
|
|
expect(offCanvasEl.classList.contains('show')).toEqual(true)
|
|
done()
|
|
})
|
|
|
|
offCanvas.show()
|
|
})
|
|
|
|
it('should not fire shown when show is prevented', done => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('div')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
const expectEnd = () => {
|
|
setTimeout(() => {
|
|
expect().nothing()
|
|
done()
|
|
}, 10)
|
|
}
|
|
|
|
offCanvasEl.addEventListener('show.bs.offcanvas', e => {
|
|
e.preventDefault()
|
|
expectEnd()
|
|
})
|
|
|
|
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
|
|
throw new Error('should not fire shown event')
|
|
})
|
|
|
|
offCanvas.show()
|
|
})
|
|
})
|
|
|
|
describe('hide', () => {
|
|
it('should do nothing if already shown', () => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
|
|
|
|
spyOn(EventHandler, 'trigger')
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('div')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
offCanvas.hide()
|
|
|
|
expect(EventHandler.trigger).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should hide a shown element', done => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas show"></div>'
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('div')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
|
|
expect(offCanvasEl.classList.contains('show')).toEqual(false)
|
|
done()
|
|
})
|
|
|
|
offCanvas.hide()
|
|
})
|
|
|
|
it('should not fire hidden when hide is prevented', done => {
|
|
fixtureEl.innerHTML = '<div class="offcanvas show"></div>'
|
|
|
|
const offCanvasEl = fixtureEl.querySelector('div')
|
|
const offCanvas = new OffCanvas(offCanvasEl)
|
|
|
|
const expectEnd = () => {
|
|
setTimeout(() => {
|
|
expect().nothing()
|
|
done()
|
|
}, 10)
|
|
}
|
|
|
|
offCanvasEl.addEventListener('hide.bs.offcanvas', e => {
|
|
e.preventDefault()
|
|
expectEnd()
|
|
})
|
|
|
|
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
|
|
throw new Error('should not fire hidden event')
|
|
})
|
|
|
|
offCanvas.hide()
|
|
})
|
|
})
|
|
|
|
describe('data-api', () => {
|
|
it('should not prevent event for input', done => {
|
|
fixtureEl.innerHTML = [
|
|
'<input type="checkbox" data-bs-toggle="offcanvas" data-bs-target="#offcanvasdiv1" />',
|
|
'<div id="offcanvasdiv1" class="offcanvas"></div>'
|
|
].join('')
|
|
|
|
const target = fixtureEl.querySelector('input')
|
|
const offCanvasEl = fixtureEl.querySelector('#offcanvasdiv1')
|
|
|
|
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
|
|
expect(offCanvasEl.classList.contains('show')).toEqual(true)
|
|
expect(target.checked).toEqual(true)
|
|
done()
|
|
})
|
|
|
|
target.click()
|
|
})
|
|
|
|
it('should not call toggle on disabled elements', () => {
|
|
fixtureEl.innerHTML = [
|
|
'<a href="#" data-bs-toggle="offcanvas" data-bs-target="#offcanvasdiv1" class="disabled"></a>',
|
|
'<div id="offcanvasdiv1" class="offcanvas"></div>'
|
|
].join('')
|
|
|
|
const target = fixtureEl.querySelector('a')
|
|
|
|
spyOn(OffCanvas.prototype, 'toggle')
|
|
|
|
target.click()
|
|
|
|
expect(OffCanvas.prototype.toggle).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('jQueryInterface', () => {
|
|
it('should create an offcanvas', () => {
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
jQueryMock.fn.offcanvas = OffCanvas.jQueryInterface
|
|
jQueryMock.elements = [div]
|
|
|
|
jQueryMock.fn.offcanvas.call(jQueryMock)
|
|
|
|
expect(OffCanvas.getInstance(div)).toBeDefined()
|
|
})
|
|
|
|
it('should not re create an offcanvas', () => {
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
const offCanvas = new OffCanvas(div)
|
|
|
|
jQueryMock.fn.offcanvas = OffCanvas.jQueryInterface
|
|
jQueryMock.elements = [div]
|
|
|
|
jQueryMock.fn.offcanvas.call(jQueryMock)
|
|
|
|
expect(OffCanvas.getInstance(div)).toEqual(offCanvas)
|
|
})
|
|
|
|
it('should throw error on undefined method', () => {
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
const action = 'undefinedMethod'
|
|
|
|
jQueryMock.fn.offcanvas = OffCanvas.jQueryInterface
|
|
jQueryMock.elements = [div]
|
|
|
|
try {
|
|
jQueryMock.fn.offcanvas.call(jQueryMock, action)
|
|
} catch (error) {
|
|
expect(error.message).toEqual(`No method named "${action}"`)
|
|
}
|
|
})
|
|
|
|
it('should call offcanvas method', () => {
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
spyOn(OffCanvas.prototype, 'show')
|
|
|
|
jQueryMock.fn.offcanvas = OffCanvas.jQueryInterface
|
|
jQueryMock.elements = [div]
|
|
|
|
jQueryMock.fn.offcanvas.call(jQueryMock, 'show')
|
|
expect(OffCanvas.prototype.show).toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('getInstance', () => {
|
|
it('should return offcanvas instance', () => {
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
const offCanvas = new OffCanvas(div)
|
|
|
|
expect(OffCanvas.getInstance(div)).toEqual(offCanvas)
|
|
expect(OffCanvas.getInstance(div)).toBeInstanceOf(OffCanvas)
|
|
})
|
|
|
|
it('should return null when there is no offcanvas instance', () => {
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
expect(OffCanvas.getInstance(div)).toEqual(null)
|
|
})
|
|
})
|
|
})
|