npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-page-objects

v0.5.1

Published

Page Object pattern for testing React components

Downloads

7

Readme

React Page Objects

React page objects make it easy to test react components.

React gives you some useful utilities for testing React components however they lead to verbose code that clutters your tests.

var TestUtils = require("react/addons").addons.TestUtils;

var element = TestUtils.renderIntoDocument(<LoginPage />);
var emailNode = element.email.getDOMNode();

expect(emailNode.value).to.equal("[email protected]");

element.password.getDOMNode().value = "[email protected]";
TestUtils.Simulate.change(element.password.getDOMNode(), {
  target: {
    value: "password"
  }
});

TestUtils.Simulate.click(element.login.getDOMNode());

React Page Objects hides this complexity by providing a simpler API

var page = new PageObject(<LoginPage />);

expect(page.email.value).to.equal("[email protected]");
page.email.value = "[email protected]";
page.login.click();

##Tutorial

Say you have a login page built in React you want to test

var LoginPage = React.createClass({
  render: function () {
    return (
      <div className="login-page">
        <input ref="email" type="text" value={this.state.email} onChange={this.onEmailChanged} />
        <input ref="password" type="password" value={this.state.password} onChange={this.onPasswordChanged} />
        <input ref="login" value="Login" type="submit" onClick={this.login} />
      </div>
    );
  },
  login: function () {
    AuthService.authenticate(this.state.email, this.state.password);
  },
  onEmailChanged: function (e) {
    this.setState({ email: e.target.value });
  },
  onPasswordChanged: function () {
    this.setState({ password: this.refs.password.getDOMNode().value });
  },
  getInitialState: function () {
    return {
      email: "",
      password: ""
    };
  }
});

To easily reference specific elements within the component, you should add refs to them.

If you then pass an instance of the component into a page object, any refs will be accessible. You can then get the values of the elements and simulate events (e.g. click, select, etc).

var page = new PageObject(<LoginPage />);

page.email.value = "[email protected]";
page.password.value = "password";
page.login.click();

You can also create a custom type for a page using PageObject#extend

var LoginPageObject = PageObject.extend({
  getComponent: function () {
    return <LoginPage />;
  },
  loginAs: function (email, password) {
    this.email.value = "[email protected]";
    this.password.value = "password";
    this.login.click();
  }
});

var page = new LoginPageObject();

page.loginAs("[email protected]", "password");

The elements hash map allows you to specify the page object type of any refs allowing you to build more complex pages. They key in the elements is the name of the ref and the value is the page object type.

var NewsFeedItem = React.createClass({
  render: function () {
    return (
      <div className="news-feed-item">
        <User ref="user" user={this.props.item.user} />
        <h1 ref="title">{this.props.item.title}</h1>
        <div ref="body">{this.props.item.body}</div>
      </div>
    );
  }
});

var NewsFeedItemPageObject = PageObject.extend({
  elements: {
    user: PageObject
  },
  getComponent: function (props) {
    return <NewsFeed {...props} />;
  }
});

var User = React.createClass({
  render: function () {
    return (
      <div className="user">
        <span ref="name">{this.props.user.name}</span>
      </div>
    );
  }
});

var item = new NewsFeedItemPageObject({
  item: {
    user: { name: "Foo Bar" },
    title: "Foo",
    body: "Lorum Ipsum"
  }
});

expect(item.title.value).to.equal("Foo");
expect(item.user.name.value).to.equal("Foo Bar");

If you have an array of page objects, you should add a ref to the parent element and then in elements hash the value should be an array containing the page object type.

var NewsFeed = React.createClass({
  render: function () {
    return (
      <div className="news-feed">
        <div className="news-feed-items" ref="items">
          {this.state.items.map(function (item) {
            return <NewsFeedItem key={item.id} item={item} />;
          })}
        </div>
      </div>
    );
  },
  getInitialState: function () {
    return {
      items: NewsFeedService.getNewsFeedItemsFor(this.props.user)
    };
  }
});

var NewsFeedPageObject = PageObject.extend({
  elements: {
    items: [NewsFeedItemPageObject]
  },
  getComponent: function (props) {
    return <NewsFeed {...props} />;
  }
});

var newsFeed = new NewsFeedPageObject({
  user: "foo"
});

expect(newsFeed.items).to.not.be.empty;
expect(newsFeed.items.get(1).user.name.value).to.equal("foo");