Skip to content

Commit

Permalink
Asserting undefined value intentionally (#144)
Browse files Browse the repository at this point in the history
* asserting undefined value

* adding enzyme-matchers undefined value tests
  • Loading branch information
DianaSuvorova authored and blainekasten committed Aug 17, 2017
1 parent 8f12d8a commit 4fc2e82
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ describe('toHaveProp', () => {
expect(pass).toBeTruthy();
expect(fail).toBeFalsy();
});

it('works without a prop value', () => {
const wrapper = builder(<Fixture />);
const truthy = toHaveProp(wrapper.find(User), 'name');

expect(truthy.pass).toBeTruthy();
});

it('works with undefined value', () => {
const wrapper = builder(<Fixture />);
const falsy = toHaveProp(wrapper.find(User), 'name', undefined);

expect(falsy.pass).toBeFalsy();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ describe('toHaveState', () => {
expect(truthyResults.pass).toBeTruthy();
expect(falsyResults.pass).toBeFalsy();
});

it('works without a prop value', () => {
const wrapper = builder(<Fixture />);
const truthy = toHaveState(wrapper, 'foo');

expect(truthy.pass).toBeTruthy();
});

it('works with undefined value', () => {
const wrapper = builder(<Fixture />);
const falsy = toHaveState(wrapper, 'foo', undefined);

expect(falsy.pass).toBeFalsy();
});
});
});
});
5 changes: 2 additions & 3 deletions packages/enzyme-matchers/src/assertions/toHaveProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })}`,
Expand All @@ -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}"`,
Expand Down
4 changes: 2 additions & 2 deletions packages/enzyme-matchers/src/assertions/toHaveState.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-enzyme/src/__failing_tests__/toHaveProp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ describe('failing test', () => {
it('fails NOT toHaveProp', () => {
expect(shallow(<Fixture disabled />)).not.toHaveProp('disabled', true);
});

it('fails toHaveProp undefined value', () => {
expect(shallow(<Fixture disabled={'value'} />)).toHaveProp(
'enabled',
undefined
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ describe('failing test', () => {

expect(shallow(<Fixture disabled />)).not.toHaveState('foo', true);
});

it('fails toHaveState undefined value', () => {
expect(shallow(<Fixture />)).toHaveState('foo', undefined);
});
});

0 comments on commit 4fc2e82

Please sign in to comment.