Skip to content

Commit a6201ad

Browse files
committed
feat(Brain): ensure User objects are actually User objects
1 parent f9b18be commit a6201ad

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/brain.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ const EventEmitter = require('events').EventEmitter
44

55
const User = require('./user')
66

7+
// If necessary, reconstructs a User object. Returns either:
8+
//
9+
// 1. If the original object was falsy, null
10+
// 2. If the original object was a User object, the original object
11+
// 3. If the original object was a plain JavaScript object, return
12+
// a User object with all of the original object's properties.
13+
let reconstructUserIfNecessary = function (user) {
14+
if (!user) {
15+
return null
16+
}
17+
18+
if (!user.constructor || (user.constructor && user.constructor.name !== 'User')) {
19+
let id = user.id
20+
delete user.id
21+
// Use the old user as the "options" object,
22+
// populating the new user with its values.
23+
return new User(id, user)
24+
} else {
25+
return user
26+
}
27+
}
28+
729
class Brain extends EventEmitter {
830
// Represents somewhat persistent storage for the robot. Extend this.
931
//
@@ -116,6 +138,14 @@ class Brain extends EventEmitter {
116138
this.data[k] = data[k]
117139
}
118140

141+
// Ensure users in the brain are still User objects.
142+
if (data && data.users) {
143+
for (let k in data.users) {
144+
let user = this.data.users[k]
145+
this.data.users[k] = reconstructUserIfNecessary(user)
146+
}
147+
}
148+
119149
this.emit('loaded', this.data)
120150
}
121151

test/brain_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ describe('Brain', function () {
5858
this.brain.mergeData({})
5959
expect(this.brain.emit).to.have.been.calledWith('loaded', this.brain.data)
6060
})
61+
62+
it('coerces loaded data into User objects', function () {
63+
this.brain.mergeData({users: {'4': {'name': 'new', 'id': '4'}}})
64+
let user = this.brain.userForId('4')
65+
expect(user.constructor.name).to.equal('User')
66+
expect(user.id).to.equal('4')
67+
expect(user.name).to.equal('new')
68+
})
6169
})
6270

6371
describe('#save', () => it('emits a save event', function () {
@@ -308,5 +316,16 @@ describe('Brain', function () {
308316
expect(result).to.have.members([this.user1, this.user2])
309317
expect(result).to.not.have.members([this.user3])
310318
})
319+
320+
it('returns User objects, not POJOs', function () {
321+
expect(this.brain.userForId('1').constructor.name).to.equal('User')
322+
for (let user of this.brain.usersForFuzzyName('Guy')) {
323+
expect(user.constructor.name).to.equal('User')
324+
}
325+
326+
for (let user of this.brain.usersForRawFuzzyName('Guy One')) {
327+
expect(user.constructor.name).to.equal('User')
328+
}
329+
})
311330
})
312331
})

0 commit comments

Comments
 (0)