Last active
September 20, 2018 15:59
-
-
Save MarceloPrado/80996c9843e97638c4c1f2e322f553f7 to your computer and use it in GitHub Desktop.
This is an example showing how to test a server-side meteor method.
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
/* eslint-env mocha */ | |
/* eslint-disable func-names, prefer-arrow-callback */ | |
import { Meteor } from 'meteor/meteor'; | |
import { Accounts } from 'meteor/accounts-base'; | |
import { assert } from 'meteor/practicalmeteor:chai'; | |
import { resetDatabase } from 'meteor/xolvio:cleaner'; | |
import { Random } from 'meteor/random'; | |
import { Factory } from 'meteor/dburles:factory'; | |
import './methods.js'; // import all the methods that will be tested | |
import Appointments from './appointments.js'; | |
if (Meteor.isServer) { | |
const delay = milisecs => new Promise(res => setTimeout(() => res(), milisecs)); | |
describe('Appointments', () => { | |
describe('methods', () => { | |
const isDefined = target => { | |
assert.isNotNull(target, 'unexpected null value'); | |
assert.isDefined(target, 'unexpected undefined value'); | |
if (typeof target === 'string') { | |
assert.notEqual(target.trim(), ''); | |
} | |
}; | |
let meteorUserOriginal = null; | |
let userId = null; | |
let companyId = null; | |
let thisContext = null; | |
beforeEach(() => { | |
resetDatabase(); | |
meteorUserOriginal = Meteor.user; | |
userId = Accounts.createUser({ username: Random.id() }); | |
companyId = Random.id(5); | |
isDefined(userId); | |
thisContext = { userId }; | |
Meteor.user = () => { | |
const users = Meteor.users.find({ _id: userId }).fetch(); | |
if (!users || users.length > 1) { | |
throw new Error('Meteor.user() mock cannot find by UserId'); | |
} | |
users[0].companyId = companyId; | |
return users[0]; | |
}; | |
}); | |
afterEach(() => { | |
Meteor.users.remove(userId); | |
Meteor.user = meteorUserOriginal; | |
userId = null; | |
Appointments.remove(); | |
}); | |
it('can insert appointment', function() { | |
const appointment = { | |
scheduled_services: [Random.id(10), Random.id(10), Random.id(10)], | |
type: 'scheduled', | |
professional_id: Random.id(10), | |
client_id: Random.id(10), | |
companyId: Random.id(10), | |
date_info: { | |
duration: 30, | |
start: 900, | |
day: new Date(), | |
}, | |
created_at: new Date(), | |
updated_at: new Date(), | |
}; | |
const insertAppointment = Meteor.server.method_handlers['Appointments.insert']; | |
insertAppointment.apply(thisContext, [appointment]); | |
const getAppointment = Appointments.findOne(appointment); | |
assert.strictEqual(getAppointment.scheduled_services[0], appointment.scheduled_services[0]); | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment