Removing an intercept in Cypress - integration-testing

I have an intercept that serves up a stubbed json response like this:
cy.intercept('GET', '**/api/v1/myroute/*', { fixture: 'myData.json' }).as('myAlias')
Is there a way I can remove this intercept halfway through a test somehow? I was hoping to delete the alias so the xhr request doesn't get intercepted at all. Thanks!

OK, figured this one out. Simply do this:
cy.intercept('GET', '**/api/v1/myroute/*', (req) => {
req.continue()
});

You can try to use RouteMatcher's times option like this:
cy.intercept({
method: 'GET',
pathname: '/api/v1/myroute/*'
times: 1
}, { fixture: 'myData.json' }).as('myAlias')
so when it is called a second time it won't be intercepted

Related

How to pass route param while using Inertia manual visit

How can I pass the route param while using Inertia manual visit, for example:
Route:
Route::post('/explore/gallery/like/{$post}', [ExploreController::class, 'likeToggle'])
->name('explore.post.like');
Component:
Inertia.visit(route('explore.post.like'),
{
method: 'post',
preserveScroll: true,
data: {
$post: this.id
},
},
);
but is shows the error tho,
Error occur in Ziggy, and says you need pass $post,
Ok, Change route('explore.post.like') to route('explore.post.like', this.id)
According Ziggy documentation you can also use like :
route('explore.post.like', this.id)
route('explore.post.like', [this.id])
route('explore.post.like', { $post: this.id })
A simple solution would be:
Inertia.post(route('explore.post.like', [this.id, 'if you have other params']), {}, {
preserveScroll: true,
});
Pls make sure the order is correct, follow the order of your route's param

Cypress redirect to another url on click of logout is timing out

Here is the scenario:
I'm logging to my application by visiting app.domain/login (example) which will redirect me to something like another.app.domain/
This is working fine. But when I logout: cy.contains('logout').click(), I am getting
CypressError: Timed out after waiting '60000ms' for your remote page to load.
Any suggestions to get around this issue? Ps: I just started to learn Cypress and I want to logout mainly because I want to restore the state back.. I don't want my environment to be updated/modified with the automation scripts. Thanks in advance.
First of all, in cypress there is no AfterAll hook, so we have to use more handmade solution. Second think: if you want to use cypress instead of curl to clean up after test is ok (but I recommend us curl ;)).
First you need to add line to scripts section in package.json:
{
"scripts": {
"after-cypress": "cypress run --spec cypress/hooks/after-all.js",
}
}
File cypress/hooks/after-all.js should looks like this:
describe('clean up after test', () => {
before(() => {
cy.login() // Should we save auth token here?
});
it('', () => {
cy
.request()
.request()
.request() // each of request should delete created during test data
});
});

How to call external rest api using metero HTTP module

I am currently new to Meteor and Angular2 and I built an application with same.
I want to make External API call inside Meteor Server using Meteor HTTP Module.
Could you please give an example code in "Typescript" how to do this? Thanks in advance.
Use HTTP.call() for external invoking API. You can call both Get and post requests with this. See the documentation link for details.
Here is a simple example
HTTP.call('POST', 'http://api.twitter.com/xyz', {
data: { some: 'json', stuff: 1 }
}, () => (error, result) {
if (!error) {
Session.set('twizzled', true);
}
});

angular 2 - asynchrone issue

I have a very simple Typescript script (ionic2 and angular2) that add an authentication header before an HTTP call. Here is the idea (simplified code):
function CreateAuthorization(){
this.header.append('tests' : 'test')
Storage.retrieve('Auth').then(data){
this.header.append('authorization' : data.token)
}
}
function customHttp(url){
CreateAuthorization();
Http.get(url, this.header);
}
In my Request header, I have 'test' = 'test' but I do NOT have 'authorization' = 'MyToken'.
How can I make in sort to "wait" for the header to be set in Storage.retrieve('Auth') ?
I know that I can use a setTimeout() but I don't like this dirty workaround.
The solution could be an observable/promise but I don't really master those things.
Any help would be very appreciated :)
Geoffrey
It's because your CreateAuthorization method is asynchronous. I would try something like that leveraging promise chaining to be notified when the Authorization header is actually added:
createAuthorization() {
this.header.append('tests' : 'test');
return Storage.retrieve('Auth').then(data){
this.header.append('authorization', data.token);
return true;
}
}
customHttp(url) {
this.createAuthorization().then(() => {
this.http.get(url, { headers: this.header });
});
}

Iron Router: How to Provide Same Data to Multiple Routes

It's evident how to provide route-specific data, i.e. through the use of a controller:
PostController = RouteController.extend({
layoutTemplate: 'PostLayout',
template: 'Post',
waitOn: function () { return Meteor.subscribe('post', this.params._id); },
data: function () { return Posts.findOne({_id: this.params._id}) },
action: function () {
this.render();
}
});
But how does one provide data for the application in general? In the case that every route needs to be subscribed to the same subset of information, so that the pub/sub doesn't need to be re-done on every route change. Thanks!
It sounds to me like you are looking for a completely general publication/subscription scheme so that you do not have to define the waitOn/data option combination for every single route or route controller that you define. In that case, you can simply publish a given set of data on the server like so:
Meteor.publish('someData', function() {
return SomeDataCollection.find({});
});
and subscribe to that set of data on the client like so:
Meteor.subscribe('someData');
With this publication/subscription pair setup, you will have access to the data provided in all routes. You just have to make sure that you check for non-existent data in your code to handle the first load of any given template when the data has not been loaded on the client yet. In this manner, you would never have to actually define a the waitOn and data options for any route or route controller.
If you would like to utilize Iron Router in a different way than through route controllers, you also have the option of waiting on one/many subscriptions globally by using the Router.configure({}); function. To use the example above:
Router.configure({
waitOn: function() {
return Meteor.subscribe('someData');
}
});
For information about this route option and all of the other options that you have available at a global level, check this out.

Resources