mirror of
https://github.com/datashard/snapshot.git
synced 2024-11-21 21:52:28 +00:00
feat: convert to JSON for dom is working, close #2
This commit is contained in:
parent
515b5ba900
commit
b78795e924
3 changed files with 37 additions and 15 deletions
12
README.md
12
README.md
|
@ -88,6 +88,18 @@ If you change the site values, the saved snapshot will no longer match, throwing
|
|||
|
||||
![Snapshot mismatch](img/snapshot-mismatch.png)
|
||||
|
||||
### Options
|
||||
|
||||
You can control snapshot comparison and behavior through a few options.
|
||||
|
||||
```js
|
||||
cy.get(...).snapshot({
|
||||
name: 'human snapshot name', // to use in the snapshot file
|
||||
json: false // convert DOM elements into JSON
|
||||
// when storing in the snapshot file
|
||||
})
|
||||
```
|
||||
|
||||
## Debugging
|
||||
|
||||
To debug this module run with environment variable `DEBUG=@cypress/snapshot`
|
||||
|
|
24
src/index.js
24
src/index.js
|
@ -5,11 +5,11 @@ const itsName = require('its-name')
|
|||
const { initStore } = require('snap-shot-store')
|
||||
const la = require('lazy-ass')
|
||||
const is = require('check-more-types')
|
||||
const switchcase = require('switchcase')
|
||||
const compare = require('snap-shot-compare')
|
||||
|
||||
const {
|
||||
isJqueryElement,
|
||||
serializeDomElement,
|
||||
serializeReactToHTML,
|
||||
identity,
|
||||
countSnapshots
|
||||
|
@ -130,23 +130,23 @@ function registerCypressSnapshot () {
|
|||
storeSnapshot({
|
||||
value,
|
||||
name,
|
||||
// compare: compareValues,
|
||||
raiser: cyRaiser
|
||||
})
|
||||
}
|
||||
|
||||
const pickSerializer = switchcase({
|
||||
// [isJqueryElement]: serializeDomElement,
|
||||
[isJqueryElement]: serializeReactToHTML,
|
||||
default: identity
|
||||
})
|
||||
const pickSerializer = (asJson, value) => {
|
||||
if (isJqueryElement(value)) {
|
||||
return asJson ? serializeDomElement : serializeReactToHTML
|
||||
}
|
||||
return identity
|
||||
}
|
||||
|
||||
function snapshot (value, humanName) {
|
||||
console.log('human name', humanName)
|
||||
const snapshotName = getSnapshotName(this.test, humanName)
|
||||
const serializer = pickSerializer(value)
|
||||
function snapshot (value, { name, json } = {}) {
|
||||
console.log('human name', name)
|
||||
const snapshotName = getSnapshotName(this.test, name)
|
||||
const serializer = pickSerializer(json, value)
|
||||
const serialized = serializer(value)
|
||||
setSnapshot(snapshotName, serialized, value, humanName)
|
||||
setSnapshot(snapshotName, serialized, value)
|
||||
|
||||
// always just pass value
|
||||
return value
|
||||
|
|
16
src/utils.js
16
src/utils.js
|
@ -8,9 +8,7 @@ function isJqueryElement (x) {
|
|||
// converts DOM element to a JSON object
|
||||
function serializeDomElement ($el) {
|
||||
// console.log('snapshot value!', $el)
|
||||
const json = sd.toJSON($el.context)
|
||||
// remove React id, too transient
|
||||
delete json.attributes['data-reactid']
|
||||
const json = sd.toJSON($el[0])
|
||||
// console.log('as json', json)
|
||||
|
||||
// hmm, why is value not serialized?
|
||||
|
@ -18,6 +16,18 @@ function serializeDomElement ($el) {
|
|||
json.attributes.value = $el.context.value
|
||||
}
|
||||
|
||||
return deleteReactIdFromJson(json)
|
||||
}
|
||||
|
||||
// remove React id, too transient
|
||||
function deleteReactIdFromJson (json) {
|
||||
if (json.attributes) {
|
||||
delete json.attributes['data-reactid']
|
||||
}
|
||||
|
||||
if (Array.isArray(json.childNodes)) {
|
||||
json.childNodes.forEach(deleteReactIdFromJson)
|
||||
}
|
||||
return json
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue