chore: use switchcase to pick serializer

This commit is contained in:
Gleb Bahmutov 2017-12-10 10:12:29 -05:00
parent f87b2bbad1
commit b6416087b1
4 changed files with 50 additions and 31 deletions

View file

@ -1,4 +1,6 @@
{ {
"standard.enable": false, "standard.enable": false,
"eslint.enable": true "eslint.enable": true,
"eslint.autoFixOnSave": true,
"git.ignoreLimitWarning": true
} }

View file

@ -87,6 +87,7 @@
"check-more-types": "2.24.0", "check-more-types": "2.24.0",
"its-name": "1.0.0", "its-name": "1.0.0",
"lazy-ass": "1.6.0", "lazy-ass": "1.6.0",
"snap-shot-store": "1.2.3" "snap-shot-store": "1.2.3",
"switchcase": "0.0.3"
} }
} }

View file

@ -1,12 +1,12 @@
'use strict' 'use strict'
/* global cy, Cypress */ /* global cy, Cypress */
const sd = require('@wildpeaks/snapshot-dom')
const itsName = require('its-name') 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 { serializeDomElement, identity, countSnapshots } = require('./utils')
const switchcase = require('switchcase')
/* eslint-disable no-console */ /* eslint-disable no-console */
function registerCypressSnapshot () { function registerCypressSnapshot () {
@ -41,7 +41,7 @@ function registerCypressSnapshot () {
la(is.string(js), 'expected JavaScript snapshot source', js) la(is.string(js), 'expected JavaScript snapshot source', js)
console.log('read snapshots.js file') console.log('read snapshots.js file')
const store = eval(js) || {} const store = eval(js) || {}
console.log('have %d snapshot(s)', Object.keys(store).length) console.log('have %d snapshot(s)', countSnapshots(store))
storeSnapshot = initStore(store) storeSnapshot = initStore(store)
} }
@ -103,22 +103,10 @@ function registerCypressSnapshot () {
}) })
} }
function snapshot$ (test, $el, humanName) { const pickSerializer = switchcase({
console.log('snapshot value!', $el) [isJqueryElement]: serializeDomElement,
const json = sd.toJSON($el.context) default: identity
// remove React id, too transient })
delete json.attributes['data-reactid']
console.log('as json', json)
// hmm, why is value not serialized?
if ($el.context.value && !json.attributes.value) {
json.attributes.value = $el.context.value
}
const name = getSnapshotName(test, humanName)
setSnapshot(name, json, $el, humanName)
return $el
}
function isJqueryElement (x) { function isJqueryElement (x) {
return 'wrap' in x return 'wrap' in x
@ -126,11 +114,11 @@ function registerCypressSnapshot () {
function snapshot (value, humanName) { function snapshot (value, humanName) {
console.log('human name', humanName) console.log('human name', humanName)
if (isJqueryElement(value)) { const snapshotName = getSnapshotName(this.test, humanName)
snapshot$(this.test, value, humanName) const serializer = pickSerializer(value)
} else { const serialized = serializer(value)
setSnapshot(getSnapshotName(this.test, humanName), value, humanName) setSnapshot(snapshotName, serialized, value, humanName)
}
// always just pass value // always just pass value
return value return value
} }
@ -141,7 +129,7 @@ function registerCypressSnapshot () {
if (storeSnapshot) { if (storeSnapshot) {
cy.log('saving snapshots') cy.log('saving snapshots')
const snapshots = storeSnapshot() const snapshots = storeSnapshot()
console.log('snapshots on finish') console.log('%d snapshot(s) on finish', countSnapshots(snapshots))
console.log(snapshots) console.log(snapshots)
snapshots.__version = Cypress.version snapshots.__version = Cypress.version

View file

@ -1,3 +1,31 @@
module.exports = { const sd = require('@wildpeaks/snapshot-dom')
SNAPSHOT_FILE_NAME: 'snapshots.js'
// 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']
// 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 json
}
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,
identity,
countSnapshots
} }