Skip to content

Instantly share code, notes, and snippets.

@laradevitt
Created March 4, 2017 18:53
Show Gist options
  • Save laradevitt/0395e88896a8f95aae4a68d64d3377f9 to your computer and use it in GitHub Desktop.
Save laradevitt/0395e88896a8f95aae4a68d64d3377f9 to your computer and use it in GitHub Desktop.
Testing Meteor app with Chimp + Mocha - (1) User login and logout
describe('pages > manageVacancy', function() {
before( function(done) {
// When starting Chimp, passing the --ddp flag provides access to a global
// 'server' object that allows you to call methods via DDP.
// Since the server instance has a separate DDP connection from that between
// browser > server, we need to log in separately first. Requires that your app
// has accounts-password Meteor package. We will also call 'logout' method
// in mocha's after() hook to prevent disconnection problem.
server.call('login', privilegedUser);
done();
});
it ('log in user and redirect to homepage @watch', function(done) {
loginUser(privilegedUser);
// At this point we can access user info by using Meteor's accounts API.
var getUserId = function() {
return Meteor.userId();
};
var userId = server.execute(getUserId);
// Wait until user has been redirected to the homepage, which is the
// expected behaviour after logging in.
browser.waitUntil(function() {
var loadedUrl = browser.getUrl();
return loadedUrl === url + '/'
}, 5000, 'expect log-in to take user to homepage');
done();
});
it ('log out user and redirect to signOut page @watch', function(done) {
logoutUser();
// Wait until user has been redirected to the 'signOut' page, which is the
// expected behaviour after logging out.
browser.waitUntil(function() {
var loadedUrl = browser.getUrl();
return loadedUrl === url + '/signOut'
}, 5000, 'expect to be taken to signOut page');
done();
});
after( function() {
// When we're finished, log out of the server instance.
server.call('logout');
});
});
// Chimp 0.47.2
// Mocha 3.2.0
// Meteor 1.4.2.3
global.url = 'http://localhost:3000';
global.privilegedUser = {
'user': { 'email': '[email protected]' },
'password': 'password'
};
/**
* Log in user via the UI.
*/
global.loginUser = function(user) {
// Go to register page.
browser.url(url + '/signIn');
// Target our form elements.
var email = browser.element('[name="at-field-email"]');
var password = browser.element('[name="at-field-password"]');
// Wait for elements to be present within the DOM before acting on them.
email.waitForExist(2000);
browser.setValue('[name="at-field-email"]', user.user.email);
password.waitForExist(2000);
browser.setValue( '[name="at-field-password"]', user.password);
// Submit the form.
browser.submitForm( 'form' );
};
/**
* Log out user via the UI.
*/
global.logoutUser = function() {
// Target our logout link.
var logoutLink = browser.element('.navbar-right .dropdown ul li:last-child a');
// Wait for the element to be present in the DOM.
logoutLink.waitForExist(2000)
// Activate the menu and click the link to log out.
browser.click('.navbar-right .dropdown');
browser.click('.navbar-right .dropdown ul li:last-child a');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment