Socket
Socket
Sign inDemoInstall

@wildpeaks/snapshot-dom

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.2.1 to 1.3.0

10

package.json
{
"name": "@wildpeaks/snapshot-dom",
"version": "1.2.1",
"version": "1.3.0",
"description": "Converts a DOM element to a JSON tree",
"main": "src/snapshot.js",
"files": [
"src/snapshot.js",
"test/snapshot.tests.js"
"src/snapshot.js",
"test/snapshot.tests.js"
],

@@ -29,5 +29,5 @@ "scripts": {

"devDependencies": {
"jsdom": "latest",
"mocha": "latest"
"jsdom": "15.1.1",
"mocha": "6.1.4"
}
}
# Snapshot
[![Build Status](https://travis-ci.org/wildpeaks/package-snapshot-dom.svg?branch=master)](https://travis-ci.org/wildpeaks/package-snapshot-dom)
Converts an HTMLElement to a JSON tree, useful for automated DOM tests.

@@ -6,0 +4,0 @@

/* eslint-env mocha */
const assert = require('assert');
const jsdom = require('jsdom');
const {JSDOM} = require('jsdom');
const snapshot = require('..');
function resetDOM(){
global.document = jsdom.jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach(property => {
if (typeof global[property] === 'undefined'){
global[property] = document.defaultView[property];
}
describe('toJSON', () => {
// Reset DOM
before(() => {
const dom = new JSDOM('<!DOCTYPE html><html><head></head><body></body></html>');
global.document = dom.window.document;
});
global.navigator = {
userAgent: 'mocha'
};
}
it('Missing element', () => {
assert.deepStrictEqual(snapshot.toJSON(), {});
});
it('Invalid element (0)', () => {
assert.deepStrictEqual(snapshot.toJSON(0), {});
});
it('Invalid element (1)', () => {
assert.deepStrictEqual(snapshot.toJSON(1), {});
});
it('Invalid element (false)', () => {
assert.deepStrictEqual(snapshot.toJSON(false), {});
});
it('Invalid element (true)', () => {
assert.deepStrictEqual(snapshot.toJSON(true), {});
});
it('Invalid element (null)', () => {
assert.deepStrictEqual(snapshot.toJSON(null), {});
});
it('Invalid element (undefined)', () => {
assert.deepStrictEqual(snapshot.toJSON(undefined), {}); // eslint-disable-line no-undefined
});
describe('@wildpeaks/snapshot-dom', () => {
describe('toJSON', () => {
before(resetDOM);
it('Empty body', () => {
document.body.innerHTML = '';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body'
}
);
});
it('Missing element', () => {
assert.deepStrictEqual(snapshot.toJSON(), {});
});
it('Invalid element (0)', () => {
assert.deepStrictEqual(snapshot.toJSON(0), {});
});
it('Invalid element (1)', () => {
assert.deepStrictEqual(snapshot.toJSON(1), {});
});
it('Invalid element (false)', () => {
assert.deepStrictEqual(snapshot.toJSON(false), {});
});
it('Invalid element (true)', () => {
assert.deepStrictEqual(snapshot.toJSON(true), {});
});
it('Invalid element (null)', () => {
assert.deepStrictEqual(snapshot.toJSON(null), {});
});
it('Invalid element (undefined)', () => {
assert.deepStrictEqual(snapshot.toJSON(undefined), {}); // eslint-disable-line no-undefined
});
it('Empty body', () => {
document.body.innerHTML = '';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body'
}
);
});
it('Single paragraph in body', () => {
document.body.innerHTML = '<p class="test1"></p>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'p',
attributes: {
class: 'test1'
}
it('Single paragraph in body', () => {
document.body.innerHTML = '<p class="test1"></p>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'p',
attributes: {
class: 'test1'
}
]
}
);
});
it('Single paragraph in detached element', () => {
const element = document.createElement('p');
element.className = 'test2';
assert.deepStrictEqual(
snapshot.toJSON(element),
{
tagName: 'p',
attributes: {
class: 'test2'
}
}
);
});
]
}
);
});
it('Nested elements in body', () => {
document.body.innerHTML = '<div class="outer"><div class="inner"></div><p></p></div>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'div',
attributes: {
class: 'outer'
},
childNodes: [
{
tagName: 'div',
attributes: {
class: 'inner'
}
},
{
tagName: 'p'
}
]
}
]
it('Single paragraph in detached element', () => {
const element = document.createElement('p');
element.className = 'test2';
assert.deepStrictEqual(
snapshot.toJSON(element),
{
tagName: 'p',
attributes: {
class: 'test2'
}
);
});
}
);
});
it('Text fragment', () => {
document.body.innerHTML = '<p>Hello World</p>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'p',
childNodes: [
{
nodeName: '#text',
nodeValue: 'Hello World'
it('Nested elements in body', () => {
document.body.innerHTML = '<div class="outer"><div class="inner"></div><p></p></div>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'div',
attributes: {
class: 'outer'
},
childNodes: [
{
tagName: 'div',
attributes: {
class: 'inner'
}
]
}
]
}
);
});
it('Attributes', () => {
document.body.innerHTML = '<button role="heading" style="color: green">Search</button>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'button',
attributes: {
role: 'heading',
style: 'color: green'
},
childNodes: [
{
nodeName: '#text',
nodeValue: 'Search'
}
]
}
]
}
);
});
{
tagName: 'p'
}
]
}
]
}
);
});
it('Duplicated attribute (alphabetic order)', () => {
document.body.innerHTML = '<div class="AAA" class="ZZZ"></div>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'div',
attributes: {
class: 'AAA'
it('Text fragment', () => {
document.body.innerHTML = '<p>Hello World</p>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'p',
childNodes: [
{
nodeName: '#text',
nodeValue: 'Hello World'
}
}
]
}
);
});
it('Duplicated attribute (reverse order)', () => {
document.body.innerHTML = '<div class="ZZZ" class="AAA"></div>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'div',
attributes: {
class: 'ZZZ'
]
}
]
}
);
});
it('Attributes', () => {
document.body.innerHTML = '<button role="heading" style="color: green">Search</button>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'button',
attributes: {
role: 'heading',
style: 'color: green'
},
childNodes: [
{
nodeName: '#text',
nodeValue: 'Search'
}
]
}
]
}
);
});
it('Duplicated attribute (alphabetic order)', () => {
document.body.innerHTML = '<div class="AAA" class="ZZZ"></div>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'div',
attributes: {
class: 'AAA'
}
]
}
);
});
}
]
}
);
});
it('Duplicated attribute (reverse order)', () => {
document.body.innerHTML = '<div class="ZZZ" class="AAA"></div>';
assert.deepStrictEqual(
snapshot.toJSON(document.body),
{
tagName: 'body',
childNodes: [
{
tagName: 'div',
attributes: {
class: 'ZZZ'
}
}
]
}
);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc