Skip to content

Commit

Permalink
Prevent crashing when a component is null (#26)
Browse files Browse the repository at this point in the history
* failing specs

* updates to pass specs

* fixed typo in spec
  • Loading branch information
joshwnj authored and MicheleBertoli committed Nov 15, 2017
1 parent 87782a7 commit 9ffdeb8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions test/core/__snapshots__/snapshot.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
exports[`test fails if new is null but old is not 1`] = `
Object {
"actual": "null",
"count": 1,
"diff": "",
"expected": "<div />",
"pass": false,
}
`;

exports[`test fails if old was null but new is not 1`] = `
Object {
"actual": "<div />",
"count": 1,
"diff": "",
"expected": "null",
"pass": false,
}
`;

exports[`test fails if the type changes 1`] = `
Object {
"actual": "<span>
Expand All @@ -19,3 +39,9 @@ Object {
"pass": false,
}
`;

exports[`test passes if new and old are null 1`] = `
Object {
"pass": true,
}
`;
46 changes: 46 additions & 0 deletions test/core/snapshot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})

0 comments on commit 9ffdeb8

Please sign in to comment.