<commit_before>import Offcanvas from '../../src/offcanvas'
import EventHandler from '../../src/dom/event-handler'

/** Test helpers */
import { clearBodyAndDocument, clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
import { isVisible } from '../../src/util'

describe('Offcanvas', () => {
  let fixtureEl

  beforeAll(() => {
    fixtureEl = getFixture()
  })

  afterEach(() => {
    clearFixture()
    document.body.classList.remove('offcanvas-open')
    clearBodyAndDocument()
  })

  beforeEach(() => {
    clearBodyAndDocument()
  })

  describe('VERSION', () => {
    it('should return plugin version', () => {
      expect(Offcanvas.VERSION).toEqual(jasmine.any(String))
    })
  })

  describe('Default', () => {
    it('should return plugin default config', () => {
      expect(Offcanvas.Default).toEqual(jasmine.any(Object))
    })
  })

  describe('DATA_KEY', () => {
    it('should return plugin data key', () => {
      expect(Offcanvas.DATA_KEY).toEqual('bs.offcanvas')
    })
  })

  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._config.keyboard).toBe(true)
      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')

      offCanvasEl.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()
    })

    it('should not hide if esc is pressed but with keyboard = false', () => {
      fixtureEl.innerHTML = '<div class="offcanvas"></div>'

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl, { keyboard: false })
      const keyDownEsc = createEvent('keydown')
      keyDownEsc.key = 'Escape'

      spyOn(offCanvas, 'hide')

      document.dispatchEvent(keyDownEsc)

      expect(offCanvas._config.keyboard).toBe(false)
      expect(offCanvas.hide).not.toHaveBeenCalled()
    })
  })

  describe('config', () => {
    it('should have default values', () => {
      fixtureEl.innerHTML = [
        '<div class="offcanvas">',
        '</div>'
      ].join('')

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl)

      expect(offCanvas._config.backdrop).toEqual(true)
      expect(offCanvas._backdrop._config.isVisible).toEqual(true)
      expect(offCanvas._config.keyboard).toEqual(true)
      expect(offCanvas._config.scroll).toEqual(false)
    })

    it('should read data attributes and override default config', () => {
      fixtureEl.innerHTML = [
        '<div class="offcanvas" data-bs-scroll="true" data-bs-backdrop="false"  data-bs-keyboard="false">',
        '</div>'
      ].join('')

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl)

      expect(offCanvas._config.backdrop).toEqual(false)
      expect(offCanvas._backdrop._config.isVisible).toEqual(false)
      expect(offCanvas._config.keyboard).toEqual(false)
      expect(offCanvas._config.scroll).toEqual(true)
    })

    it('given a config object must override data attributes', () => {
      fixtureEl.innerHTML = [
        '<div class="offcanvas" data-bs-scroll="true" data-bs-backdrop="false"  data-bs-keyboard="false">',
        '</div>'
      ].join('')

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl, {
        backdrop: true,
        keyboard: true,
        scroll: false
      })
      expect(offCanvas._config.backdrop).toEqual(true)
      expect(offCanvas._config.keyboard).toEqual(true)
      expect(offCanvas._config.scroll).toEqual(false)
    })
  })
  describe('options', () => {
    it('if scroll is enabled, should allow body to scroll while offcanvas is open', done => {
      fixtureEl.innerHTML = '<div class="offcanvas"></div>'

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl, { scroll: true })
      const initialOverFlow = document.body.style.overflow

      offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
        expect(document.body.style.overflow).toEqual(initialOverFlow)

        offCanvas.hide()
      })
      offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
        expect(document.body.style.overflow).toEqual(initialOverFlow)
        done()
      })
      offCanvas.show()
    })

    it('if scroll is disabled, should not allow body to scroll while offcanvas is open', done => {
      fixtureEl.innerHTML = '<div class="offcanvas"></div>'

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl, { scroll: false })

      offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
        expect(document.body.style.overflow).toEqual('hidden')

        offCanvas.hide()
      })
      offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
        expect(document.body.style.overflow).not.toEqual('hidden')
        done()
      })
      offCanvas.show()
    })

    it('should hide a shown element if user click on backdrop', done => {
      fixtureEl.innerHTML = '<div class="offcanvas"></div>'
<commit_msg>Fix offcanvas test to expect the initial body `overflow` value (#33927)<commit_after>import Offcanvas from '../../src/offcanvas'
import EventHandler from '../../src/dom/event-handler'

/** Test helpers */
import { clearBodyAndDocument, clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
import { isVisible } from '../../src/util'

describe('Offcanvas', () => {
  let fixtureEl

  beforeAll(() => {
    fixtureEl = getFixture()
  })

  afterEach(() => {
    clearFixture()
    document.body.classList.remove('offcanvas-open')
    clearBodyAndDocument()
  })

  beforeEach(() => {
    clearBodyAndDocument()
  })

  describe('VERSION', () => {
    it('should return plugin version', () => {
      expect(Offcanvas.VERSION).toEqual(jasmine.any(String))
    })
  })

  describe('Default', () => {
    it('should return plugin default config', () => {
      expect(Offcanvas.Default).toEqual(jasmine.any(Object))
    })
  })

  describe('DATA_KEY', () => {
    it('should return plugin data key', () => {
      expect(Offcanvas.DATA_KEY).toEqual('bs.offcanvas')
    })
  })

  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._config.keyboard).toBe(true)
      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')

      offCanvasEl.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()
    })

    it('should not hide if esc is pressed but with keyboard = false', () => {
      fixtureEl.innerHTML = '<div class="offcanvas"></div>'

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl, { keyboard: false })
      const keyDownEsc = createEvent('keydown')
      keyDownEsc.key = 'Escape'

      spyOn(offCanvas, 'hide')

      document.dispatchEvent(keyDownEsc)

      expect(offCanvas._config.keyboard).toBe(false)
      expect(offCanvas.hide).not.toHaveBeenCalled()
    })
  })

  describe('config', () => {
    it('should have default values', () => {
      fixtureEl.innerHTML = [
        '<div class="offcanvas">',
        '</div>'
      ].join('')

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl)

      expect(offCanvas._config.backdrop).toEqual(true)
      expect(offCanvas._backdrop._config.isVisible).toEqual(true)
      expect(offCanvas._config.keyboard).toEqual(true)
      expect(offCanvas._config.scroll).toEqual(false)
    })

    it('should read data attributes and override default config', () => {
      fixtureEl.innerHTML = [
        '<div class="offcanvas" data-bs-scroll="true" data-bs-backdrop="false"  data-bs-keyboard="false">',
        '</div>'
      ].join('')

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl)

      expect(offCanvas._config.backdrop).toEqual(false)
      expect(offCanvas._backdrop._config.isVisible).toEqual(false)
      expect(offCanvas._config.keyboard).toEqual(false)
      expect(offCanvas._config.scroll).toEqual(true)
    })

    it('given a config object must override data attributes', () => {
      fixtureEl.innerHTML = [
        '<div class="offcanvas" data-bs-scroll="true" data-bs-backdrop="false"  data-bs-keyboard="false">',
        '</div>'
      ].join('')

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl, {
        backdrop: true,
        keyboard: true,
        scroll: false
      })
      expect(offCanvas._config.backdrop).toEqual(true)
      expect(offCanvas._config.keyboard).toEqual(true)
      expect(offCanvas._config.scroll).toEqual(false)
    })
  })
  describe('options', () => {
    it('if scroll is enabled, should allow body to scroll while offcanvas is open', done => {
      fixtureEl.innerHTML = '<div class="offcanvas"></div>'

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl, { scroll: true })
      const initialOverFlow = document.body.style.overflow

      offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
        expect(document.body.style.overflow).toEqual(initialOverFlow)

        offCanvas.hide()
      })
      offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
        expect(document.body.style.overflow).toEqual(initialOverFlow)
        done()
      })
      offCanvas.show()
    })

    it('if scroll is disabled, should not allow body to scroll while offcanvas is open', done => {
      fixtureEl.innerHTML = '<div class="offcanvas"></div>'

      const offCanvasEl = fixtureEl.querySelector('.offcanvas')
      const offCanvas = new Offcanvas(offCanvasEl, { scroll: false })
      const initialOverFlow = document.body.style.overflow

      offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
        expect(document.body.style.overflow).toEqual('hidden')

        offCanvas.hide()
      })
      offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
        expect(document.body.style.overflow).toEqual(initialOverFlow)
        done()
      })
      offCanvas.show()
    })

    it('should hide a shown element if user click on backdrop', done => {
      fixtureEl.innerHTML = '<div class="offcanvas"></div>'

{'fraction_non_alphanumeric': 0.14840218238503508, 'fraction_numerical': 0.0003897116134060795, 'mean_word_length': 3.1699707507312316, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1858985', 'n_tokens_mistral': 3725, 'n_tokens_neox': 3729, 'n_words': 689}