Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crashing when a component is null #26

Merged
merged 3 commits into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
})