Last active
August 29, 2015 14:14
-
-
Save thealexbaron/21278bc00ec86414f3fb to your computer and use it in GitHub Desktop.
Test loading indicator component for ReactJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @jsx React.DOM */ | |
jest.dontMock('../components/pendingData.jsx'); | |
describe('PendingData', function() { | |
var React = require('react/addons'); | |
var PendingData = require('../components/pendingData.jsx'); | |
var TestUtils = React.addons.TestUtils; | |
var slowPendingData = TestUtils.renderIntoDocument( | |
<PendingData waitFor={null}>data loaded</PendingData> | |
); | |
it('starts out empty', function() { | |
expect(slowPendingData.state.showLoading).toBeFalsy(); | |
expect(slowPendingData.state.dataLoaded).toBeFalsy(); | |
}); | |
it('shows loading indicator after 500 ms', function() { | |
setTimeout(function() { | |
expect(slowPendingData.state.showLoading).toBeTruthy(); | |
expect(slowPendingData.state.dataLoaded).toBeFalsy(); | |
}, 500); | |
}); | |
it('shows contents when data arrives', function() { | |
slowPendingData.setProps({ waitFor: true }, function() { | |
expect(slowPendingData.state.showLoading).toBeFalsy(); | |
expect(slowPendingData.state.dataLoaded).toBeTruthy(); | |
var span = TestUtils.findRenderedDOMComponentWithTag(slowPendingData, 'span'); | |
expect(span.getDOMNode().textContent).toEqual('data loaded'); | |
}); | |
}); | |
var quickPendingData = TestUtils.renderIntoDocument( | |
<PendingData>data loaded</PendingData> | |
); | |
it('fills in data immediately (quick response)', function() { | |
setTimeout(function() { | |
expect(quickPendingData.state.showLoading).toBeFalsy(); | |
expect(quickPendingData.state.dataLoaded).toBeTruthy(); | |
var span = TestUtils.findRenderedDOMComponentWithTag(quickPendingData, 'span'); | |
expect(span.getDOMNode().textContent).toEqual('data loaded'); | |
}, 1); // the state isn't set immediately so maybe waiting 1ms is okay? | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment