node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

mockbot-element

mockbot-element

mock html dom element

Version Coverage Status Downloads Version License

Installation

$ npm init
$ npm install mockbot-element --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 browser object:

var elementFactory = require('mockbot-element');

var testAttr = "alpha",
    testValue = "zorro",
    el = elementFactory.create({ tagName: "div", id: "d1" });
    
el.setAttribute(testAttr,testValue);
var result = el.getAttribute(testAttr);

mockbot-document integration

This package is also installed as part of mockbot-document. Through mockbot-document you can create mockbot-elements by simulating the browser document.createElement method.

mockbot-node

This package derives from mockbot-node. See that packages documentation for additional methods.


Modules

mockbot-elementmockbot-node

Module

mockbot-element-factory

Factory module

External

mockbot-node

MockBot Node

mockbot-element ⇐ mockbot-node

Module

Extends: mockbot-node
Properties

Name Type Description
id String the id of the element
tagName String read-only tagName of element as uppercase (i.e. 'DIV')
outerHTML String WARNING: only get currently works, set not implemented

mockbot-element.setAttribute(name, value)

mock element.setAttribute

Kind: instance method of mockbot-element

Param Type Description
name string attribute name
value string attribute value

Example (usage)

el.setAttribute("width","5");

mockbot-element.getAttribute(name)

mock element.getAttribute

Kind: instance method of mockbot-element

Param Type Description
name string attribute name

Example (usage)

var w = el.getAttribute("width");

mockbot-element.toString()

return value of outerHTML

Kind: instance method of mockbot-element
Example (usage)

console.log("ELEMENT: " + el);

mockbot-element-factory

Factory module

mockbot-element-factory.create(spec) ⇒ mockbot-element

Factory method It takes one spec parameter that must be an object with named parameters

Kind: static method of mockbot-element-factory

Param Type Description
spec Object Named parameters object
spec.tagName string required HTML tagName (a, div, x-thing, etc.)
spec.id string optional id attribute for HTML element

Example (Usage)

var factory = require("mockbot-element");
var obj = factory.create({ tagName: "div" });

Example (Usage with id)

var factory = require("mockbot-element");
var obj = factory.create({ tagName: "div", id: "d1" });

mockbot-node

MockBot Node

Kind: global external
See: mockbot-node


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.8

  • moved node base method (cloneNode) to mockbot-node package
  • now derives from mockbot-node package
  • added outerHTML property (WARNING: only get works, not set)
  • added toString method

Version 0.1.7

  • updated usage example
  • removed client example

Version 0.1.6

  • updated factory create documentation

Version 0.1.5

  • fixed documentation for getAttribute

Version 0.1.4

  • added tagName property

Version 0.1.3

  • added id property
  • cleaned up documentation
  • updated methods to simulate real methods

Version 0.1.2

  • removed gitlab from package.json list

Version 0.1.1

  • changed license to MIT
  • integrated travis-ci and codecov.io

Version 0.1.0

  • initial release