React - How to test a decorated class











up vote
0
down vote

favorite












how can I test a decorated component?
I decorated a component using the react-tracking library, like:



@track((props) => ({category: 'EDIT', page: 
props.history.location.pathname}))
class EditTemplate extends React.Component<Props, State> {...}

export default EditTemplate;


When I try to test it like this:



it("test description", () => {
const wrapper = mount(<EditTemplate history={mockHistory}/>);
expect(wrapper.prop("history")).toEqual("xyz");
expect(wrapper.state("code")).toEqual("some value");
});


the prosp test works fine, but the state is always null! I tried using instance(), dive(), get(0), with the wrapper but the result it's always the same. If I remove the decorator, the test passes.
Any suggestions?
Thanks










share|improve this question
























  • what does decorator do?
    – skyboyer
    Nov 7 at 10:17










  • adds the track information to the component. It's the react-tracking library
    – gepeppe
    Nov 7 at 11:07










  • yes, missed that. so it's just ordinary HOC but used as decorator. don't see any magic there. have just played with example from docs and wrapper.dive().state() definitely works
    – skyboyer
    Nov 7 at 12:52










  • it says that "state() can only be called on the root"... I created a function in the component that returns the state, and I got the state from that function directly and it works. I'd be nice to access to it without the workaround.
    – gepeppe
    Nov 7 at 15:54










  • so have you finally dealt with checking props and only state is left to work at? but do you really need accessing state directly? typically we call some callback prop or simulate some events and then just validate component's state through .toMatchSnapshot(). maybe it would be better to go this way.
    – skyboyer
    Nov 7 at 16:09















up vote
0
down vote

favorite












how can I test a decorated component?
I decorated a component using the react-tracking library, like:



@track((props) => ({category: 'EDIT', page: 
props.history.location.pathname}))
class EditTemplate extends React.Component<Props, State> {...}

export default EditTemplate;


When I try to test it like this:



it("test description", () => {
const wrapper = mount(<EditTemplate history={mockHistory}/>);
expect(wrapper.prop("history")).toEqual("xyz");
expect(wrapper.state("code")).toEqual("some value");
});


the prosp test works fine, but the state is always null! I tried using instance(), dive(), get(0), with the wrapper but the result it's always the same. If I remove the decorator, the test passes.
Any suggestions?
Thanks










share|improve this question
























  • what does decorator do?
    – skyboyer
    Nov 7 at 10:17










  • adds the track information to the component. It's the react-tracking library
    – gepeppe
    Nov 7 at 11:07










  • yes, missed that. so it's just ordinary HOC but used as decorator. don't see any magic there. have just played with example from docs and wrapper.dive().state() definitely works
    – skyboyer
    Nov 7 at 12:52










  • it says that "state() can only be called on the root"... I created a function in the component that returns the state, and I got the state from that function directly and it works. I'd be nice to access to it without the workaround.
    – gepeppe
    Nov 7 at 15:54










  • so have you finally dealt with checking props and only state is left to work at? but do you really need accessing state directly? typically we call some callback prop or simulate some events and then just validate component's state through .toMatchSnapshot(). maybe it would be better to go this way.
    – skyboyer
    Nov 7 at 16:09













up vote
0
down vote

favorite









up vote
0
down vote

favorite











how can I test a decorated component?
I decorated a component using the react-tracking library, like:



@track((props) => ({category: 'EDIT', page: 
props.history.location.pathname}))
class EditTemplate extends React.Component<Props, State> {...}

export default EditTemplate;


When I try to test it like this:



it("test description", () => {
const wrapper = mount(<EditTemplate history={mockHistory}/>);
expect(wrapper.prop("history")).toEqual("xyz");
expect(wrapper.state("code")).toEqual("some value");
});


the prosp test works fine, but the state is always null! I tried using instance(), dive(), get(0), with the wrapper but the result it's always the same. If I remove the decorator, the test passes.
Any suggestions?
Thanks










share|improve this question















how can I test a decorated component?
I decorated a component using the react-tracking library, like:



@track((props) => ({category: 'EDIT', page: 
props.history.location.pathname}))
class EditTemplate extends React.Component<Props, State> {...}

