feat: convert to JSON for dom is working, close #2

This commit is contained in:
Gleb Bahmutov 2017-12-10 23:04:58 -05:00
parent 515b5ba900
commit b78795e924
3 changed files with 37 additions and 15 deletions

View file

@ -88,6 +88,18 @@ If you change the site values, the saved snapshot will no longer match, throwing
![Snapshot mismatch](img/snapshot-mismatch.png) ![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 ## Debugging
To debug this module run with environment variable `DEBUG=@cypress/snapshot` To debug this module run with environment variable `DEBUG=@cypress/snapshot`

View file

@ -5,11 +5,11 @@ const itsName = require('its-name')
const { initStore } = require('snap-shot-store') const { initStore } = require('snap-shot-store')
const la = require('lazy-ass') const la = require('lazy-ass')
const is = require('check-more-types') const is = require('check-more-types')
const switchcase = require('switchcase')
const compare = require('snap-shot-compare') const compare = require('snap-shot-compare')
const { const {
isJqueryElement, isJqueryElement,
serializeDomElement,
serializeReactToHTML, serializeReactToHTML,
identity, identity,
countSnapshots countSnapshots
@ -130,23 +130,23 @@ function registerCypressSnapshot () {
storeSnapshot({ storeSnapshot({
value, value,
name, name,
// compare: compareValues,
raiser: cyRaiser raiser: cyRaiser
}) })
} }
const pickSerializer = switchcase({ const pickSerializer = (asJson, value) => {
// [isJqueryElement]: serializeDomElement, if (isJqueryElement(value)) {
[isJqueryElement]: serializeReactToHTML, return asJson ? serializeDomElement : serializeReactToHTML
default: identity }
}) return identity
}
function snapshot (value, humanName) { function snapshot (value, { name, json } = {}) {
console.log('human name', humanName) console.log('human name', name)
const snapshotName = getSnapshotName(this.test, humanName) const snapshotName = getSnapshotName(this.test, name)
const serializer = pickSerializer(value) const serializer = pickSerializer(json, value)
const serialized = serializer(value) const serialized = serializer(value)
setSnapshot(snapshotName, serialized, value, humanName) setSnapshot(snapshotName, serialized, value)
// always just pass value // always just pass value
return value return value

View file

@ -8,9 +8,7 @@ function isJqueryElement (x) {
// converts DOM element to a JSON object // converts DOM element to a JSON object
function serializeDomElement ($el) { function serializeDomElement ($el) {
// console.log('snapshot value!', $el) // console.log('snapshot value!', $el)
const json = sd.toJSON($el.context) const json = sd.toJSON($el[0])
// remove React id, too transient
delete json.attributes['data-reactid']
// console.log('as json', json) // console.log('as json', json)
// hmm, why is value not serialized? // hmm, why is value not serialized?
@ -18,6 +16,18 @@ function serializeDomElement ($el) {
json.attributes.value = $el.context.value 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 return json
} }