var usersHelpers = require('../../server/db/users/usersHelpers.js');
var mongoose = require('mongoose');

// Open connection -- NOTE: must be in first file in server/
mongoose.connect('mongodb://localhost:27017');

describe('usersHelpers', function () {
  var mockUser = {
    name: 'Willy'
  }

  // Drop database before running tests in top file -- NOTE: must be in first file in server/
  beforeAll(function (done) {
    mongoose.connection.on('open', function () {
      mongoose.connection.db.dropDatabase(function (error) {
        if (error) {
          throw error;
        }

        done();
      });
    });
  });

  describe('.postUser()', function () {
    it('should be a function', function () {
      expect(typeof usersHelpers.postUser).toBe('function');
    });

    it('should store a new user in the database', function (done) {
      var postPromise = usersHelpers.postUser(mockUser);

      postPromise
        .then(function (result) {
          expect(result.name).toBe('Willy');
          done();
        });
    });

    it('should return an error if a user already exists', function (done) {
      var postPromise = usersHelpers.postUser(mockUser);

      postPromise
        .then(function (result) {
          expect(result.message).toBe('User already exists.');
          done();
        });
    });
  });

  describe('.getUser()', function () {
    it('should be a function', function () {
      expect(typeof usersHelpers.postUser).toBe('function');
    });

    it('should retreive a user from the database', function (done) {
      var getPromise = usersHelpers.getUser(mockUser);

      getPromise
        .then(function (result) {
          expect(result.name).toBe('Willy');
          done();
        });
    });

    it('should return an error if the user does not exist', function (done) {
      mockUser.name = 'Little Willy'
      var getPromise = usersHelpers.getUser(mockUser);

      getPromise
        .then(function (result) {
          expect(result.message).toBe('User does not exist.');
          done();
        });
    });
  });
});

@@ -66,562 +66,88 @@
 ');%0A
-var mongoose = require('mongoose');%0A%0A// Open connection -- NOTE: must be in first file in server/%0Amongoose.connect('mongodb://localhost:27017');%0A%0Adescribe('usersHelpers', function () %7B%0A  var mockUser = %7B%0A    name: 'Willy'%0A  %7D%0A%0A  // Drop database before running tests in top file -- NOTE: must be in first file in server/%0A  beforeAll(function (done) %7B%0A    mongoose.connection.on('open', function () %7B%0A      mongoose.connection.db.dropDatabase(function (error) %7B%0A        if (error) %7B%0A          throw error;%0A        %7D%0A%0A        done();%0A      %7D);%0A    %7D);%0A  %7D)
+%0Adescribe('usersHelpers', function () %7B%0A  var mockUser = %7B%0A    name: 'Willy'%0A  %7D
 ;%0A%0A 
@@ -1384,16 +1384,17 @@
 e Willy'
+;
 %0A      v

move database connection and drop functions to menusHelpers spec