From 4fc2e823bd3c45b5f21fdf1a7157a5a3444efb70 Mon Sep 17 00:00:00 2001 From: DianaSuvorova Date: Thu, 17 Aug 2017 08:33:53 -0700 Subject: [PATCH] Asserting undefined value intentionally (#144) * asserting undefined value * adding enzyme-matchers undefined value tests --- .../src/assertions/__tests__/toHaveProp--tests.js | 14 ++++++++++++++ .../src/assertions/__tests__/toHaveState--tests.js | 14 ++++++++++++++ .../enzyme-matchers/src/assertions/toHaveProp.js | 5 ++--- .../enzyme-matchers/src/assertions/toHaveState.js | 4 ++-- .../src/__failing_tests__/toHaveProp.test.js | 7 +++++++ .../src/__failing_tests__/toHaveState.test.js | 4 ++++ 6 files changed, 43 insertions(+), 5 deletions(-) diff --git a/packages/enzyme-matchers/src/assertions/__tests__/toHaveProp--tests.js b/packages/enzyme-matchers/src/assertions/__tests__/toHaveProp--tests.js index dc87d5c..07ed2c7 100644 --- a/packages/enzyme-matchers/src/assertions/__tests__/toHaveProp--tests.js +++ b/packages/enzyme-matchers/src/assertions/__tests__/toHaveProp--tests.js @@ -110,6 +110,20 @@ describe('toHaveProp', () => { expect(pass).toBeTruthy(); expect(fail).toBeFalsy(); }); + + it('works without a prop value', () => { + const wrapper = builder(); + const truthy = toHaveProp(wrapper.find(User), 'name'); + + expect(truthy.pass).toBeTruthy(); + }); + + it('works with undefined value', () => { + const wrapper = builder(); + const falsy = toHaveProp(wrapper.find(User), 'name', undefined); + + expect(falsy.pass).toBeFalsy(); + }); }); }); }); diff --git a/packages/enzyme-matchers/src/assertions/__tests__/toHaveState--tests.js b/packages/enzyme-matchers/src/assertions/__tests__/toHaveState--tests.js index fe7bfb0..5704b88 100644 --- a/packages/enzyme-matchers/src/assertions/__tests__/toHaveState--tests.js +++ b/packages/enzyme-matchers/src/assertions/__tests__/toHaveState--tests.js @@ -89,6 +89,20 @@ describe('toHaveState', () => { expect(truthyResults.pass).toBeTruthy(); expect(falsyResults.pass).toBeFalsy(); }); + + it('works without a prop value', () => { + const wrapper = builder(); + const truthy = toHaveState(wrapper, 'foo'); + + expect(truthy.pass).toBeTruthy(); + }); + + it('works with undefined value', () => { + const wrapper = builder(); + const falsy = toHaveState(wrapper, 'foo', undefined); + + expect(falsy.pass).toBeFalsy(); + }); }); }); }); diff --git a/packages/enzyme-matchers/src/assertions/toHaveProp.js b/packages/enzyme-matchers/src/assertions/toHaveProp.js index 51ddc47..adcd9b7 100644 --- a/packages/enzyme-matchers/src/assertions/toHaveProp.js +++ b/packages/enzyme-matchers/src/assertions/toHaveProp.js @@ -18,7 +18,6 @@ function toHaveProp( propValue?: any ): Matcher { const props = enzymeWrapper.props(); - const contextualInformation = { actual: `Actual: ${stringify({ [propKey]: props[propKey] })}`, expected: `Expected: ${stringify({ [propKey]: propValue })}`, @@ -37,8 +36,8 @@ function toHaveProp( } // key exists given above check, and we're not validating over values, - // so its always true - if (propValue === undefined) { + // so its always true unless the undefined value was provided explicitly + if (propValue === undefined && arguments.length === 2) { return { pass: true, message: `Expected wrapper to have any value for the prop "${propKey}"`, diff --git a/packages/enzyme-matchers/src/assertions/toHaveState.js b/packages/enzyme-matchers/src/assertions/toHaveState.js index e64cc3a..904fa60 100644 --- a/packages/enzyme-matchers/src/assertions/toHaveState.js +++ b/packages/enzyme-matchers/src/assertions/toHaveState.js @@ -37,8 +37,8 @@ function toHaveState( } // key exists given above check, and we're not validating over values, - // so its always true - if (stateValue === undefined) { + // so its always true unless the undefined value was provided explicitly + if (stateValue === undefined && arguments.length === 2) { return { pass: true, message: `Expected <${name( diff --git a/packages/jest-enzyme/src/__failing_tests__/toHaveProp.test.js b/packages/jest-enzyme/src/__failing_tests__/toHaveProp.test.js index 778f6cc..844e598 100644 --- a/packages/jest-enzyme/src/__failing_tests__/toHaveProp.test.js +++ b/packages/jest-enzyme/src/__failing_tests__/toHaveProp.test.js @@ -16,4 +16,11 @@ describe('failing test', () => { it('fails NOT toHaveProp', () => { expect(shallow()).not.toHaveProp('disabled', true); }); + + it('fails toHaveProp undefined value', () => { + expect(shallow()).toHaveProp( + 'enabled', + undefined + ); + }); }); diff --git a/packages/jest-enzyme/src/__failing_tests__/toHaveState.test.js b/packages/jest-enzyme/src/__failing_tests__/toHaveState.test.js index 2efe37c..92cd2c1 100644 --- a/packages/jest-enzyme/src/__failing_tests__/toHaveState.test.js +++ b/packages/jest-enzyme/src/__failing_tests__/toHaveState.test.js @@ -28,4 +28,8 @@ describe('failing test', () => { expect(shallow()).not.toHaveState('foo', true); }); + + it('fails toHaveState undefined value', () => { + expect(shallow()).toHaveState('foo', undefined); + }); });