fix: Centralise user deserialization

This commit is contained in:
Bouncey
2019-03-04 21:10:12 +00:00
committed by mrugesh mohapatra
parent 0c23844793
commit 72a0d63aa0
6 changed files with 132 additions and 95 deletions

View File

@@ -5,8 +5,10 @@ import sinon from 'sinon';
import {
prepUniqueDaysByHours,
calcCurrentStreak,
calcLongestStreak
calcLongestStreak,
getUserById
} from './user-stats';
import { mockUserID, mockApp, mockUser } from '../boot_tests/fixtures';
// setting now to 2016-02-03T11:00:00 (PST)
const clock = sinon.useFakeTimers(1454526000000);
@@ -577,4 +579,51 @@ describe('user stats', () => {
}
);
});
describe('getUserById', () => {
const stubUser = {
findById(id, cb) {
cb(null, { id: 123 });
}
};
it('returns a promise', () => {
expect.assertions(3);
expect(typeof getUserById('123', stubUser).then).toEqual('function');
expect(typeof getUserById('123', stubUser).catch).toEqual('function');
expect(typeof getUserById('123', stubUser).finally).toEqual('function');
});
it('resolves a user for a given id', done => {
expect.assertions(7);
return getUserById(mockUserID, mockApp.models.User)
.then(user => {
expect(user).toEqual(mockUser);
// fields added or removed by getUserById
expect(user).not.toHaveProperty('progressTimestamps');
expect(user).toHaveProperty('completedChallengeCount');
expect(user).toHaveProperty('completedProjectCount');
expect(user).toHaveProperty('completedCertCount');
expect(user).toHaveProperty('completedLegacyCertCount');
expect(user.completedChallenges).toEqual([]);
})
.then(done)
.catch(done);
});
it('throws when no user is found', done => {
const noUserError = new Error('No user found');
const throwyUserModel = {
findById(_, cb) {
cb(noUserError);
}
};
expect(
getUserById('not-a-real-id', throwyUserModel).catch(error => {
expect(error).toEqual(noUserError);
done();
})
);
});
});
});