Writing test cases using Jest and Enzyme
I am working on writing test cases for a project. I am writing the test for my Container. The container has a function which is as shown below:
getContactDetails = (reqObject) => {
app.outageCenterService.getContact(reqObject).then(
response => {
app.logger.getLogger().info('Below is the response...');
app.logger.getLogger().info(this.state.contactDetails);
this.setState({contactDetails: response.contactDetails},()=>{});
if (this.state.contactDetails.isContactPresent) {
this.setState({ isVisible: true });
} else {
this.setState({ isVisible: false });
}
},
reject => {
app.logger.getLogger().info(reject);
}
);
}
While running the test,in the function,the line app.outageCenterService.getContact(reqObject)
throws an error saying TypeError: Cannot read property 'getContact' of undefined
. I understand its because outageCenterService
is globally defined and jest/enzyme is not able to find it. But I don't know how to solve this issue.
My test looks something like this:
describe('test the OutageAlert Component', () => {
let outageAlert, errorHandlerFn;
errorHandlerFn=jest.fn();
getContactFn=jest.fn();
outageAlert = shallow(<OutageAlertComponent errorHandler={errorHandlerFn} getContact={getContactFn} />);
});
Can anyone please help me with this on how to write the test case for this scenario?
reactjs jestjs enzyme
add a comment |
I am working on writing test cases for a project. I am writing the test for my Container. The container has a function which is as shown below:
getContactDetails = (reqObject) => {
app.outageCenterService.getContact(reqObject).then(
response => {
app.logger.getLogger().info('Below is the response...');
app.logger.getLogger().info(this.state.contactDetails);
this.setState({contactDetails: response.contactDetails},()=>{});
if (this.state.contactDetails.isContactPresent) {
this.setState({ isVisible: true });
} else {
this.setState({ isVisible: false });
}
},
reject => {
app.logger.getLogger().info(reject);
}
);
}
While running the test,in the function,the line app.outageCenterService.getContact(reqObject)
throws an error saying TypeError: Cannot read property 'getContact' of undefined
. I understand its because outageCenterService
is globally defined and jest/enzyme is not able to find it. But I don't know how to solve this issue.
My test looks something like this:
describe('test the OutageAlert Component', () => {
let outageAlert, errorHandlerFn;
errorHandlerFn=jest.fn();
getContactFn=jest.fn();
outageAlert = shallow(<OutageAlertComponent errorHandler={errorHandlerFn} getContact={getContactFn} />);
});
Can anyone please help me with this on how to write the test case for this scenario?
reactjs jestjs enzyme
isapp
on the global, aswindow.app.outageCenterService.getContact
?
– GibboK
Nov 20 '18 at 13:35
add a comment |
I am working on writing test cases for a project. I am writing the test for my Container. The container has a function which is as shown below:
getContactDetails = (reqObject) => {
app.outageCenterService.getContact(reqObject).then(
response => {
app.logger.getLogger().info('Below is the response...');
app.logger.getLogger().info(this.state.contactDetails);
this.setState({contactDetails: response.contactDetails},()=>{});
if (this.state.contactDetails.isContactPresent) {
this.setState({ isVisible: true });
} else {
this.setState({ isVisible: false });
}
},
reject => {
app.logger.getLogger().info(reject);
}
);
}
While running the test,in the function,the line app.outageCenterService.getContact(reqObject)
throws an error saying TypeError: Cannot read property 'getContact' of undefined
. I understand its because outageCenterService
is globally defined and jest/enzyme is not able to find it. But I don't know how to solve this issue.
My test looks something like this:
describe('test the OutageAlert Component', () => {
let outageAlert, errorHandlerFn;
errorHandlerFn=jest.fn();
getContactFn=jest.fn();
outageAlert = shallow(<OutageAlertComponent errorHandler={errorHandlerFn} getContact={getContactFn} />);
});
Can anyone please help me with this on how to write the test case for this scenario?
reactjs jestjs enzyme
I am working on writing test cases for a project. I am writing the test for my Container. The container has a function which is as shown below:
getContactDetails = (reqObject) => {
app.outageCenterService.getContact(reqObject).then(
response => {
app.logger.getLogger().info('Below is the response...');
app.logger.getLogger().info(this.state.contactDetails);
this.setState({contactDetails: response.contactDetails},()=>{});
if (this.state.contactDetails.isContactPresent) {
this.setState({ isVisible: true });
} else {
this.setState({ isVisible: false });
}
},
reject => {
app.logger.getLogger().info(reject);
}
);
}
While running the test,in the function,the line app.outageCenterService.getContact(reqObject)
throws an error saying TypeError: Cannot read property 'getContact' of undefined
. I understand its because outageCenterService
is globally defined and jest/enzyme is not able to find it. But I don't know how to solve this issue.
My test looks something like this:
describe('test the OutageAlert Component', () => {
let outageAlert, errorHandlerFn;
errorHandlerFn=jest.fn();
getContactFn=jest.fn();
outageAlert = shallow(<OutageAlertComponent errorHandler={errorHandlerFn} getContact={getContactFn} />);
});
Can anyone please help me with this on how to write the test case for this scenario?
reactjs jestjs enzyme
reactjs jestjs enzyme
edited Nov 20 '18 at 17:20
skyboyer
3,83311229
3,83311229
asked Nov 20 '18 at 10:08
pranamipranami
239314
239314
isapp
on the global, aswindow.app.outageCenterService.getContact
?
– GibboK
Nov 20 '18 at 13:35
add a comment |
isapp
on the global, aswindow.app.outageCenterService.getContact
?
– GibboK
Nov 20 '18 at 13:35
is
app
on the global, as window.app.outageCenterService.getContact
?– GibboK
Nov 20 '18 at 13:35
is
app
on the global, as window.app.outageCenterService.getContact
?– GibboK
Nov 20 '18 at 13:35
add a comment |
1 Answer
1
active
oldest
votes
You could consider use the globals config
http://facebook.github.io/jest/docs/api.html#globals-object or create a file and
and create a mock in jest for you global, then you can access it from your test.
Alternatively you could have your container accepting an argument for your contact
getContactDetails = (reqObject, contact) => { ...}
so you can pass it in your test and wherever you container is being used.
I am not allowed to change the code.So, how do I write the test in this case.So, do I have to create a mock function with forgetContactDetails
? I am sorry I didn't get how to proceed.
– pranami
Nov 22 '18 at 10:58
in this case you can use mock with globals-object
– GibboK
Nov 23 '18 at 18:54
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390624%2fwriting-test-cases-using-jest-and-enzyme%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could consider use the globals config
http://facebook.github.io/jest/docs/api.html#globals-object or create a file and
and create a mock in jest for you global, then you can access it from your test.
Alternatively you could have your container accepting an argument for your contact
getContactDetails = (reqObject, contact) => { ...}
so you can pass it in your test and wherever you container is being used.
I am not allowed to change the code.So, how do I write the test in this case.So, do I have to create a mock function with forgetContactDetails
? I am sorry I didn't get how to proceed.
– pranami
Nov 22 '18 at 10:58
in this case you can use mock with globals-object
– GibboK
Nov 23 '18 at 18:54
add a comment |
You could consider use the globals config
http://facebook.github.io/jest/docs/api.html#globals-object or create a file and
and create a mock in jest for you global, then you can access it from your test.
Alternatively you could have your container accepting an argument for your contact
getContactDetails = (reqObject, contact) => { ...}
so you can pass it in your test and wherever you container is being used.
I am not allowed to change the code.So, how do I write the test in this case.So, do I have to create a mock function with forgetContactDetails
? I am sorry I didn't get how to proceed.
– pranami
Nov 22 '18 at 10:58
in this case you can use mock with globals-object
– GibboK
Nov 23 '18 at 18:54
add a comment |
You could consider use the globals config
http://facebook.github.io/jest/docs/api.html#globals-object or create a file and
and create a mock in jest for you global, then you can access it from your test.
Alternatively you could have your container accepting an argument for your contact
getContactDetails = (reqObject, contact) => { ...}
so you can pass it in your test and wherever you container is being used.
You could consider use the globals config
http://facebook.github.io/jest/docs/api.html#globals-object or create a file and
and create a mock in jest for you global, then you can access it from your test.
Alternatively you could have your container accepting an argument for your contact
getContactDetails = (reqObject, contact) => { ...}
so you can pass it in your test and wherever you container is being used.
answered Nov 20 '18 at 13:40
GibboKGibboK
34.7k107318545
34.7k107318545
I am not allowed to change the code.So, how do I write the test in this case.So, do I have to create a mock function with forgetContactDetails
? I am sorry I didn't get how to proceed.
– pranami
Nov 22 '18 at 10:58
in this case you can use mock with globals-object
– GibboK
Nov 23 '18 at 18:54
add a comment |
I am not allowed to change the code.So, how do I write the test in this case.So, do I have to create a mock function with forgetContactDetails
? I am sorry I didn't get how to proceed.
– pranami
Nov 22 '18 at 10:58
in this case you can use mock with globals-object
– GibboK
Nov 23 '18 at 18:54
I am not allowed to change the code.So, how do I write the test in this case.So, do I have to create a mock function with for
getContactDetails
? I am sorry I didn't get how to proceed.– pranami
Nov 22 '18 at 10:58
I am not allowed to change the code.So, how do I write the test in this case.So, do I have to create a mock function with for
getContactDetails
? I am sorry I didn't get how to proceed.– pranami
Nov 22 '18 at 10:58
in this case you can use mock with globals-object
– GibboK
Nov 23 '18 at 18:54
in this case you can use mock with globals-object
– GibboK
Nov 23 '18 at 18:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53390624%2fwriting-test-cases-using-jest-and-enzyme%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
is
app
on the global, aswindow.app.outageCenterService.getContact
?– GibboK
Nov 20 '18 at 13:35