| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /* eslint-disable
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon')
- const chai = require('chai')
- const should = chai.should()
- const { expect } = chai
- const modulePath = '../../../app/js/ContactManager.js'
- const SandboxedModule = require('sandboxed-module')
- const { ObjectId } = require('mongodb')
- const tk = require('timekeeper')
- describe('ContactManager', function () {
- beforeEach(function () {
- tk.freeze(Date.now())
- this.ContactManager = SandboxedModule.require(modulePath, {
- requires: {
- './mongodb': {
- db: (this.db = { contacts: {} }),
- ObjectId,
- },
- },
- })
- this.user_id = ObjectId().toString()
- this.contact_id = ObjectId().toString()
- return (this.callback = sinon.stub())
- })
- afterEach(function () {
- return tk.reset()
- })
- describe('touchContact', function () {
- beforeEach(function () {
- this.db.contacts.updateOne = sinon.stub().callsArg(3)
- })
- describe('with a valid user_id', function () {
- beforeEach(function () {
- return this.ContactManager.touchContact(
- this.user_id,
- (this.contact_id = 'mock_contact'),
- this.callback
- )
- })
- it('should increment the contact count and timestamp', function () {
- this.db.contacts.updateOne
- .calledWith(
- {
- user_id: sinon.match(
- o => o.toString() === this.user_id.toString()
- ),
- },
- {
- $inc: {
- 'contacts.mock_contact.n': 1,
- },
- $set: {
- 'contacts.mock_contact.ts': new Date(),
- },
- },
- {
- upsert: true,
- }
- )
- .should.equal(true)
- })
- return it('should call the callback', function () {
- return this.callback.called.should.equal(true)
- })
- })
- return describe('with an invalid user id', function () {
- beforeEach(function () {
- return this.ContactManager.touchContact(
- 'not-valid-object-id',
- this.contact_id,
- this.callback
- )
- })
- return it('should call the callback with an error', function () {
- return this.callback.calledWith(sinon.match(Error)).should.equal(true)
- })
- })
- })
- return describe('getContacts', function () {
- beforeEach(function () {
- this.user = {
- contacts: ['mock', 'contacts'],
- }
- return (this.db.contacts.findOne = sinon
- .stub()
- .callsArgWith(1, null, this.user))
- })
- describe('with a valid user_id', function () {
- beforeEach(function () {
- return this.ContactManager.getContacts(this.user_id, this.callback)
- })
- it("should find the user's contacts", function () {
- return this.db.contacts.findOne
- .calledWith({
- user_id: sinon.match(o => o.toString() === this.user_id.toString()),
- })
- .should.equal(true)
- })
- return it('should call the callback with the contacts', function () {
- return this.callback
- .calledWith(null, this.user.contacts)
- .should.equal(true)
- })
- })
- return describe('with an invalid user id', function () {
- beforeEach(function () {
- return this.ContactManager.getContacts(
- 'not-valid-object-id',
- this.callback
- )
- })
- return it('should call the callback with an error', function () {
- return this.callback.calledWith(sinon.match(Error)).should.equal(true)
- })
- })
- })
- })
|