From 9ffdeb80b0bb30237878fb1517c1f496b538ba6d Mon Sep 17 00:00:00 2001 From: Josh Johnston Date: Wed, 15 Nov 2017 21:20:36 +1100 Subject: [PATCH] Prevent crashing when a component is null (#26) * failing specs * updates to pass specs * fixed typo in spec --- src/core/snapshot.js | 6 ++- test/core/__snapshots__/snapshot.spec.js.snap | 26 +++++++++++ test/core/snapshot.spec.js | 46 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/core/snapshot.js b/src/core/snapshot.js index 7134983..9c2382d 100644 --- a/src/core/snapshot.js +++ b/src/core/snapshot.js @@ -10,14 +10,16 @@ const typeOf = { value: Symbol.for('react.test.json') } const cleanUp = (result) => { result.actual = result.actual.trim() result.expected = result.expected.trim() - result.diff = result.diff.replace(/\n[ ]+\n/g, '\n\n') + result.diff = (result.diff || '').replace(/\n[ ]+\n/g, '\n\n') } const snapshot = (name, tree, update) => { const destination = path.resolve(base, `${name}.snap`) const state = new SnapshotState(null, update, destination) - Object.defineProperty(tree, '$$typeof', typeOf) + if (tree) { + Object.defineProperty(tree, '$$typeof', typeOf) + } const result = state.match(name, tree) state.save(update) diff --git a/test/core/__snapshots__/snapshot.spec.js.snap b/test/core/__snapshots__/snapshot.spec.js.snap index 47227c1..029fd76 100644 --- a/test/core/__snapshots__/snapshot.spec.js.snap +++ b/test/core/__snapshots__/snapshot.spec.js.snap @@ -1,3 +1,23 @@ +exports[`test fails if new is null but old is not 1`] = ` +Object { + "actual": "null", + "count": 1, + "diff": "", + "expected": "
", + "pass": false, +} +`; + +exports[`test fails if old was null but new is not 1`] = ` +Object { + "actual": "
", + "count": 1, + "diff": "", + "expected": "null", + "pass": false, +} +`; + exports[`test fails if the type changes 1`] = ` Object { "actual": " @@ -19,3 +39,9 @@ Object { "pass": false, } `; + +exports[`test passes if new and old are null 1`] = ` +Object { + "pass": true, +} +`; diff --git a/test/core/snapshot.spec.js b/test/core/snapshot.spec.js index a093fc2..0df052f 100644 --- a/test/core/snapshot.spec.js +++ b/test/core/snapshot.spec.js @@ -36,3 +36,49 @@ test('does not fail if update is true', () => { expect(result).toEqual({ pass: true }) }) + +test('passes if null, first time', () => { + const tree = null + const result = snapshot('name', tree) + + expect(result).toEqual({ pass: true }) +}) + +test('passes if new and old are null', () => { + const name = 'both-null' + snapshot(name, null) + const result = snapshot(name, null) + + expect(result).toMatchSnapshot() +}) + +test('fails if old was null but new is not', () => { + const name = 'old-was-null' + snapshot(name, null) + + const tree = { type: 'div' } + const result = snapshot(name, tree) + + expect(result).toMatchSnapshot() +}) + +test('fails if new is null but old is not', () => { + const name = 'new-is-null' + const tree = { type: 'div' } + snapshot(name, tree) + + const result = snapshot(name, null) + + expect(result).toMatchSnapshot() +}) + +test('null tree does not fail if update is true', () => { + const name = 'null-with-update' + const tree = { type: 'div' } + snapshot(name, tree) + + const update = true + const result = snapshot(name, null, update) + + expect(result).toEqual({ pass: true }) +})