0.6.0 • Published 7 years ago

galvanize-superjsdom v0.6.0

Weekly downloads
2
License
CC-BY-NC-SA-4.0
Repository
github
Last release
7 years ago

galvanize-superJSDOM

A simple, opinionated library for writing integration tests with Superagent and JSDOM.

npm version Build Status

Description

This library is maintained for use by Galvanize instructors and curriculum developers to test student exercise code. It's designed to test simple applications written by junior developers by automating user actions, such as filling in a form and clicking a submit button.

Installation

npm install galvanize-superjsdom

Usage

Here's an example of an exercise with some tests that check the DOM for the correct content.

'use strict'
const expect = require('chai').expect
const server = require('../app')
const request = require('supertest')(server)
const Page = require('galvanize-superjsdom')

describe("POST /", () => {
  it("can fill in forms", () => {
    const request = supertest(app)
    const page = new Page(request)

    return page.visit("/")
      .clickLink('New Person')
      .fillIn('First Name', 'Sue')
      .fillIn('Last Name', 'Sylvester')
      .check('Check it out')
      .select('Thirty', {from: 'Age'})
      .clickButton('Submit Me')
      .promise
      .then(function(page){

        // page.$ is a jQuery object representing the document
        expect(page.$("h1").text()).to.equal("It worked")

        // you can also access
        //
        //  - page.window
        //  - page.response.body
      })
  })

});

Page

Kind: global class

new Page(request)

Represents a Page.

Requires an instance of an express server wrapped in supertest:

const server = require('../myApp/app');
const request = require('supertest')(server);
ParamTypeDescription
requestexpressan instance of the app wrapped in supertest

page.visit(path) ⇒ Promise

Page.prototype.visit - get a jsdom instance with jquery returned as a promise

Kind: instance method of Page
Returns: Promise - a promise containing a jsdom object

ParamTypeDescription
pathstringthe relative path

page.validate($) ⇒ Promise

Page.prototype.validate - A promise that validates the page against w3c standards

Kind: instance method of Page
Returns: Promise - resolves the promise with the jsdom instance if there are no errors found, otherwise rejects with an object containing the errors

ParamTypeDescription
$jsdoman instance of jsdom

page.get(path) ⇒ Promise

Page.prototype.get - Sends a GET request to the path and resolves a promise with that object

Kind: instance method of Page
Returns: Promise - resolves the promise with the response object, otherwise rejects it with an error

ParamTypeDescription
pathstringthe path

page.post(path, controls) ⇒ Promise

Page.prototype.post - Sends a post request with the data provided. This is typically used in conjunction with the clickButton method.

Kind: instance method of Page
Returns: Promise - Resolves with a request object

ParamTypeDescription
pathstringThe path to send
controlsobjectan object formatted as an array of objects controls containing a name and value. jQuery's .serializeArray function will format controls properly.

page.clickLink(text) ⇒ Promise

Page.prototype.clickLink - A thenable that visits a link by loading the href gotten by the text of the link.

Kind: instance method of Page
Returns: Promise - Returns the resolve or reject value of the promise returned by the visit method

ParamTypeDescription
textstringThe text of the link to be clicked

page.fillIn(text, options) ⇒ function

Page.prototype.fillIn - Fills in an input or textarea that is attached to a label

Kind: instance method of Page
Returns: function - returns a function usable in a promise chain

ParamTypeDescription
textstringthe visible content of the label attached to the input via a for
optionsobjectthe content - a string will be converted to an object with the format: { 'with': str }

page.check(text) ⇒ function

Page.prototype.check - Checks a checkbox given the text of the checkbox

Kind: instance method of Page
Returns: function - A function usable in a promise chain

ParamTypeDescription
textstringThe visible content of the label associated with the checkbox

page.clickButton(text) ⇒ Promise

Page.prototype.clickButton - Submits a form by gathering form values and creating a post request

Kind: instance method of Page
Returns: Promise - Returns a promise from the Post method

ParamTypeDescription
textstringThe visible text of the submit button

page.select(text, options) ⇒ function

Page.prototype.select - Selects an option from a select box based on the text of the option

Kind: instance method of Page
Returns: function - a function usable in a promise chain

ParamTypeDescription
textstringThe visible text of the label associated with the select box
optionsobjectan object to configure the selection - - a string will be converted to an object with the format: { 'from': str }

page.wait(time) ⇒ function

Page.prototype.time - waits for roughly the amount of milliseconds to pass before resolving the next promise

It's important to note that the time the promise actually executes after may be a bit longer than the amount of time specified, due to javascript's event loop not actually interrupting currently executing code. For instance, this method won't interrupt an infinite loop.

Kind: instance method of Page
Returns: function - a function usable in a promise chain

ParamTypeDescription
textstringThe visible text of the label associated with the select box
optionsobjectan object to configure the selection - - a string will be converted to an object with the format: { 'from': str }

Contributing

  • Clone this repo
  • Run yarn to install dependencies
  • Run npm test to run tests
  • Run DEBUG=true npm test to run tests
0.6.0

7 years ago

0.5.2

7 years ago

0.3.4

7 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.0.1

8 years ago