export default EditTemplate;


When I try to test it like this:



it("test description", () => {
const wrapper = mount(<EditTemplate history={mockHistory}/>);
expect(wrapper.prop("history")).toEqual("xyz");
expect(wrapper.state("code")).toEqual("some value");
});


the prosp test works fine, but the state is always null! I tried using instance(), dive(), get(0), with the wrapper but the result it's always the same. If I remove the decorator, the test passes.
Any suggestions?
Thanks







react-native testing jestjs enzyme






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 16:06









skyboyer

2,69511028




2,69511028










asked Nov 7 at 7:44









gepeppe

96111




96111












  • what does decorator do?
    – skyboyer
    Nov 7 at 10:17










  • adds the track information to the component. It's the react-tracking library
    – gepeppe
    Nov 7 at 11:07










  • yes, missed that. so it's just ordinary HOC but used as decorator. don't see any magic there. have just played with example from docs and wrapper.dive().state() definitely works
    – skyboyer
    Nov 7 at 12:52










  • it says that "state() can only be called on the root"... I created a function in the component that returns the state, and I got the state from that function directly and it works. I'd be nice to access to it without the workaround.
    – gepeppe
    Nov 7 at 15:54










  • so have you finally dealt with checking props and only state is left to work at? but do you really need accessing state directly? typically we call some callback prop or simulate some events and then just validate component's state through .toMatchSnapshot(). maybe it would be better to go this way.
    – skyboyer
    Nov 7 at 16:09


















  • what does decorator do?
    – skyboyer
    Nov 7 at 10:17










  • adds the track information to the component. It's the react-tracking library
    – gepeppe
    Nov 7 at 11:07










  • yes, missed that. so it's just ordinary HOC but used as decorator. don't see any magic there. have just played with example from docs and wrapper.dive().state() definitely works
    – skyboyer
    Nov 7 at 12:52










  • it says that "state() can only be called on the root"... I created a function in the component that returns the state, and I got the state from that function directly and it works. I'd be nice to access to it without the workaround.
    – gepeppe
    Nov 7 at 15:54










  • so have you finally dealt with checking props and only state is left to work at? but do you really need accessing state directly? typically we call some callback prop or simulate some events and then just validate component's state through .toMatchSnapshot(). maybe it would be better to go this way.
    – skyboyer
    Nov 7 at 16:09
















what does decorator do?
– skyboyer
Nov 7 at 10:17




what does decorator do?
– skyboyer
Nov 7 at 10:17












adds the track information to the component. It's the react-tracking library
– gepeppe
Nov 7 at 11:07




adds the track information to the component. It's the react-tracking library
– gepeppe
Nov 7 at 11:07












yes, missed that. so it's just ordinary HOC but used as decorator. don't see any magic there. have just played with example from docs and wrapper.dive().state() definitely works
– skyboyer
Nov 7 at 12:52




yes, missed that. so it's just ordinary HOC but used as decorator. don't see any magic there. have just played with example from docs and wrapper.dive().state() definitely works
– skyboyer
Nov 7 at 12:52












it says that "state() can only be called on the root"... I created a function in the component that returns the state, and I got the state from that function directly and it works. I'd be nice to access to it without the workaround.
– gepeppe
Nov 7 at 15:54




it says that "state() can only be called on the root"... I created a function in the component that returns the state, and I got the state from that function directly and it works. I'd be nice to access to it without the workaround.
– gepeppe
Nov 7 at 15:54












so have you finally dealt with checking props and only state is left to work at? but do you really need accessing state directly? typically we call some callback prop or simulate some events and then just validate component's state through .toMatchSnapshot(). maybe it would be better to go this way.
– skyboyer
Nov 7 at 16:09




so have you finally dealt with checking props and only state is left to work at? but do you really need accessing state directly? typically we call some callback prop or simulate some events and then just validate component's state through .toMatchSnapshot(). maybe it would be better to go this way.
– skyboyer
Nov 7 at 16:09

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185291%2freact-how-to-test-a-decorated-class%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185291%2freact-how-to-test-a-decorated-class%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud