Skip to content

Commit

Permalink
feat #113 (quantity selector): add jQueryInterface unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MewenLeHo authored and julien-deramond committed Feb 24, 2022
1 parent cac060b commit 77b8c57
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion js/tests/unit/quantity-selector.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import QuantitySelector from '../../src/quantity-selector'
import { clearFixture, getFixture, createEvent } from '../helpers/fixture'
import { clearFixture, getFixture, createEvent, jQueryMock } from '../helpers/fixture'

describe('QuantitySelector', () => {
let fixtureEl
Expand Down Expand Up @@ -36,6 +36,49 @@ describe('QuantitySelector', () => {
})
})

describe('jQueryInterface', () => {
it('should create a quantity selector', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')

jQueryMock.fn.quantity_selector = QuantitySelector.jQueryInterface
jQueryMock.elements = [div]

jQueryMock.fn.quantity_selector.call(jQueryMock)

expect(QuantitySelector.getInstance(div)).not.toBeNull()
})

it('should not re create a quantity selector', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')
const quantity_selector = new QuantitySelector(div)

jQueryMock.fn.quantity_selector = QuantitySelector.jQueryInterface
jQueryMock.elements = [div]

jQueryMock.fn.quantity_selector.call(jQueryMock)

expect(QuantitySelector.getInstance(div)).toEqual(quantity_selector)
})

it('should throw error on undefined method', () => {
fixtureEl.innerHTML = '<div></div>'

const div = fixtureEl.querySelector('div')
const action = 'undefinedMethod'

jQueryMock.fn.quantity_selector = QuantitySelector.jQueryInterface
jQueryMock.elements = [div]

expect(() => {
jQueryMock.fn.quantity_selector.call(jQueryMock, action)
}).toThrowError(TypeError, `No method named "${action}"`)
})
})

it('should take care of element either passed as a CSS selector or DOM element (Step Up button)', () => {
fixtureEl.innerHTML = [
'<div class="input-group quantity-selector">',
Expand Down

0 comments on commit 77b8c57

Please sign in to comment.