How to mock object in mocha unittest? - javascript

In my reactjs component I have a method called getContainer(doc) with this line:
let w = Math.max(doc.documentElement.clientWidth, window.innerWidth);
It takes 1 parameter called doc: htmldocument. In my mocha unittest I got:
let mockDoc = {documentElement: {clientWidth:100}}
let res = wrapper.instance().getContainer(mockDoc);
When I run the test I still get an error , how can I fix this?
Error:
TypeError: Cannot read property 'clientWidth' of undefined

you can use an visual dom library like jsdom,for more details you can see jsdom.it is simple to use.here is a demo by enzyme

Related

How to get properties of SpecResult in the Jasmine?

How to get access to the status property of SpecResult type of Global in the afterEach block in the Jasmine ver. 4.5?
I'm using bare Jasmine (with no Protracktor or WebDriverIO) and Selenium. I'd like to take screenshots only for tests that failed.
How can I get access to the current test status in the afterEach block?
I've tried this:
let status = jasmine.getEnv.SpecResult.status
let status = jasmine.getEnv().SpecResult.status
let status = jasmine.SpecResult.status
let status = this.SpecResult.status
But all of these got or 'undefined' or 'cannot read property of ...'

Mapbox-Gl-Draw: Cannot set property 'modes' of undefined

I am trying to initialize a MapboxDraw object with the following JS code:
var graphicsController = MapboxDraw(); // Initialize the graphics controller
I am importing mapbox-gl-draw w/ the following CDN:
<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.js'></script>
However I get the following error message:
Uncaught TypeError: Cannot set property 'modes' of undefined
(mapbox-gl-draw.js:1)
What does this mean? Is it some bug of mapbox-gl-draw that I can just ignore or do I need to do anything to fix it?
MapboxDraw is a constructor, so you need to initialize it with new keyword:
var graphicsController = new MapboxDraw();

how to get/set TestStep's property value using javascript in soapUI?

i have tried using Groovy script.
the following code is to set property value using roovy script:
testRunner.testCase.testSuite.testCases[testCaseName].testSteps[testStepName].setPropertyValue("request",object);
"request" is a property of testStep.
object is a some value.
when i try above code in javascript but i got following error:
org.mozilla.javascript.ecmaerror: TypeErro: org.mozilla.javascript.ecmaerror Cannot read property "testSteps" from undefined.
So please tell me how to use in using javascript?
hurra, i got solution. Please see following code:
var project = testRunner.getTestCase().getTestSuite().getProject();
var testSuite = project.getTestSuiteByName("TestSuite");
var testCasesItr=testSuite.getTestCaseList().iterator();
while(testCasesItr.hasNext())
{
var testStepsItr = testCasesItr.getTestStepList().iterator();
while(testStepsItr.hasNext()){
var testStep = testStepsItr.next();
log.info(testStep.getPropertyValue("response"));
// here you can set property
// testStep.setPropertyValue("request","someValue");
}
}
above code will run for all the testcases.

I am not able to use webdriver-js-extender, inorder to access the mobile commands provided by protractor

My hook for webdriver-js-extender
var webdriver = require('selenium-webdriver');
var extendedWebdriver = require('webdriver-js-extender').extend(webdriver);
on calling setNetworkConnection(5) using extendedWebdriver object it throows below mentioned error
extendedWebdriver.setNetworkConnection(5);
Message:
Failed: Cannot read property 'defineCommand' of undefined Stack:
TypeError: Cannot read property 'defineCommand' of undefined
at Extender.defineCommand (/Users/abhishek/abhishek-test-volunteeringMobileApp/node_modules/webdriver-js-extender/lib/extender.ts:33:19)
at CommandDefinition.compile (/Users/abhishek/abhishek-test-volunteeringMobileApp/node_modules/webdriver-js-extender/lib/command_definition.ts:13:16)
at Object.extend (/Users/abhishek/abhishek-test-volunteeringMobileApp/node_modules/webdriver-js-extender/lib/index.ts:65:50)

Run jsTestDriver in WebStorm for Jasmine + Backbone

I am writing tests in Jasmine for a Backbone application and I want to know what proportion of the code my tests cover. For this goal I want to use jsTestDriver. But I have a problem: I created a config file and added all resources there, but when I start test Backbone methods don't define. This is my config file:
server: http://localhost:9876
load:
- lib/jasmine-1.3.1/jasmine.js
- lib/jasmine-jquery.js
- lib/JasmineAdapter.js
- lib/sinon-1.5.2.js
- cordova-2.2.0.js
- libs/jquery-1.8.2.min.js
- libs/underscore-min.js
- libs/backbone-min.js
- libs/lazyload-min.js
- core/js/core.js
- index.js
test:
- spec/test.js
The order is the same as on SpecRunner file. It is my test file:
describe("Attributes", function(){
it("Test", function() {
c = new Cars;
expect(c.attributes.StartDate).toBeDefined();
expect(c.attributes.StartDate).toBeDefined();
})
});
Cars is a Backbone model and this model has default attribute StartSate. In my test I want check that this attribute defined. And ofcourse the mistake in WebStorm:
TypeError: TypeError: Cannot read property 'attributes' of undefined
TypeError: Cannot read property 'attributes' of undefined
at null.<anonymous> (spec/test.js:10:21)
I think it's better to use the has method of the model object to check for an attribute, rather than checking the attributes property:
describe("Attributes", function(){
it("Test", function() {
c = new Cars;
expect(c.has("StartDate")).toBe(true);
})
});
This way you can add some implicit logic to the model that can override the has method. Also, you didn't specify how you extended the model to create your Cars class. Have you specified default values?

Categories