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,
"eslint.enable": true
"eslint.enable": true,
"eslint.autoFixOnSave": true,
"git.ignoreLimitWarning": true
}

View file

@ -87,6 +87,7 @@
"check-more-types": "2.24.0",
"its-name": "1.0.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'
/* global cy, Cypress */
const sd = require('@wildpeaks/snapshot-dom')
const itsName = require('its-name')
const { initStore } = require('snap-shot-store')
const la = require('lazy-ass')
const is = require('check-more-types')
const { serializeDomElement, identity, countSnapshots } = require('./utils')
const switchcase = require('switchcase')
/* eslint-disable no-console */
function registerCypressSnapshot () {
@ -41,7 +41,7 @@ function registerCypressSnapshot () {
la(is.string(js), 'expected JavaScript snapshot source', js)
console.log('read snapshots.js file')
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)
}
@ -103,22 +103,10 @@ function registerCypressSnapshot () {
})
}
function snapshot$ (test, $el, humanName) {
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
}
const name = getSnapshotName(test, humanName)
setSnapshot(name, json, $el, humanName)
return $el
}
const pickSerializer = switchcase({
[isJqueryElement]: serializeDomElement,
default: identity
})
function isJqueryElement (x) {
return 'wrap' in x
@ -126,11 +114,11 @@ function registerCypressSnapshot () {
function snapshot (value, humanName) {
console.log('human name', humanName)
if (isJqueryElement(value)) {
snapshot$(this.test, value, humanName)
} else {
setSnapshot(getSnapshotName(this.test, humanName), value, humanName)
}
const snapshotName = getSnapshotName(this.test, humanName)
const serializer = pickSerializer(value)
const serialized = serializer(value)
setSnapshot(snapshotName, serialized, value, humanName)
// always just pass value
return value
}
@ -141,7 +129,7 @@ function registerCypressSnapshot () {
if (storeSnapshot) {
cy.log('saving snapshots')
const snapshots = storeSnapshot()
console.log('snapshots on finish')
console.log('%d snapshot(s) on finish', countSnapshots(snapshots))
console.log(snapshots)
snapshots.__version = Cypress.version

View file

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