How to past value from get method into custom method - cypress

I am finding the way how to get value from get method into custom command.
Code shoud look like this:
cy.get('#alias').customCommand(name)
I know i can send it throw parameter like cy.customCommand('#alias', name), but i would like to use chain and in customCommand get value from previous chained command.
Is it posible?

I figured out. Look at https://code.i-harness.com/en/docs/cypress/api/cypress-api/custom-commands
by this, you have to add into your custom command option {prevSubject: true} and then method get will past value into first parameter of your custom command.
So:
support/commands.ts
Cypress.Commands.add('customCommand', { prevSubject: true }, (element, datatable, jqSelection) => {..})
support/index.ts
declare global {
namespace Cypress {
interface Chainable {
customCommand(datatable, jqSelection): void
}
}
}
Your step:
cy.get('#value').customCommand(datatable, jqselection)

You can do something like this:
cy.get('#alias').then((name) => {
cy.customCommand(name)
})

Related

Is there a way to check if an element has a class in cypress.io?

I don't want to test that it has the class like ...should('have.class', "some-class") I just want know if it does, and if doesn't then perform some action that gives it that class.
Basically, I want to see if the element has Mui-checked and if it doesn't them programmatically check it.
You can use the hasClass() jquery method for this:
cy.get('selector').then(($ele) => {
if ($ele.hasClass('foo')) {
//Do something when you have the class
} else {
//Do something when you don't have the class
}
})
Add the class like this. You don't need to check the condition since $el.addClass() works either way.
cy.get('selector').then($el => $el.addClass("blue"))
You can retrieve the class attribute with should() and check that it contains your class, something like:
cy.get('.selector')
.should('have.attr', 'class')
.and('contain', 'some-class');

Mocking out a function inside another function

I don't actually want 'anotherFunction' to execute.
But rather when it's called inside someFunction, to have it return a specific value rather than actually executing:
// Testing this function
export function someFunction(foo, bar): string {
// want to provide a mock result here
const baz = anotherFunction(foo, bar);
// do something unrelated
}
export function anotherFunction(quz, quux): any {
// do something unrelated
}
How would you go about this with jasmine? The examples I find all assume a class and then use:
// Can't use this as the method I'd like to "mock out" is not in a class
const spy = spyOn(someClass, 'aMethod');
I'm looking for something similar to the mock function in Jest. That documentation helps communicate my question better:
"Mock functions allow you to test the links between code by erasing the actual implementation of a function, ..."
But then something similar in Jasmine.
Try something like this:
import * as helpers from './file/where/anotherFunction/is';
...
spyOn(helpers, 'anotherFunction');
Check this link out.

Route to two methods of controller with one row in Laravel 4

It is simple to route this way:
Route::get('user/profile', 'PaymentsController#profile');
Route::get('user/delete', 'PaymentsController#delete');
I want to do this with one row:
Route::get('user/{subsection}', 'PaymentsController#'.$subsection);
but my syntax seems to be wrong. Is it possible to be done with one row? It would be nice if it is possible.
No, you can't do that, but you can make proxy method
Route::get('user/{subsection}', 'PaymentsController#profileDelete');
And method will looks like
public function profileDelete($subsection) {
return $this->$subsection();
}
public function profile(){}
public function delete(){}
Also, you can bind {subsection}
Route::bind('subsection', function ($subsection) {
if (!in_array($subsection, ['profile', 'delete'])) {
throw new Exception;
}
return $subsection;
});
Not exactly one row, but you can do this:
Route::get('user/{subsection}', function($subsection){
if(!method_exists('PaymentsController', $subsection)){
App::abort(404);
}
return App::make('PaymentsController')->callAction($subsection);
});
Alternatively to the method_exists you could also use a route condition to only allow a predefined set of subsections:
Route::get('user/{subsection}', function($subsection){
return App::make('PaymentsController')->callAction($subsection);
})->where('subsection', '(profile|delete)');

how to use Route::input in laravel4?

I am trying to use Laravel 4 method called Route:input("users"). But I am getting following error
Call to undefined method Illuminate\Routing\Router::input()
Any idea how Route::input() works. Is there any file I need to change.
Thanks all
Route::filter('userFilter', function () {
if (Route::input('name') == 'John') {
return 'Welcome John.';
}
});
Route::get('user/{name}', array(
'before' => 'userFilter',
function ($name) {
return 'Hello, you are not John.';
}));
It looks as though Route::input was added in Laravel 4.1, make sure this is the version you are working with if you need to use this functionality.
I assume you've read the docs, but since you asked how it works, here's the example:
Accessing A Route Parameter Value
If you need to access a route parameter value outside of a route, you may use the Route::input method:
Route::filter('foo', function()
{
// Do something with Route::input('users');
});

How to enforce Grails command objects have been validated?

We use the following general pattern with Grails controllers and command objects
SomeController {
def someAction() {
SomeCommandObject co = SomeCommandObject.valueOf(params)
if(!co.validate()) {
// return bad request
}
someService.doWork(co)
// return ok
}
SomeService {
def doWork(SomeCommandObject co) {
notTrue(!co.hasErrors(), 'cant have errors') // Commons validation
// do actual work
}
}
Apparently, if co.validate() has not been called, .hasErrors() will always return false. Is there a better way to enforce that .validate() has been called before a command object is passed between application layers? We don't want to pass around invalid command objects but we don't want to force every new method to re-validate the command object either.
Note: We aren't using the default controller/command object creation pattern because we need to do some custom parameter map checking, we use a static valueOf method instead to create the command object. Answers that change that practice are also welcome.
EDIT: A little more info on why we aren't using the 'default' controller/command object creation. Specifically why we aren't doing ..
def someAction(SomeCommandObject co) {
}
We have a requirement to disallow random query parameters, eg. endpoint/object?color=blue. To do that we need access to the parameter map in the command object to verify that it doesn't contain any 'unexpected' parameter keys. As I understand it, the default way would just create a member on the CO named color, and I don't see how to prevent arbitrary members using even custom validators. I'd happily entertain suggestions for doing so, thereby allowing us to use this default means.
Yes; what you can do is pass the command object as a parameter to the controller, and then the command will always be validated automatically.
Also, what you can do, is to make a filter or similar, so that you don't have to check for the hasErrors() each time, but handle all the cases in the same way (for example, by throwing an error, or returning with a specific response).
In an application we created, we had something like:
withValidCommand(cmd) {
// do work
}
Which worked pretty well. But maybe you can come up something even more elegant.
You should be doing this:
def someAction(SomeCommandObject co) {
if (!co.hasErrors()) {
someService.doWork(co)
}
}
By passing SomeCommandObject in as the argument grails will automatically populate it from params and validate. No need to do it manually.

Resources