Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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()
}
Expand Down
20 changes: 18 additions & 2 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Img src="foo" />)
window.Image.prototype.decode = origionalDecode
window.Image.prototype.decode = originalDecode
return p.then(() => {
// i.instance().i.dispatchEvent(new Event('load'))
i.update()
Expand Down Expand Up @@ -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(<Img src="bar10" onLoad={onLoadMock}/>)
const instance = i.instance()
instance.onLoad()
expect(onLoadMock).toHaveBeenCalled()
})

test('onError callback is passed', () => {
const onErrorMock = jest.fn()
const i = shallow(<Img src="bar11" onError={onErrorMock}/>)
const instance = i.instance()
instance.onError();
expect(onErrorMock).toHaveBeenCalled()
})