-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#81 Add new getRecentActivity function to the activity service
- Loading branch information
1 parent
9c190e3
commit 363c148
Showing
2 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,128 @@ | ||
import { QuizService } from '../quiz/quiz.service'; | ||
import { UnhandledError } from '../util/common.errors'; | ||
import { ActivityService } from './activity.service'; | ||
|
||
const sut = new ActivityService(); | ||
const mockQuizService = { | ||
getRecentQuizUploads: jest.fn(), | ||
getRecentQuizCompletions: jest.fn(), | ||
}; | ||
|
||
const sut = new ActivityService(mockQuizService as unknown as QuizService); | ||
describe('activity', () => { | ||
describe('activity.service', () => { | ||
describe('getRecentActivity', () => { | ||
it('must work when there is only an upload', async () => { | ||
mockQuizService.getRecentQuizUploads.mockResolvedValueOnce([ | ||
{ | ||
date: new Date('2020-06-07'), | ||
uploadedAt: new Date('2021-01-01'), | ||
uploadedBy: { name: 'Grant', email: '[email protected]' }, | ||
type: 'SHARK', | ||
}, | ||
]); | ||
mockQuizService.getRecentQuizCompletions.mockResolvedValueOnce([]); | ||
|
||
const actual = await sut.getRecentActivity(5); | ||
|
||
expect(actual).toEqual([ | ||
{ | ||
date: new Date('2021-01-01'), | ||
text: 'New SHARK quiz from June 7, 2020 uploaded by Grant', | ||
}, | ||
]); | ||
}); | ||
it('must work when there is only a completion', async () => { | ||
mockQuizService.getRecentQuizUploads.mockResolvedValueOnce([]); | ||
mockQuizService.getRecentQuizCompletions.mockResolvedValueOnce([ | ||
{ | ||
quizDate: new Date('2020-03-23'), | ||
quizType: 'SHARK', | ||
score: 12, | ||
completedBy: [{ name: 'Master' }, { name: 'Beginner' }, { email: '[email protected]' }], | ||
completionDate: new Date('2021-01-01'), | ||
}, | ||
]); | ||
|
||
const actual = await sut.getRecentActivity(5); | ||
|
||
expect(actual).toEqual([ | ||
{ | ||
date: new Date('2021-01-01'), | ||
text: 'Master, Beginner & [email protected] scored 12 on the SHARK quiz from March 23, 2020', | ||
}, | ||
]); | ||
}); | ||
it('must call quizService.getRecentQuizUploads and quizService.getRecentQuizCompletions and combine the results', async () => { | ||
mockQuizService.getRecentQuizUploads.mockResolvedValueOnce([ | ||
{ | ||
date: new Date('2020-06-07'), | ||
uploadedAt: new Date('2021-01-21'), | ||
uploadedBy: { name: 'Bob', email: '[email protected]' }, | ||
type: 'BRAINWAVES', | ||
}, | ||
{ | ||
date: new Date('2020-08-08'), | ||
uploadedAt: new Date('2021-01-11'), | ||
uploadedBy: { name: 'Tracey', email: '[email protected]' }, | ||
type: 'SHARK', | ||
}, | ||
{ | ||
date: new Date('2020-10-11'), | ||
uploadedAt: new Date('2021-01-02'), | ||
uploadedBy: { name: 'Grant', email: '[email protected]' }, | ||
type: 'SHARK', | ||
}, | ||
]); | ||
mockQuizService.getRecentQuizCompletions.mockResolvedValueOnce([ | ||
{ | ||
quizDate: new Date('2020-03-23'), | ||
quizType: 'BRAINWAVES', | ||
score: 19, | ||
completedBy: [{ name: 'Chloe' }], | ||
completionDate: new Date('2021-01-31'), | ||
}, | ||
{ | ||
quizDate: new Date('2020-03-23'), | ||
quizType: 'SHARK', | ||
score: 12, | ||
completedBy: [{ name: 'Daniel' }], | ||
completionDate: new Date('2021-01-05'), | ||
}, | ||
{ | ||
quizDate: new Date('2020-03-23'), | ||
quizType: 'SHARK', | ||
score: 9, | ||
completedBy: [{ name: 'Master' }, { name: 'Beginner' }, { email: '[email protected]' }], | ||
completionDate: new Date('2021-01-01'), | ||
}, | ||
]); | ||
|
||
const actual = await sut.getRecentActivity(5); | ||
|
||
expect(actual).toEqual([ | ||
{ | ||
date: new Date('2021-01-31'), | ||
text: 'Chloe scored 19 on the BRAINWAVES quiz from March 23, 2020', | ||
}, | ||
{ | ||
date: new Date('2021-01-21'), | ||
text: 'New BRAINWAVES quiz from June 7, 2020 uploaded by Bob', | ||
}, | ||
{ | ||
date: new Date('2021-01-11'), | ||
text: 'New SHARK quiz from August 8, 2020 uploaded by Tracey', | ||
}, | ||
{ | ||
date: new Date('2021-01-05'), | ||
text: 'Daniel scored 12 on the SHARK quiz from March 23, 2020', | ||
}, | ||
{ | ||
date: new Date('2021-01-02'), | ||
text: 'New SHARK quiz from October 11, 2020 uploaded by Grant', | ||
}, | ||
]); | ||
}); | ||
}); | ||
describe('userListToString', () => { | ||
it('must throw an error if the user list is empty', () => { | ||
expect(() => sut.userListToString([])).toThrow(UnhandledError); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters