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

String for mocks looks weird #12

Closed
eskimoblood opened this issue Sep 29, 2016 · 10 comments
Closed

String for mocks looks weird #12

eskimoblood opened this issue Sep 29, 2016 · 10 comments
Labels

Comments

@eskimoblood
Copy link

I use Jest to mock out some child components, with the react-test-renderer I got

<mocked someProps="someString" />

using enzyme and shallowToJson I got:

<function (props) {return (/* istanbul ignore next */_react2.default.createElement( /* istanbul ignore next */'mocked', /* istanbul ignore next */_extends({ originalComponent: componentName }, props), props.children));} someProps="someString" />

Do you think its possible to recognise mocks and render they just like the react-test-renderer.

@adriantoine
Copy link
Owner

Hi, thanks for raising the bug, I will have a look!

@adriantoine
Copy link
Owner

@eskimoblood Can you give me more details about your test? How exactly are you mocking the component?

@adriantoine
Copy link
Owner

In one of my project, I'm using enzyme-to-json and Jest mocking like that:

jest.mock('../Field', () => {
    return function Field(props) {
        return props.children;
    };
});

and it works fine with Field as a display name.

But I'm interested in your issue, can you provide more details?

@eskimoblood
Copy link
Author

I'm using the approach from thus gist

@eskimoblood
Copy link
Author

But let me try your approach, maybe this fix the problem.

@adriantoine
Copy link
Owner

Cool, thanks for the link, I'm looking at it!

@adriantoine
Copy link
Owner

Ok so I've just tried that (basically just copying from your link):

function mockComponent(componentName) {
    return props => {
        return (
            <mocked originalComponent={componentName} {...props}>{props.children}</mocked>
        );
    };
}

jest.mock('./fixtures/mocked-component', () => { return mockComponent('Icon'); });

import MockedComponent from './fixtures/mocked-component';

it('converts a mocked component', () => {
    const shallowed = shallow(
        <MockedComponent className="class"><strong>Hello!</strong></MockedComponent>
    );
    expect(shallowToJson(shallowed)).toMatchSnapshot();
});

and the snapshot output is:

exports[`test converts a mocked component 1`] = `
<mocked
  className="class"
  originalComponent="Icon">
  <strong>
    Hello!
  </strong>
</mocked>
`;

which seems ok 🤔

@bsr203
Copy link

bsr203 commented Sep 29, 2016

I too had transpiled code in my snapshot even with react-test-renderer jestjs/jest#1756

though it is closed, @cpojer acknowledge it was reported by others too.

@eskimoblood
Copy link
Author

I just move to

jest.mock('../../components', () => {
  return {
    TextField: () => 'TextField',
    DatePicker: () => 'DatePicker'
  }
});

as suggested in the comments of the gist and it works. So just close the issue. Thanks for your help.

@adriantoine
Copy link
Owner

Ok thanks for the insight @bsr203, that's strange 🤔

@eskimoblood I'm glad it works with you 😊 I'm still curious of what was causing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants