mirror of
https://github.com/datashard/snapshot.git
synced 2024-11-22 14:12:27 +00:00
chore: use switchcase to pick serializer
This commit is contained in:
parent
f87b2bbad1
commit
b6416087b1
4 changed files with 50 additions and 31 deletions
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
|
@ -1,4 +1,6 @@
|
||||||
{
|
{
|
||||||
"standard.enable": false,
|
"standard.enable": false,
|
||||||
"eslint.enable": true
|
"eslint.enable": true,
|
||||||
|
"eslint.autoFixOnSave": true,
|
||||||
|
"git.ignoreLimitWarning": true
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
42
src/index.js
42
src/index.js
|
@ -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,15 +41,15 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
global.before(() => {
|
global.before(() => {
|
||||||
cy.log('before all tests')
|
cy.log('before all tests')
|
||||||
cy
|
cy
|
||||||
.readFile(SNAPSHOT_FILENAME, 'utf-8', { log: false })
|
.readFile(SNAPSHOT_FILENAME, 'utf-8', { log: false })
|
||||||
.then(evaluateLoadedSnapShots)
|
.then(evaluateLoadedSnapShots)
|
||||||
// no way to catch an error yet
|
// no way to catch an error yet
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
|
32
src/utils.js
32
src/utils.js
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue