const mongoose = require( 'mongoose' );
const mockgoose = require( 'mockgoose' );
const chai = require( 'chai' );
const chaiAsPromised = require( 'chai-as-promised' );
const locations = require( '../app/routes/locations' );

chai.use( chaiAsPromised );
const expect = chai.expect;

mockgoose( mongoose );
const Location = require( '../app/models/location' ).default;
const LocationCollection = require( '../app/models/location-collection' ).default;

const testUserId = 'testUser';
const homeLocation = new Location( { userId: testUserId, name: 'foo', address: 'bar' } );
const testUserLocationCollection = new LocationCollection( { userId: testUserId, locations: [ homeLocation ] } );

function populateDb( done ) {
  homeLocation.save()
  .then( testUserLocationCollection.save )
  .then( () => done() )
  .then( null, ( err ) => done( err ) );
}

describe( 'locations', function() {
  before( function( done ) {
    mongoose.connect( 'mongodb://example.localhost/TestingDB', function( err ) {
      if ( err ) return done( err );
      mockgoose.reset( () => populateDb( done ) );
    } );
  } );

  afterEach( function() {
  } );

  describe( '.listLocationsForUser', function() {
    it( 'returns an Array', function() {
      return expect( locations.listLocationsForUser( testUserId ) ).to.eventually.be.an.instanceOf( Array );
    } );

    it( 'returns an array with all current Locations', function( done ) {
      locations.listLocationsForUser( testUserId )
      .then( function( data ) {
        if ( data.length === 1 && data[0].name === homeLocation.name && data[0].address === homeLocation.address ) return done();
        done( 'location not found' );
      } )
      //return expect( locations.listLocationsForUser( testUserId ) ).to.eventually.have.property( 'address', homeLocation.address );
    } );

    it( 'returns an array that does not include Locations from other users', function() {
      return expect( locations.listLocationsForUser( 'foobar' ) ).to.eventually.not.include( homeLocation );
    } );
  } );
} );

@@ -485,276 +485,677 @@
 nst 
-homeLocation = new Location( %7B userId: testUserId, name: 'foo', address: 'bar' %7D );%0Aconst testUserLocationCollection = new LocationCollection( %7B userId: testUserId, locations: %5B homeLocation %5D %7D );%0A%0Afunction populateDb( done ) %7B%0A  homeLocation.save()%0A  .then( testUser
+testUserId2 = 'testUser2';%0Alet homeLocation;%0Alet workLocation;%0Alet testUserLocationCollection;%0Alet testUser2LocationCollection;%0A%0Afunction populateDb( done ) %7B%0A  homeLocation = new Location( %7B userId: testUserId, name: 'foo', address: 'bar' %7D );%0A  workLocation = new Location( %7B userId: testUserId2, name: 'work', address: 'place' %7D );%0A  testUserLocationCollection = new LocationCollection( %7B userId: testUserId, locations: %5B homeLocation %5D %7D );%0A  testUser2LocationCollection = new LocationCollection( %7B userId: testUserId2, locations: %5B workLocation %5D %7D );%0A  homeLocation.save()%0A  .then( workLocation.save )%0A  .then( testUserLocationCollection.save )%0A  .then( testUser2
 Loca
@@ -1425,24 +1425,86 @@
 e( err );%0A  
+    done();%0A    %7D );%0A  %7D );%0A%0A  beforeEach( function( done ) %7B%0A
     mockgoos
@@ -1544,51 +1544,8 @@
  );%0A
-    %7D );%0A  %7D );%0A%0A  afterEach( function() %7B%0A
   %7D 
@@ -2059,17 +2059,17 @@
   done( 
-'
+%60
 location
@@ -2082,153 +2082,49 @@
 ound
-' );%0A      %7D )%0A      //return expect( locations.listLocationsForUser( testUserId ) ).to.eventually.have.property( 'address', homeLocation.address
+ in $%7BJSON.stringify( data )%7D%60 );%0A      %7D
  );%0A
@@ -2211,32 +2211,38 @@
 sers', function(
+ done 
 ) %7B%0A      return
@@ -2230,39 +2230,24 @@
 ne ) %7B%0A     
- return expect(
  locations.l
@@ -2271,60 +2271,272 @@
 er( 
-'foobar' ) ).to.eventually.not.include( homeLocation
+testUserId2 )%0A      .then( function( data ) %7B%0A        if ( data.length === 1 && data%5B0%5D.name !== homeLocation.name && data%5B0%5D.address !== homeLocation.address ) return done();%0A        done( %60data did not match expected data in $%7BJSON.stringify( data )%7D%60 );%0A      %7D
  );%0A

Clean up initial tests