jeudi 30 juillet 2015

Handling WebSocket connections in Jasmine tests

I have my test.login.js:

it('calls login when there\'s a username present', () => {
    React.findDOMNode(LoginElement.refs.username).value = 'foo';
    TestUtils.Simulate.submit(form);
    expect(LoginElement.state.errored).toEqual(false);
});

By submitting the form, it calls a login method:

login() {
    let typedUsername = React.findDOMNode(this.refs.username).value;
    if (!typedUsername) {
        return this.setState({
            errored: true
        });
    }
    // we don't actually send the request from here, but set the username on the AuthModel and call the `login` method below
    AuthModel.set('username', typedUsername);
    AuthModel.login();
},

So I'm trying to test the functionality of Login.jsx, not AuthModel.js, however by calling AuthModel.login(), it sends a message over a WebSocket. However, the issue is that in my actual app, I don't load anything until the WebSocket has connected (I fire an event to then render the React app), however in my Jasmine test, I don't wait for this event, so I receive:

ERROR: null, DOMException{stack: 'Error: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

And my test fails, which, it shouldn't fail because it's encapsulated functionality does what I want it to. It just errors further up the dependency tree.

What is my best approach for either working around this, or to mitigate the WebSocket trying to connect in my test env? (I'm extremely new to testing, so these concepts are very alien to me right now)

Aucun commentaire:

Enregistrer un commentaire