jeudi 30 juillet 2015

Testing a React component - determining where the functionality should be tested

I am extremely new to testing in general, not just React testing, so I'm still trying to figure out not only how to test, but what to test.

Here is my login callback that gets called on submit of a form:

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();
},

AuthModel is a Backbone.js Model. But for this question, lets just say it's an external module that I'm importing into my Login.jsx component.

I'm writing a test to see that if a username is typed, then AuthModel.login() gets called. Do I want to test that in my test.Login.js test file, do I test that in my test.AuthModel.js test file?

it('calls login when there\'s a username present', () => {
    React.findDOMNode(LoginElement.refs.username).value = 'foo';
    TestUtils.Simulate.submit(form);
    // not sure which direction to take this test
});

Current test (in test.login.js) for context...

Any advice is appreciated, as like I said, this is genuinely the first testing I've ever done.

Aucun commentaire:

Enregistrer un commentaire