From 3cdf0af343be3a70ccd535204c6a6e6f99001503 Mon Sep 17 00:00:00 2001 From: Joshua Date: Fri, 9 Sep 2022 14:50:23 +0200 Subject: [PATCH] move utils to own files --- src/utils.js | 68 ------------------- .../serializers/deleteTransientIdsFromJson.js | 16 +++++ src/utils/serializers/serializeDomElement.js | 16 +++++ src/utils/serializers/serializeToHTML.js | 16 +++++ .../serializers/stripTransientIdAttributes.js | 5 ++ src/utils/snapshots/compareValues.js | 6 ++ 6 files changed, 59 insertions(+), 68 deletions(-) delete mode 100644 src/utils.js create mode 100644 src/utils/serializers/deleteTransientIdsFromJson.js create mode 100644 src/utils/serializers/serializeDomElement.js create mode 100644 src/utils/serializers/serializeToHTML.js create mode 100644 src/utils/serializers/stripTransientIdAttributes.js create mode 100644 src/utils/snapshots/compareValues.js diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index d89d386..0000000 --- a/src/utils.js +++ /dev/null @@ -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 -} diff --git a/src/utils/serializers/deleteTransientIdsFromJson.js b/src/utils/serializers/deleteTransientIdsFromJson.js new file mode 100644 index 0000000..3136d1d --- /dev/null +++ b/src/utils/serializers/deleteTransientIdsFromJson.js @@ -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; +}; diff --git a/src/utils/serializers/serializeDomElement.js b/src/utils/serializers/serializeDomElement.js new file mode 100644 index 0000000..c89136a --- /dev/null +++ b/src/utils/serializers/serializeDomElement.js @@ -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); +}; diff --git a/src/utils/serializers/serializeToHTML.js b/src/utils/serializers/serializeToHTML.js new file mode 100644 index 0000000..2cda454 --- /dev/null +++ b/src/utils/serializers/serializeToHTML.js @@ -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; +}; diff --git a/src/utils/serializers/stripTransientIdAttributes.js b/src/utils/serializers/stripTransientIdAttributes.js new file mode 100644 index 0000000..d560c11 --- /dev/null +++ b/src/utils/serializers/stripTransientIdAttributes.js @@ -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, ""); +}; diff --git a/src/utils/snapshots/compareValues.js b/src/utils/snapshots/compareValues.js new file mode 100644 index 0000000..1cfb8dd --- /dev/null +++ b/src/utils/snapshots/compareValues.js @@ -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 }); +};