diff --git a/src/index.js b/src/index.js
index 88cd674d..b659b37e 100644
--- a/src/index.js
+++ b/src/index.js
@@ -8,7 +8,9 @@ class Img extends Component {
unloader: node,
decode: bool,
src: oneOfType([string, arrayOf(string)]),
- container: func
+ container: func,
+ onLoad: func,
+ onError: func
}
static defaultProps = {
@@ -54,6 +56,7 @@ class Img extends Component {
cache[this.sourceList[this.state.currentIndex]] = true
/* istanbul ignore else */
if (this.i) this.setState({isLoaded: true})
+ if (this.props.onLoad) this.props.onLoad()
}
onError = () => {
@@ -95,9 +98,10 @@ class Img extends Component {
// currentIndex is zero bases, length is 1 based.
// if we have no more sources to try, return - we are done
- if (nextIndex === this.sourceList.length)
+ if (nextIndex === this.sourceList.length) {
+ if (this.props.onError) this.props.onError()
return this.setState({isLoading: false})
-
+ }
// otherwise, try the next img
this.loadImg()
}
diff --git a/src/index.test.js b/src/index.test.js
index a36b99aa..9eaff0ce 100644
--- a/src/index.test.js
+++ b/src/index.test.js
@@ -24,12 +24,12 @@ test('render with src array', () => {
})
test.skip('render with decode=true', () => {
- const origionalDecode = window.Image.prototype.decode
+ const originalDecode = window.Image.prototype.decode
const p = Promise.resolve()
window.Image.prototype.decode = () => p
const i = shallow(
)
- window.Image.prototype.decode = origionalDecode
+ window.Image.prototype.decode = originalDecode
return p.then(() => {
// i.instance().i.dispatchEvent(new Event('load'))
i.update()
@@ -173,3 +173,19 @@ test('onError try the next image. If its cached as error, skip it', () => {
isLoaded: false
})
})
+
+test('onLoad callback is passed', () => {
+ const onLoadMock = jest.fn()
+ const i = shallow(
)
+ const instance = i.instance()
+ instance.onLoad()
+ expect(onLoadMock).toHaveBeenCalled()
+})
+
+test('onError callback is passed', () => {
+ const onErrorMock = jest.fn()
+ const i = shallow(
)
+ const instance = i.instance()
+ instance.onError();
+ expect(onErrorMock).toHaveBeenCalled()
+})