Skip to content

Commit

Permalink
Merge branch 'master' into dep_updates
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles authored Jan 11, 2019
2 parents 42bff23 + 02ef730 commit d7b0ee7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"coveralls": "^3.0.2",
"mocha": "^5.2.0",
"mockery": "^1.4.0",
"nyc": "^11.0.2",
"nyc": "^13.0.0",
"semantic-release": "^15.9.3",
"sinon": "~1.17.0",
"sinon-chai": "^2.8.0",
Expand Down
30 changes: 30 additions & 0 deletions src/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ const EventEmitter = require('events').EventEmitter

const User = require('./user')

// If necessary, reconstructs a User object. Returns either:
//
// 1. If the original object was falsy, null
// 2. If the original object was a User object, the original object
// 3. If the original object was a plain JavaScript object, return
// a User object with all of the original object's properties.
let reconstructUserIfNecessary = function (user) {
if (!user) {
return null
}

if (!user.constructor || (user.constructor && user.constructor.name !== 'User')) {
let id = user.id
delete user.id
// Use the old user as the "options" object,
// populating the new user with its values.
return new User(id, user)
} else {
return user
}
}

class Brain extends EventEmitter {
// Represents somewhat persistent storage for the robot. Extend this.
//
Expand Down Expand Up @@ -116,6 +138,14 @@ class Brain extends EventEmitter {
this.data[k] = data[k]
}

// Ensure users in the brain are still User objects.
if (data && data.users) {
for (let k in data.users) {
let user = this.data.users[k]
this.data.users[k] = reconstructUserIfNecessary(user)
}
}

this.emit('loaded', this.data)
}

Expand Down
19 changes: 19 additions & 0 deletions test/brain_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ describe('Brain', function () {
this.brain.mergeData({})
expect(this.brain.emit).to.have.been.calledWith('loaded', this.brain.data)
})

it('coerces loaded data into User objects', function () {
this.brain.mergeData({users: {'4': {'name': 'new', 'id': '4'}}})
let user = this.brain.userForId('4')
expect(user.constructor.name).to.equal('User')
expect(user.id).to.equal('4')
expect(user.name).to.equal('new')
})
})

describe('#save', () => it('emits a save event', function () {
Expand Down Expand Up @@ -308,5 +316,16 @@ describe('Brain', function () {
expect(result).to.have.members([this.user1, this.user2])
expect(result).to.not.have.members([this.user3])
})

it('returns User objects, not POJOs', function () {
expect(this.brain.userForId('1').constructor.name).to.equal('User')
for (let user of this.brain.usersForFuzzyName('Guy')) {
expect(user.constructor.name).to.equal('User')
}

for (let user of this.brain.usersForRawFuzzyName('Guy One')) {
expect(user.constructor.name).to.equal('User')
}
})
})
})

0 comments on commit d7b0ee7

Please sign in to comment.