mirror of
https://github.com/datashard/snapshot.git
synced 2024-11-24 06:42:29 +00:00
move utils to own files
This commit is contained in:
parent
2cd172d773
commit
3cdf0af343
6 changed files with 59 additions and 68 deletions
68
src/utils.js
68
src/utils.js
|
@ -1,68 +0,0 @@
|
|||
const sd = require('@wildpeaks/snapshot-dom')
|
||||
const beautify = require('js-beautify').html
|
||||
|
||||
// converts DOM element to a JSON object
|
||||
function serializeDomElement ($el) {
|
||||
// console.log('snapshot value!', $el)
|
||||
const json = sd.toJSON($el[0])
|
||||
// console.log('as json', json)
|
||||
|
||||
// hmm, why is value not serialized?
|
||||
if ($el.context.value && !json.attributes.value) {
|
||||
json.attributes.value = $el.context.value
|
||||
}
|
||||
|
||||
return deleteTransientIdsFromJson(json)
|
||||
}
|
||||
|
||||
// remove React and Angular ids, which are transient
|
||||
function deleteTransientIdsFromJson(json) {
|
||||
if (json.attributes) {
|
||||
delete json.attributes['data-reactid']
|
||||
|
||||
Object.keys(json.attributes)
|
||||
.filter(key => key.startsWith('_ng'))
|
||||
.forEach(attr => delete json.attributes[attr])
|
||||
delete json.attributes['']
|
||||
}
|
||||
|
||||
if (Array.isArray(json.childNodes)) {
|
||||
json.childNodes.forEach(deleteTransientIdsFromJson)
|
||||
}
|
||||
return json
|
||||
}
|
||||
|
||||
const stripTransientIdAttributes = (html) => {
|
||||
const dataReactId = /data\-reactid="[\.\d\$\-abcdfef]+"/g
|
||||
const angularId = /_ng(content|host)\-[0-9a-z-]+(="")?/g
|
||||
return html.replace(dataReactId, '')
|
||||
.replace(angularId, '')
|
||||
}
|
||||
|
||||
const serializeToHTML = (el$) => {
|
||||
const html = el$[0].outerHTML
|
||||
const stripped = stripTransientIdAttributes(html)
|
||||
const options = {
|
||||
wrap_line_length: 80,
|
||||
indent_inner_html: true,
|
||||
indent_size: 2,
|
||||
wrap_attributes: 'force'
|
||||
}
|
||||
const pretty = beautify(stripped, options)
|
||||
return pretty
|
||||
}
|
||||
|
||||
const identity = (x) => x
|
||||
|
||||
const publicProps = (name) => !name.startsWith('__')
|
||||
|
||||
const countSnapshots = (snapshots) =>
|
||||
Object.keys(snapshots).filter(publicProps).length
|
||||
|
||||
module.exports = {
|
||||
SNAPSHOT_FILE_NAME: 'snapshots.js',
|
||||
serializeDomElement,
|
||||
serializeToHTML,
|
||||
identity,
|
||||
countSnapshots
|
||||
}
|
16
src/utils/serializers/deleteTransientIdsFromJson.js
Normal file
16
src/utils/serializers/deleteTransientIdsFromJson.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
// remove React and Angular ids, which are transient
|
||||
module.exports = function deleteTransientIdsFromJson(json) {
|
||||
if (json.attributes) {
|
||||
delete json.attributes["data-reactid"];
|
||||
|
||||
Object.keys(json.attributes)
|
||||
.filter((key) => key.startsWith("_ng"))
|
||||
.forEach((attr) => delete json.attributes[attr]);
|
||||
delete json.attributes[""];
|
||||
}
|
||||
|
||||
if (Array.isArray(json.childNodes)) {
|
||||
json.childNodes.forEach(deleteTransientIdsFromJson);
|
||||
}
|
||||
return json;
|
||||
};
|
16
src/utils/serializers/serializeDomElement.js
Normal file
16
src/utils/serializers/serializeDomElement.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
const sd = require("@wildpeaks/snapshot-dom");
|
||||
const deleteTransientIdsFromJson = require("./deleteTransientIdsFromJson");
|
||||
|
||||
// converts DOM element to a JSON object
|
||||
module.exports = function serializeDomElement($el) {
|
||||
// console.log('snapshot value!', $el)
|
||||
const json = sd.toJSON($el[0]);
|
||||
// console.log('as json', json)
|
||||
|
||||
// hmm, why is value not serialized?
|
||||
if ($el.context.value && !json.attributes.value) {
|
||||
json.attributes.value = $el.context.value;
|
||||
}
|
||||
|
||||
return deleteTransientIdsFromJson(json);
|
||||
};
|
16
src/utils/serializers/serializeToHTML.js
Normal file
16
src/utils/serializers/serializeToHTML.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
const stripTransientIdAttributes = require("./stripTransientIdAttributes");
|
||||
const beautify = require("js-beautify").html;
|
||||
|
||||
|
||||
module.exports = (el$) => {
|
||||
const html = el$[0].outerHTML;
|
||||
const stripped = stripTransientIdAttributes(html);
|
||||
const options = {
|
||||
wrap_line_length: 80,
|
||||
indent_inner_html: true,
|
||||
indent_size: 2,
|
||||
wrap_attributes: "force",
|
||||
};
|
||||
const pretty = beautify(stripped, options);
|
||||
return pretty;
|
||||
};
|
5
src/utils/serializers/stripTransientIdAttributes.js
Normal file
5
src/utils/serializers/stripTransientIdAttributes.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
module.exports = (html) => {
|
||||
const dataReactId = /data\-reactid="[\.\d\$\-abcdfef]+"/g;
|
||||
const angularId = /_ng(content|host)\-[0-9a-z-]+(="")?/g;
|
||||
return html.replace(dataReactId, "").replace(angularId, "");
|
||||
};
|
6
src/utils/snapshots/compareValues.js
Normal file
6
src/utils/snapshots/compareValues.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
const compare = require("snap-shot-compare");
|
||||
module.exports = function compareValues({ expected, value }) {
|
||||
const noColor = false;
|
||||
const json = true;
|
||||
return compare({ expected, value, noColor, json });
|
||||
};
|
Loading…
Reference in a new issue