How to import external script in a Jest unit test? - zendesk-sdk

I am writing a Jest unit test in my React app.
In my function there is a ZAFClient object which is available from
<script src="https://assets.zendesk.com/apps/sdk/2.0/zaf_sdk.js" />
I add above script in my React app home page html.
Now obviously the ZAFClient object is not available in the Jest environment, hence I am facing issues while writing unit tests.
Either I want to add this script in my jest unit test or mock this Object anyhow. Not know how to do these!
Can anyone help here. Thanks
Note- My app is React app runs in Zendesk Support App environment in IFrame.

Related

Understanding XCTestCase life cycle - How to perform setup BEFORE Xcode unit test launches the app?

I am new to unit tests in Xcode and Swift and have some trouble to understand the life cycle of XCTestCase.
How/where to add setup code which is executed before the actual app is launched?
Problem is, that first the host app is launched before any of the test setup methods (class func setUp(), func setUp(), func setUpWithError()) are executed.
It is even possible to run test code before the host app launches?
Details:
As described in a previous question my app uses a SQLite database to persist some data. When the app launches a database connection is created and data is read from the database.
To make tests consistent and repeatable I would like to use a fresh database with some well defined data every time a run the test. To archive this I tried to override setUpWithError remove the existing db file and move a file with pre-defined data in place instead.
Unfortunately this does not work, because setUpWithError is executed only after the host app was launched. The same is true for all other test setup methods.
Moving a fresh database file in place before running the tests is only an example. The problem is the same for all local data which should be in place before the host app launches to ensure repeatable tests.
An answer to my previous question included a UIApplication extension with a isTesting method which can be used to check if a test is performed. While I could use this in my app code to setup the test data, I would consider this a bad solution. I would like to keep the code completely separated from the production code. Is this possible?
There are several approaches to set up data before running a test case
NSPrincipalClass
As described here you can create a class, and the init method of that class is executed before running any test. This helps setting up dependencies used by many test cases. I don't think this is the way to go in your case.
isTesting
Instead of setting up the code in your app target, you can also check for isTesting early in the didFinishLaunchingWithOptions method of your AppDelegate and simply return there. In this case the regular code is not executed and you can run you custom database setup code in the setup of your test class.
Dependency Injection
Right now you are doing integration testing in my opinion. If you want to have proper unit tests, they should not operate against the database of the underlying app. Instead create an extra (in memory) database and inject that into the code you are testing. I think this is the way you should follow. The benefits are
Your app is not affected by your tests. Next time you start the app to manually test, your original data is still there.
Your unit tests are not affected by manual changes to the app
Your unit tests are also independent of each other. Order of execution doesn't change the results when setting up the database fresh for each test case.
It is even possible to run test code before the host app launches
If you have to ask that, your tests are not unit tests. Unit tests test code, not the app. A test that requires the app to be running would be a UI test.
In fact, the best approach for unit tests is to put all your testable code into a framework and give the framework unit tests; that way, your tests run much faster because the app never has to launch at all.
It sounds to me like the problem lies deeper in your app's code: you have evidently not written your code in such a way as to be testable. So that would be your first move. Writing testable code, and writing unit tests, is an art; you have to separate out the "system under test", which should be your code alone, and make sure you are not testing anything that doesn't belong to you and whose workings are already known — like Core Data.

Any easier way for finding selectors, while building Appium test framework for Cordova iOS app

I am working on automating a iOS app using Appium-Java.
While writing the Page objects we need to find identifiers (driver.findelement) or when UI changes.
Currently its tedious job, as manually need to traverse through till required screen & then write the selectors( n CSS / XPath / XCUnit based Xpath)
Currently, there's no easy way to test CSS selectors / XPath (unlike web where we can test it then & there on Firefox / chrome using browser add-ons)
Is there any way to invoke certain screen directly instead of traversing from the starting point in iOS/Android native app or Cordova based app, while writing the selectors?
Kindly advise. Thanks.
For a Cordova-based iOS app, you can use wd and wd-bridge to provide a mechanism to allow Appium to communicate directly with the Webview, enabling you to use CSS selectors.
This blog post gives a great tutorial on how to setup such a test environment using Protractor/Jasmine.
You can then write Protractor tests which allow you to reference your app UI elements as HTML components, e.g.:
describe('Testing the app', function () {
it('01. should have a header', function () {
var header = element(by.id('header'));
expect(header.getAttribute('class')).toContain('toolbar');
});
});

Application running when executing iOS tests interfere Nocilla Stubs

I'm using Nocilla to stub HTTP requests and it is working ok. I can stub the requests, return json fixtures and assert over it. The problem is that the application runs when the test starts and some requests are made on the viewDidLoad of the first view controller which cause an exception on Nocilla. How I can prevent the application from running since I only want the test code to be executed?
PS: I don't want to stub all the requests because I'm testing them!
I managed to find the solution. These kind of tests should be run in logic tests target instead of application test target which uses the application. More documentation of the differences and how to create a logic test target can be found here.

Testing Web Apps Using DRT

I'm trying to use DRT for running acceptance tests.
Because it's an acceptance test I need to change the location to open the page under test. But of course, after I've done it my test script is gone.
I tried to use iFrames as a workaround, but Dart doesn't provide any means of getting the content of an iFrame. Which means that it's possible to load the page under test into an iframe, but it's impossible to get its html.
I've checked all the DRT tests in the Dart repo:
http://code.google.com/p/dart/source/browse/#svn%2Fbranches%2Fbleeding_edge%2Fdart%2Ftests%2Fhtml
but it seems that none of them changes the location.
Is it possible to use DRT for running acceptance tests? Is there a workaround I didn't think of?
We haven't come up with a good trick (redirection or iframes) to load the app as it is written and runs the test code on top of it. Instead, you could copy the entrypoint of an app and include the test code there, then run the modified app directly in DRT.
Here is an example from the web-ui codebase of a test that does this. This test runs the TodoMVC app and interacts with it:
https://github.com/dart-lang/web-ui/blob/master/test/data/input/todomvc_listorder_test.html
All we did is copy the original app's html, add the 'testing.js' script tag, and replace the dart script tag with the test code. It might be possible to create a script that automates what we do manually today, but we haven't done that.

Why is my functional test getting the meta tag http-equiv='refresh' and then quitting?

When I run a simple functional test to get (for example) the users/signIn page, I'm getting this:
<html><head><meta http-equiv="refresh" content="0;url=https://localhost/index.php/users/signIn"/></head></html>
and then the functional test just stops. It happens in other functional tests too, but not on every request. Other tests will run fine, then when it gets to a certain request in the test, it will get that response (with the requested URL in the content attribute), and stop.
Any ideas on why this might be happening?
These functional tests used to work, but I just got this project back from another development company and I don't have an idea of where to start looking for the changes. Of course I can do diffs on the files with the version control, but I don't know where to start. Thanks for any leads!
Argh, found it quicker than I thought.
The SSL filter was turned on, and needs to be disabled for the test environment. They had removed the test environment from app.yml.
test:
disable_sslfilter: true

Resources