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

Enzyme .text() selector fails on emotion styled components #733

Closed
tennisgent opened this issue Jun 18, 2018 · 4 comments
Closed

Enzyme .text() selector fails on emotion styled components #733

tennisgent opened this issue Jun 18, 2018 · 4 comments
Labels

Comments

@tennisgent
Copy link

  • emotion version: 9.2.3
  • react version: 16.3.2

Relevant code.

const AppContainer = styled('div')`
  font-family: sans-serif;
  text-align: center;
`;

function App() {
  return <AppContainer>Hello CodeSandbox</AppContainer>;
}

test("should contain proper text", () => {
  const wrapper = shallow(<App />);
  expect(wrapper.text()).toEqual("Hello CodeSandbox");
});

What you did:

I am trying to test that a given emotion styled-component has the proper text as children using Enzyme. I have written the above test and it fails with the error message below:

What happened:

Expected value to equal:
  "Hello CodeSandbox"
Received:
  "<Styled(div) />"

Reproduction:
Here is the error in CodeSandbox: https://codesandbox.io/s/5zkq790j74

Interestingly, if I call wrapper.debug(), it seems to output the proper markup:

test("should contain proper text", () => {
  const wrapper = shallow(<App />);
  console.log(wrapper.debug());
  expect(wrapper.text()).toEqual("Hello CodeSandbox");
});

// Outputs:
// <Styled(div)>
//   Hello CodeSandbox
// </Styled(div)> 
@Ailrun
Copy link
Member

Ailrun commented Jun 18, 2018

Thank you for your report!
However, even without styled component, you example doesn't work.
You can check it via following code.

export function App() {
  return <div>Hello CodeSandbox</div>;
}

const wrapper = shallow(<App />);
console.log(wrapper.text());

You should access children of wrapper to get a valid text node, instead of accessing it directly.

@tennisgent
Copy link
Author

tennisgent commented Jun 19, 2018

Sorry. That example was probably a little too simplistic. This example I think better illustrates the issue:

CodeSandbox 2: https://codesandbox.io/s/qkl68mk86w

@Ailrun
Copy link
Member

Ailrun commented Jun 19, 2018

@tennisgent If you introduce a new component like

export function X({ className, children }) {
  return (
    <div className={className}>{children}</div>
  )
}

export function Simple() {
  return (
    <div className="container">
      Hello CodeSandbox
      <X className="nested">Blue</X>
    </div>
  );
}

You will see that both tests fail, even after changing test to expect(wrapper.find(X).text()).toEqual("Blue");. It's what enzyme does, not we do.... Sorry for that, but there is not much we can do.

@tennisgent
Copy link
Author

Sorry for the confusion. I apparently didn't understand how Enzyme is handling custom components. Thanks for setting me straight. 👍

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

2 participants