From b6416087b1dfad7c1839b8a9cf0218e23baef723 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Sun, 10 Dec 2017 10:12:29 -0500 Subject: [PATCH] chore: use switchcase to pick serializer --- .vscode/settings.json | 4 +++- package.json | 3 ++- src/index.js | 42 +++++++++++++++--------------------------- src/utils.js | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 4a1878c..3fe16e7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,6 @@ { "standard.enable": false, - "eslint.enable": true + "eslint.enable": true, + "eslint.autoFixOnSave": true, + "git.ignoreLimitWarning": true } diff --git a/package.json b/package.json index ea8827d..2eb2082 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/index.js b/src/index.js index 079b743..48c35ff 100644 --- a/src/index.js +++ b/src/index.js @@ -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,15 +41,15 @@ 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) } global.before(() => { cy.log('before all tests') cy - .readFile(SNAPSHOT_FILENAME, 'utf-8', { log: false }) - .then(evaluateLoadedSnapShots) + .readFile(SNAPSHOT_FILENAME, 'utf-8', { log: false }) + .then(evaluateLoadedSnapShots) // no way to catch an error yet }) @@ -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 diff --git a/src/utils.js b/src/utils.js index c61eb18..4b4632f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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 }