Last active
December 4, 2020 18:49
-
-
Save brooksbecton/f763c8f8be754d180c158b848b627dbc to your computer and use it in GitHub Desktop.
Mocking Meteor User Account for Unit Tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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