0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-26 23:54:23 +01:00

Throw a TypeError instead of the generic Error (#32585)

* Change from Error to TypeError

* Convert the `NAME` to upper case to make the consistency in the error message

* Update the remaining tests to be stricter

Co-authored-by: XhmikosR <xhmikosr@gmail.com>
This commit is contained in:
Rohit Sharma 2021-01-14 01:43:30 +05:30 committed by GitHub
parent e34481b6eb
commit c9cd741aff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 26 additions and 45 deletions

View File

@ -263,7 +263,7 @@ class Dropdown extends BaseComponent {
typeof config.reference.getBoundingClientRect !== 'function'
) {
// Popper virtual elements require a getBoundingClientRect method
throw new Error(`${NAME}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`)
throw new TypeError(`${NAME.toUpperCase()}: Option "reference" provided type "object" without a required "getBoundingClientRect" method.`)
}
return config

View File

@ -111,15 +111,14 @@ const typeCheckConfig = (componentName, config, configTypes) => {
Object.keys(configTypes).forEach(property => {
const expectedTypes = configTypes[property]
const value = config[property]
const valueType = value && isElement(value) ?
'element' :
toType(value)
const valueType = value && isElement(value) ? 'element' : toType(value)
if (!new RegExp(expectedTypes).test(valueType)) {
throw new Error(
throw new TypeError(
`${componentName.toUpperCase()}: ` +
`Option "${property}" provided type "${valueType}" ` +
`but expected type "${expectedTypes}".`)
`but expected type "${expectedTypes}".`
)
}
})
}

View File

@ -1136,11 +1136,9 @@ describe('Carousel', () => {
jQueryMock.fn.carousel = Carousel.jQueryInterface
jQueryMock.elements = [div]
try {
expect(() => {
jQueryMock.fn.carousel.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
})

View File

@ -819,11 +819,9 @@ describe('Collapse', () => {
jQueryMock.fn.collapse = Collapse.jQueryInterface
jQueryMock.elements = [div]
try {
expect(() => {
jQueryMock.fn.collapse.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
})

View File

@ -393,13 +393,13 @@ describe('Dropdown', () => {
expect(() => new Dropdown(btnDropdown, {
reference: {}
})).toThrow()
})).toThrowError(TypeError, 'DROPDOWN: Option "reference" provided type "object" without a required "getBoundingClientRect" method.')
expect(() => new Dropdown(btnDropdown, {
reference: {
getBoundingClientRect: 'not-a-function'
}
})).toThrow()
})).toThrowError(TypeError, 'DROPDOWN: Option "reference" provided type "object" without a required "getBoundingClientRect" method.')
// use onFirstUpdate as Poppers internal update is executed async
const dropdown = new Dropdown(btnDropdown, {
@ -1557,11 +1557,9 @@ describe('Dropdown', () => {
jQueryMock.fn.dropdown = Dropdown.jQueryInterface
jQueryMock.elements = [div]
try {
expect(() => {
jQueryMock.fn.dropdown.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
})

View File

@ -1070,11 +1070,9 @@ describe('Modal', () => {
jQueryMock.fn.modal = Modal.jQueryInterface
jQueryMock.elements = [div]
try {
expect(() => {
jQueryMock.fn.modal.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
it('should call show method', () => {

View File

@ -206,11 +206,9 @@ describe('Popover', () => {
jQueryMock.fn.popover = Popover.jQueryInterface
jQueryMock.elements = [popoverEl]
try {
expect(() => {
jQueryMock.fn.popover.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
it('should should call show method', () => {

View File

@ -625,11 +625,9 @@ describe('ScrollSpy', () => {
jQueryMock.fn.scrollspy = ScrollSpy.jQueryInterface
jQueryMock.elements = [div]
try {
expect(() => {
jQueryMock.fn.scrollspy.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
})

View File

@ -397,11 +397,9 @@ describe('Tab', () => {
jQueryMock.fn.tab = Tab.jQueryInterface
jQueryMock.elements = [div]
try {
expect(() => {
jQueryMock.fn.tab.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
})

View File

@ -368,11 +368,9 @@ describe('Toast', () => {
jQueryMock.fn.toast = Toast.jQueryInterface
jQueryMock.elements = [div]
try {
expect(() => {
jQueryMock.fn.toast.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
})

View File

@ -1207,11 +1207,9 @@ describe('Tooltip', () => {
jQueryMock.fn.tooltip = Tooltip.jQueryInterface
jQueryMock.elements = [div]
try {
expect(() => {
jQueryMock.fn.tooltip.call(jQueryMock, action)
} catch (error) {
expect(error.message).toEqual(`No method named "${action}"`)
}
}).toThrowError(TypeError, `No method named "${action}"`)
})
})
})

View File

@ -212,7 +212,7 @@ describe('Util', () => {
expect(() => {
Util.typeCheckConfig(namePlugin, config, defaultType)
}).toThrow(new Error('COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".'))
}).toThrowError(TypeError, 'COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".')
})
it('should return null stringified when null is passed', () => {