mockbot-document
v0.1.14
Published
mock html dom document
Downloads
8
Readme
mockbot-document
mock html dom document
Installation
$ npm init
$ npm install mockbot-document --save-dev
Usage
With tools like browserify, it's easy to create client side code in node.js. But, when testing with tools like mocha, code that references browser elements or the document object will throw an error.
This can be worked around by creating a mock object that simulates the document object. Assign it to global before each test starts, then delete it from global when each test finishes. Here is an example using mocha:
"use strict";
var documentFactory = require("mockbot-document");
describe('module smoke test', () => {
beforeEach( done => {
// Call before all tests
// mock browser document
global.document = documentFactory.create();
done();
});
afterEach( done => {
// Call after all tests
delete global.document;
done();
});
it('createElement should return object', done => {
var result = document.createElement('div');
should.exist(result);
done();
})
}
Limitations
The main objective of this module is to provide placeholders to avoid lint and compiler errors. Duplicating functionality of a real browser is not as important. Though attempts will be made to simulate a response from a browser, actual functionality is not guaranteed.
Available Methods
Only a small subset of mock document methods are currently available. Over time others will be added. See the module reference below to see what is currently available.
Requesting Methods
If a specific method is desired ASAP, open up an issue on github.com to request it.
Elements
For information on available element methods, see mockbot-element.
Modules
External
mockbot-document
Module
mockbot-document.mockElement(spec) ⇒ mockbot-element
Creates a mock element to simulate html elements.
Kind: instance method of mockbot-document
| Param | Type | Description | | --- | --- | --- | | spec | Object | Named parameters object | | spec.tagName | string | Required element type name (a, div, x-thing, etc.) | | spec.id | string | Optional element id |
Example (usage)
document.mockElement( { tagName: tagName, id: id } );
var result = document.getElementById(id);
should.exist(result);
mockbot-document.querySelector() ⇒ null
Mock document.querySelector(). CURRENTLY NON-FUNCTIONAL - just a place holder for now.
Kind: instance method of mockbot-document
Example (usage)
document.querySelector("...");
mockbot-document.getElementById(id) ⇒ mockbot-element
Mock document.getElementById()
Kind: instance method of mockbot-document
| Param | Type | Description | | --- | --- | --- | | id | string | Element id |
Example (usage)
var el = document.getElementById("id");
mockbot-document.getElementsByTagName(tagName) ⇒ Array.<mockbot-element>
Mock document.getElementsByTagName()
Kind: instance method of mockbot-document
| Param | Type | Description | | --- | --- | --- | | tagName | string | Element tagName (div,p,a,etc.) |
Example (usage)
var elArray = document.getElementsByTagName("div");
mockbot-document.createElement(tagName) ⇒ mockbot-element
Mock document.createElement()
Kind: instance method of mockbot-document
| Param | Type | Description | | --- | --- | --- | | tagName | string | name of HTML element (a, div, x-thing, etc.) |
Example (usage)
var el = document.createElement("div");
mockbot-document-factory
Factory module
mockbot-document-factory.create(spec) ⇒ mockbot-document
Factory method It takes one spec parameter that must be an object with named parameters
Kind: static method of mockbot-document-factory
| Param | Type | Description | | --- | --- | --- | | spec | Object | Named parameters object |
Example (Usage example)
var factory = require("mockbot-document");
var obj = factory.create({});
mockbot-element
Mock Element
Kind: global external
See: mockbot-element
Testing
To test, go to the root folder and type (sans $):
$ npm test
Repo(s)
Contributing
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
Version History
Version 0.1.14
- fixex getElementsByTagName documentation
Version 0.1.13
- added getElementsByTagName
- fixed mockElement documentation
Version 0.1.12
- fixed issue where getElementById was returning array instead of first element
Version 0.1.11
- updated mockbot-element to version 0.1.8
Version 0.1.10
- fixed type-o in doc
Version 0.1.9
- refactored getElementById
- cleaned up documentation
Version 0.1.8
- fixed version history
Version 0.1.7
- updated mockbot-element to version 0.1.7
- removed client example
Version 0.1.6
- updated mockbot-element to version 0.1.6
Version 0.1.5
- updated mockbot-element to version 0.1.5
Version 0.1.4
- updated mockbot-element to version 0.1.4 (contains tagName property)
- updated createElement to create element using tagName
Version 0.1.3
- fixed doc errors
Version 0.1.2
- fixed version history
Version 0.1.1
- added test cases to bring coverage up to 100%
- added mockElement method
- updated to latest version of mockbot-element
Version 0.1.0
- initial release