question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to test decorated components?

See original GitHub issue

Hi! I have problem with testing decorated by translate components. Just simple example:

import React, { PropTypes } from 'react';
import { translate } from 'react-i18next';
import ProductBadge from './ProductBadge';

const ProductBadgeNew = ({ product, t }) => (
  product.is_label_new
    ? <ProductBadge text={t('vendor.badges.new')} status="sold" />
    : <span />
);

export default translate(ProductBadgeNew);

First of all when we test components, we render them into document via:

import { renderIntoDocument } from 'react-addons-test-utils';
import ProductBadgeNew from '../../ProductBadgeNew';

describe('[Component] ProductBadgeNew', () => {
  it('should render without errors when there aren\'t any props', () => {
    const component = renderIntoDocument(
      <ProductBadgeNew />
    );

    expect(component).to.be.an('object');
  });
});

But we have been decorated ProductBadgeNew component with translate, and here component variable contains Translate component which decorates ProductBadgeNew. And I can’t get props for my ProductBadgeNew. I’ve found one interesting solution https://github.com/ghengeveld/redux/blob/a68768ff8d6a4fbe7b8f6a06a8cf9b99a54aefb0/docs/recipes/WritingTests.md#testing-decorated-react-components The code after changes is:

import React, { PropTypes } from 'react';
import { translate } from 'react-i18next';
import ProductBadge from './ProductBadge';

export const ProductBadgeNew = ({ product, t }) => (
  product.is_label_new
    ? <ProductBadge text={t('vendor.badges.new')} status="sold" />
    : <span />
);

export default translate(ProductBadgeNew);
import { ProductBadgeNew } from '../../ProductBadgeNew';

describe('[Component] ProductBadgeNew', () => {
  it('should render without errors when there aren\'t any props', () => {
    const component = renderIntoDocument(
      <ProductBadgeNew />
    );

    expect(component).to.be.an('object');
  });
});

Now component variable contains our vanilla component without any decorations. BUT!😃 Inside component we use t function which is passed via props and of course we don’t have it now. How can I fix my testing workflow with your decorators? Thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
jamesvclementscommented, Oct 12, 2017

Stumbled on this when dealing with the same issue. We found the easiest solution to be mocking the translate function of react-i18next:

jest.mock('react-i18next', () => ({
  // this mock makes sure any components using the translate HoC receive the t function as a prop
  translate: () => Component => props => <Component t={() => ''} {...props} />,
}));

This helps address @sergeylaptev 's comment about complex components with child components also using the HoC

11reactions
jamuhlcommented, Dec 13, 2016

what we do for testing is:

export function MyComponent(props) {  ...  };

export default translated(MyComponent);

for test we import { MyComponent } from './MyComponent'; -> no hoc just pure component to test

Read more comments on GitHub >

github_iconTop Results From Across the Web

Please add instructions how to test decorated methods #112
The only way to test this decorator is to extract the callback function. Example 2: There is no way to jest.spyOn() on the...
Read more >
How to test decorated React component with shallow ...
You can't. First let's slightly desugar the decorator: let PlayerProfile = withMUI( class PlayerProfile extends React.Component { // .
Read more >
How To Test Python Decorators? How To Bypass A Decorator?
In this article, I show you some of the best practices to test your decorators. ... The @wraps Decorator - Accessing The Decorated...
Read more >
Testing Python decorators
Testing decorators is tiny bit different. You need to leave decorated function out of the equation and instead verify whether decorator does ...
Read more >
Create and Test Decorators in JavaScript
The logProperty decorator above redefines the decorated property on the object. We can define a new property to the constructor's prototype by ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found