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

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