Mock #ViewChild using Jest Angular - unit-testing

I am using Jest version 28.1.0 and Angular version 12.2.17. I want to mock below code but unable to do it. Please guide me how can I mock #ViewChild.
#ViewChild(ChildComponent) childComponent!: ChildComponent;
apply(): void {
this.childComponent.apply();
}

Related

Mocking PDFtron webviewer with jest in react application

Hi I am new to unit testing and I am trying to mock the 'annotationManager' of the PDFtron webviewer
I am using this below code in my test file
jest.mock('#pdftron/webviewer', () =>({
annotationManager: {
getAnnotationsList: jest.fn().mockReturnValue([]),
deleteAnnotations: jest.fn()
},
}));
In the code, I'm getting the list of annotations using 'getAnnotationsList' function and deleting it using 'deleteAnnotations' function.
In the log of the unit tests, I'm getting this following error
'cannot read the properties of undefined (reading 'getAnnotationsList')
Is this the correct way to doing things or am I missing something?
are you able to share an example of a test you are writing where you need to mock the annotation manager? Depending on how you are using the WebViewer package, mocking the annotation manager can be different. If you prefer to reach out to Apryse directly you can also reach out to them via their support form

Mocking google.maps.LatLng in jest

I am using heatmap layer for google maps, and while mapping data for heatmap, i am using constructor new google.maps.LatLng(lat, lng), and everything works file. In index.html file i have loaded //maps.googleapis.com/maps/api... and i guess that is where i get global google object with which i can call that constructor.
So the real issue is when I try to write unit test for that using Jest.
I get the message that google is not defined in global.
I tried to mock global.google in the setup for the tests but couldn't get it done.
use https://www.npmjs.com/package/#googlemaps/jest-mocks.
import { initialize } from "#googlemaps/jest-mocks";
beforeEach(() => {
initialize();
});
// Your tests

insert data to database before running protractor e2e test

I have protractor e2e tests in my project, but I have a problem to insert data to the db before running the e2e test. What is the way to do it? I have java unit test that can insert the data. Is there a way to call the java unit tests from protractor?
Thanks,
You can define a function in your protractor configuration file:
// protractor.conf.js
exports.config = {
beforeLaunch: function() {
// You can do it here.
}
...
onPrepare: function() {
// Or here.
}
https://github.com/angular/protractor/blob/master/docs/referenceConf.js#L178
You can run a java process from node with child_process. Call java from the onPrepare function: http://nodejs.org/api/child_process.html

Zend Framework 2 unit testing using CodeCeption

We are currently using CodeCeption to do some acceptance testing, but I would like to also add unit testing to the current setup.
Has anyone done Unit testing in Zend Framework 2 project using CodeCeption? And if so, how did you set up the environment?
Found this in codeception docs: http://codeception.com/docs/modules/ZF2
There is an example in this github project: https://github.com/Danielss89/zf2-codeception
I solved this problem by using PHPUnit. I followed the Zend PHPUnit guide, http://framework.zend.com/manual/current/en/tutorials/unittesting.html, which only got me so far. But, I was able to bootstrap Zend using a phpunit.xml file and pointing that to _bootstrap.php. Codeception is also able to bootstrap my tests using the _bootstrap.php file. So, now I can run my tests using both:
phpunit --bootstrap tests/unit/_bootstrap.php
and
php vendor/bin/codecept run unit
I was surprised to see how few examples there are for this. I was writing tests for a job queue client. Here is a bit of my test:
protected function setUp()
{
$this->serviceManager = new ServiceManager(new ServiceManagerConfig());
$config = Bootstrap::getConfig();
$this->serviceManager->setService('ApplicationConfig', $config);
$this->serviceManager->get('ModuleManager')->loadModules();
$this->client = $this->serviceManager->get('jobclient');
$this->client->addServers(array(array('ip' => '127.0.0.1', 'port' => '666')));
}
public function testServiceCreateSuccess() {
$this->client->setQueue($this->testData['queue']);
$this->assertEquals($this->client->getQueue(), $this->testData['queue'], 'We get the expected queue');
}
I mocked the job client and had a local config. My _bootstrap.php file looks basically the same as the Zend example except that I am using factories defined in the service config to manage some objects. So, I performed the "setService" method in the test setup rather then in the bootsrap init.

How to integrate existing SOAP webservice (CXF) implementation into Grails application?

We have an existing interface and implementation for a webservice. We would like to embed this code into a Grails 2.x application. How can i achieve this?
Take a look at the grails cxf plugin.
http://grails.org/plugin/cxf
You with it can do stuff like
import javax.jws.*
class TestService {
static expose=['cxfjax']
#WebResult(name="addResult")
#WebMethod(operationName="add")
int add(#WebParam(name="a")int a, #WebParam(name="b")int b) {
return a + b
}
}
If your do not have groovy, but have java, then use the plugin and wire in the java services using resources.groovy or resources.xml