forked from ucfopen/Obojobo
-
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.
fix notification implementation and tests
- Loading branch information
Showing
17 changed files
with
7,856 additions
and
148 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -104,5 +104,8 @@ | |
"engines": { | ||
"yarn": "^1.15.2", | ||
"node": "^18.16.0" | ||
}, | ||
"dependencies": { | ||
"yarn": "^1.22.19" | ||
} | ||
} |
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
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
60 changes: 42 additions & 18 deletions
60
packages/app/obojobo-express/__tests__/viewer_notification_state.test.js
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,41 +1,65 @@ | ||
const db = require('../server/db') | ||
const { | ||
getNotifications, | ||
getRecentNotifications | ||
getRecentNotifications, | ||
setLastLogin | ||
} = require('../server/viewer/viewer_notification_state') | ||
|
||
jest.mock('../server/db') | ||
describe('db', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
jest.resetModules() | ||
}) | ||
|
||
describe('getNotifications', () => { | ||
test('returns notifications when passed ids', async () => { | ||
test('returns notifications when passed ids', () => { | ||
const fakeNotifications = [ | ||
{ title: 'Notification 1', text: 'This is notification 1' }, | ||
{ title: 'Notification 2', text: 'This is notification 2' } | ||
] | ||
db.manyOrNone.mockResolvedValue(fakeNotifications) | ||
|
||
const result = await getNotifications([1, 2]) | ||
|
||
expect(result).toEqual(fakeNotifications) | ||
expect(db.manyOrNone).toHaveBeenCalledWith(expect.any(String), { ids: [1, 2] }) | ||
return getNotifications([1, 2]).then(result => { | ||
expect(result).toEqual(fakeNotifications) | ||
expect(db.manyOrNone).toHaveBeenCalledWith(expect.any(String), { ids: [1, 2] }) | ||
}) | ||
}) | ||
|
||
test('returns undefined when passed ids as 0', async () => { | ||
const result = await getNotifications(0) | ||
|
||
expect(result).toBeUndefined() | ||
//expect(db.manyOrNone).not.toHaveBeenCalled(1) | ||
test('returns undefined when passed ids as 0', () => { | ||
return expect(getNotifications(0)).toBeUndefined() | ||
}) | ||
}) | ||
|
||
describe('getRecentNotifications', () => { | ||
test('returns notifications created after a given date', async () => { | ||
test('returns notifications created after a given date', () => { | ||
const fakeNotifications = [{ id: 1 }, { id: 2 }] | ||
db.manyOrNone.mockResolvedValue(fakeNotifications) | ||
|
||
const result = await getRecentNotifications('2022-01-01') | ||
return getRecentNotifications('2022-01-01').then(result => { | ||
expect(result).toEqual(fakeNotifications) | ||
expect(db.manyOrNone).toHaveBeenCalledWith(expect.any(String), { date: '2022-01-01' }) | ||
}) | ||
}) | ||
|
||
test('should insert a new record if the user does not exist', () => { | ||
db.none.mockResolvedValue() | ||
|
||
const userId = 1 | ||
const today = '2023-09-13' | ||
|
||
return setLastLogin(userId, today).then(() => { | ||
expect(db.none).toHaveBeenCalledWith(expect.stringContaining('INSERT INTO users'), { | ||
userId, | ||
today | ||
}) | ||
}) | ||
}) | ||
|
||
test('should handle other errors from db.none', () => { | ||
const errorMessage = 'Database error' | ||
db.none.mockRejectedValue(new Error(errorMessage)) | ||
|
||
const userId = 1 | ||
const today = '2023-09-13' | ||
|
||
expect(result).toEqual(fakeNotifications) | ||
expect(db.manyOrNone).toHaveBeenCalledWith(expect.any(String), { date: '2022-01-01' }) | ||
return expect(setLastLogin(userId, today)).rejects.toThrow(errorMessage) | ||
}) | ||
}) |
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
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 |
---|---|---|
|
@@ -91,7 +91,6 @@ router | |
visitId: req.currentVisit.id | ||
}) | ||
}) | ||
.then(req.getNotifications()) | ||
|
||
.then(() => { | ||
logger.log( | ||
|
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
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
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
Oops, something went wrong.