Skip to content

Instantly share code, notes, and snippets.

@brooksbecton
Last active December 4, 2020 18:49
Show Gist options
  • Save brooksbecton/f763c8f8be754d180c158b848b627dbc to your computer and use it in GitHub Desktop.
Save brooksbecton/f763c8f8be754d180c158b848b627dbc to your computer and use it in GitHub Desktop.
Mocking Meteor User Account for Unit Tests
import { roles } from './../../roles';
/**
* User's Information
*/
export const users = {
eve: {
uid: null,
email: 'eve@gmail.com',
name: 'eve',
password: 'password',
},
bob: {
uid: null,
email: 'bob@gmail.com',
name: 'eve',
password: 'password',
},
};
/**
* Creating bob and eve's user accounts and assigning their userId's to the 'users' object
*/
function createUsersAccounts() {
const eveUserId = Accounts.createUser({ email: users.eve.email, password: users.eve.password });
const bobUserId = Accounts.createUser({ email: users.bob.email, password: users.bob.password });
users.eve.uid = eveUserId;
users.bob.uid = bobUserId;
}
/**
* Creating 'maintainers' role
*/
function createMaintainerRole() {
Roles.createRole(roles.maintainers);
}
/**
* Creates two users accounts
* 'eve' will have maintainer access
*/
export function createMockUsers() {
if (Meteor.isTest) {
// Removing any previous users/roles
Meteor.roles.remove({});
Meteor.users.remove({});
createMaintainerRole();
createUsersAccounts();
Roles.addUsersToRoles(users.eve.uid, roles.maintainers);
}
}
import {
users,
createMockUsers,
} from './createMockUsers';
/**
* Snip
*/
beforeEach(() => {
createMockUsers();
});
it('inserts an order for a maintainer', () => {
insertOrder._execute({ userId: users.eve.uid }, {productIds: [1,2,3] });
const orderCount = Order.find({}).fetch().length;
assert.equal(orderCount, 1);
});
it("throws error if someone is not a 'maintainer'", () => {
assert.throws(() =>
insertOrder._execute({ userId: users.bob.uid }, {productIds: [1,2,3] }));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment