|
import {expect} from 'chai'; |
|
import sinon from 'sinon'; |
|
import {shallow, mount} from 'enzyme'; |
|
import React from 'react'; |
|
import axios from 'axios'; |
|
import makePaginable from './'; |
|
|
|
const DummyPromise = new Promise(() => {}); |
|
const DummyComponent = () => <div />; |
|
const DummyContext = { router: { location: { query: {} } } }; |
|
|
|
describe('PaginableWrapper', () => { |
|
let sandbox; |
|
beforeEach(() => sandbox = sinon.sandbox.create()); |
|
afterEach(() => sandbox.restore()); |
|
|
|
it('should throw when url is not provided.', () => { |
|
expect(() => { |
|
makePaginable(null) |
|
}).to.throw(/The url must be provided, none provided./); |
|
}); |
|
|
|
it('should throw when url is not a string.', () => { |
|
expect(() => { |
|
makePaginable(null, {}) |
|
}).to.throw(/The url must be a string, `object` provided./); |
|
}); |
|
|
|
it('should request data on mount with the given url.', () => { |
|
sandbox.stub(axios, 'get').returns(DummyPromise); |
|
const Wrapped = makePaginable(DummyComponent, 'hehe'); |
|
const wrapper = mount(<Wrapped />, { context: DummyContext }); |
|
|
|
expect(axios.get.calledOnce, 'should request').to.be.true; |
|
expect(axios.get.getCall(0).args[0], 'should request to given url').to.contain('hehe'); |
|
}); |
|
|
|
it('should request data on query update.', () => { |
|
const Wrapped = makePaginable(DummyComponent, 'hehe'); |
|
const handleRequest = sandbox.spy(Wrapped.prototype, 'handleRequest'); |
|
const wrapper = mount(<Wrapped />, { context: DummyContext }); |
|
|
|
expect(handleRequest.calledOnce, 'should request').to.be.true; |
|
wrapper.setContext({ router: { location: { query: { page: 2 }}}}); |
|
expect(handleRequest.calledTwice, 'should requested again').to.be.true; |
|
}); |
|
|
|
it('should not request data on query update when the page has no changes.', () => { |
|
const Wrapped = makePaginable(DummyComponent, 'hehe'); |
|
const handleRequest = sandbox.spy(Wrapped.prototype, 'handleRequest'); |
|
const wrapper = mount(<Wrapped />, { context: DummyContext }); |
|
|
|
expect(handleRequest.calledOnce, 'should request').to.be.true; |
|
wrapper.update(); |
|
expect(handleRequest.calledTwice, 'should not request again').to.be.false; |
|
}); |
|
|
|
it('should load when data is requested.', () => { |
|
sandbox.spy(axios, 'get'); |
|
const Wrapped = makePaginable(DummyComponent, 'hehe'); |
|
const wrapper = shallow(<Wrapped />); |
|
|
|
wrapper.setState({ isLoading: true }); |
|
wrapper.instance().handleRequest(DummyContext); |
|
expect(wrapper.state('isLoading'), 'should be loading before requesting').to.be.true; |
|
}); |
|
|
|
it('should not request when data is still being requested.', () => { |
|
sandbox.spy(axios, 'get'); |
|
const Wrapped = makePaginable(DummyComponent, 'hehe'); |
|
const wrapper = shallow(<Wrapped />); |
|
|
|
wrapper.setState({ isLoading: true }); |
|
wrapper.instance().handleRequest(DummyContext.router); |
|
expect(axios.get.called, 'should be requesting now').to.be.false; |
|
}); |
|
|
|
it('should stop loading and set error when request errors.', (done) => { |
|
const rejected = new Promise((_, reject) => { reject({}) }); |
|
sandbox.stub(axios, 'get').returns(rejected); |
|
const Wrapped = makePaginable(DummyComponent, 'hehe'); |
|
const wrapper = shallow(<Wrapped />); |
|
|
|
// I'm pretty sure everything should be running `sync` now. |
|
// But we're chaining `done` in the next `then` just in case! |
|
wrapper.instance().handleRequest(DummyContext.router).catch(() => { |
|
expect(wrapper.state('isLoading'), 'should be out of loading state now').to.be.false; |
|
expect(wrapper.state('isError'), 'should have an error state now').to.be.true; |
|
expect(wrapper.state('isErrorMessage'), 'should set an error message').to.equal('Oops! The server is having issues right now. Try again!'); |
|
}).then(done, done); |
|
}); |
|
|
|
it('should check if current page is the first page after request.', (done) => { |
|
const resolved = new Promise((r) => r({ data: { current_page: 1 } })); |
|
sandbox.stub(axios, 'get').returns(resolved); |
|
const Wrapped = makePaginable(DummyComponent, 'hehe'); |
|
const wrapper = shallow(<Wrapped />); |
|
|
|
// I'm pretty sure everything should be running `sync` now. |
|
// But we're chaining `done` in the next `then` just in case! |
|
wrapper.instance().handleRequest(DummyContext.router).then(() => { |
|
expect(wrapper.state('isFirstPage'), 'should be first page at this point').to.be.true; |
|
}).then(done, done); |
|
}); |
|
|
|
it('should check if current page is the last page after request.', (done) => { |
|
const resolved = new Promise((r) => r({ data: { current_page: 10, last_page: 10 } })); |
|
sandbox.stub(axios, 'get').returns(resolved); |
|
const Wrapped = makePaginable(DummyComponent, 'hehe'); |
|
const wrapper = shallow(<Wrapped />); |
|
|
|
// I'm pretty sure everything should be running `sync` now. |
|
// But we're chaining `done` in the next `then` just in case! |
|
wrapper.instance().handleRequest(DummyContext.router).then(() => { |
|
expect(wrapper.state('isLastPage'), 'should be last page at this point').to.be.true; |
|
}).then(done, done); |
|
}); |
|
}); |