are we able to import and export functions in cypress.io? - function

there are few functions I want to use across the testing integrations in cypress.io is there a way to export / import the functions so I don't have to copy and paste the functions into each integration?
Thanks in advance for any advice

This might be useful for you.
https://docs.cypress.io/api/cypress-api/custom-commands.html#
You can define custom cypress commands and use them in your tests. ie cypress.login, cypress.clickHamburger, cypress.doSomethingCrazy

Yes. You can do it as you would do it in usual js code.
myFunction.js
export function funcName(param) {
return "Cypress is "+param;
}
myCypressTest.js
import { funcName } from "./myFunction.js";
funcName("great");

Yes, you can use import in your spec file.
For example, if you want to import a function add() from a Model.js, you can do something like:
import Model from '../../Model';
var model = new Model();
And call model.add() from your expect.

Related

Got TypeError: expect(...).toBeInTheDocument is not a function even after proper setup

I use Create React App and already declare this on src/setupTests.js:
import '#testing-library/jest-dom';
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
But every time I use expect(anything).toBeInTheDocument() on test file, when running the test I get:
TypeError: expect(...).toBeInTheDocument is not a function
To make sure that the setupTests.js is actually run, I try to use enzyme shallow on test file and it works. So what is the problem with jest-dom actually and how to solve it?
Solved with:
import '#testing-library/jest-dom/extend-expect';
on src/setupTests.js
It is easier to add in your jest.config.js
module.exports = {
...,
"setupFilesAfterEnv": [
"<rootDir>/jest.setup.js"
]
}
and to create jest.setup.js with the content
import '#testing-library/jest-dom'
With that you don't have to import the jest-dom in every test file

Exporting data from function to import to another class React Native

I have this function in my app.js file:
codePush.getUpdateMetadata().then(update => {
if (update) {
Sentry.setVersion(update.appVersion + "-codepush:" + update.label);
// update.label
}
});
I want to export the value of update.label as a string and call it in another class so I can display its value on a screen. I'm new to React Native and unsure of how to do this. How do I pass it out of the function? and How to I pass it out of the class so I can import into another?
Any help is much appreciated
You should save the label in AsyncStorage and when and where you want to show it, just retrieve it there from the AsyncStorage. You can refer this to get to know about the AsyncStorage in React Native.

Pass JSON data from App Component to another component in Angular 6

I have two components,
1. App Component
2. Main Component
app.component.ts
ngOnInit () {
this.httpService.get('./assets/batch_json_data.json').subscribe(data => {
this.batchJson = data as string [];
}
I am able to get the JSON from a file into 'batchJson' and need to pass this to my main component for further operations.
There is no event or anything that triggers this.
I have not implemented anything yet, I am trying to read #Input, #Output etc but do not understand how it works and need to go through it some more.
I have just declared basics in the main.component.ts
import { Component, OnInit, ViewChild, Input } from '#angular/core';
import { AppComponent } from '../app.component';
export class MainComponent implements OnInit {
}
Please help me out, I am an absolute rookie in Angular and am unable to try anything because my concepts are not clear and I did browse Stack Overflow, the answers are not matching my requirements.
One solution could be to use a public BehaviorSubject in your app.component.ts.
public batchJson$ = new BehaviorSubject<JSON>(null);
ngOnInit () {
this.httpService.get('./assets/batch_json_data.json').subscribe(data => {
this.batchJson = data as string [];
this.batchJson$.next(JSON.parse(data));
}
Then in your main.component.ts
constructor(private _appComponent : AppComponent )
ngOnInit(){
this._appComponent.batchJson$.subscribe((data)=>{
if(data != null){
//set data to local variable here
}
})
}
Typically I store this kind of logic in a Service, using this in a component will definitely get you pointed in the right direction to learning this concept. Preferably your component should be responsible for interacting with the UI and rendering data, while your services handle retrieving and distributing data.
you can implement common service which does all related http operations and you can inject this service in any component u want and read the json.
Make sure you return the http.get and you subscribe to it where ever you call this method.
If you are not aware of services , you can read about creating and injecting services in angular
You can use rxjs subject to emit the data through out the app and fetch it anywhere by using subject.getValue() method.
First of all you should spare time on understanding the concept of any technology before you start working on it. Else you would be spending most of the time seeking help.
I had created demo here - https://stackblitz.com/edit/angular-lko7pa. I hope it will help you out.

Angular 2 functions library

In my application I'll need to create a functions library to be accessed from different components. What is the best way to do this? Do I need to make a service with all the functions or a class or a module or a component?
In this library if I call a function from a component and this function need to access a variable in the caller, will it be possible to do?
If it's just a set of one off helper functions the easiest way is create an #Injectable service with your helper methods then inject that into any component that requires them. You can provide public vars as well so they're accessible in your html.
#Injectable()
export class Helpers {
helper1() { return "hello"; }
helper2(x) { return x + 1 }
}
#Component({
providers: [Helpers],
template: "<div>{{helper1()}}</div>" // will display 'hello'
)}
export class MyComponent {
public helper1: Function = Helpers.helper1;
constructor() {
console.log(Helpers.helper2(5)); // will output '6'
}
}
This works great for a simple set of random utility functions. If you need something more in depth please explain and we can provide alternate solutions.

What is the "hasOwnProperty()" equivalent for interface?

What is the "hasOwnProperty()" equivalent for interface?
I have found this related bug at Adobe: https://bugs.adobe.com/jira/browse/FB-27683
Any workaround except try..catch statement?
Have you considered this?
if("foo" in bar){ ...
where "foo" is the name of a property and bar is the object reference as Interface?
Here it is in action in a real world scenario:
import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
var i:IEventDispatcher = new EventDispatcher();
if("dispatchEvent" in i){
trace(" I have dispatchEvent");
}
The other answer is better, but you can also use
i['hasOwnProperty']('dispatchEvent')