galvanize-superjsdom
v0.6.0
Published
A small library for writing integration tests with superagent and jsdom
Downloads
3
Readme
galvanize-superJSDOM
A simple, opinionated library for writing integration tests with Superagent and JSDOM.
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
- Page
- new Page(request)
- .visit(path) ⇒ Page
- .clickLink(text) ⇒ Page
- .fillIn(text, options) ⇒ Page
- .check(text) ⇒ Page
- .clickButton(text) ⇒ Page
- .select(text, options) ⇒ Page
- .wait(time) ⇒ Page
- .promise ⇒ Promise
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);
| Param | Type | Description | |:--------|:---------------------|:--------------------------------------------| | request | express | an 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
| Param | Type | Description | |:------|:--------------------|:------------------| | path | string | the 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
| Param | Type | Description | |:------|:-------------------|:---------------------| | $ | jsdom | an 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
| Param | Type | Description | |:------|:--------------------|:------------| | path | string | the 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
| Param | Type | Description | |:---------|:--------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------| | path | string | The path to send | | controls | object | an 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
| Param | Type | Description | |:------|:--------------------|:-----------------------------------| | text | string | The 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
| Param | Type | Description |
|:--------|:--------------------|:---------------------------------------------------------------------------------------|
| text | string | the visible content of the label attached to the input via a for
|
| options | object | the 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
| Param | Type | Description | |:------|:--------------------|:--------------------------------------------------------------| | text | string | The 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
| Param | Type | Description | |:------|:--------------------|:--------------------------------------| | text | string | The 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
| Param | Type | Description | |:--------|:--------------------|:-------------------------------------------------------------------------------------------------------------------| | text | string | The visible text of the label associated with the select box | | options | object | an 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
| Param | Type | Description | |:--------|:--------------------|:-------------------------------------------------------------------------------------------------------------------| | text | string | The visible text of the label associated with the select box | | options | object | an 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