Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix order in which notification updates are commited to state #11374

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { NotificationObjects } from '../../../constants/notificationsConstants';
import { updateWithNotifications } from '../actions';

const { QUIZ } = NotificationObjects;

describe('classSummary/actions', () => {
describe('updateWithNotifications', () => {
it('commits quiz notifications updates in a correct order (sorted by their datetime)', () => {
const state = {
learnerMap: { 'user-id': {} },
examMap: { 'quiz-id': {} },
contentNodeMap: {},
lessonMap: {},
};
const commit = jest.fn();
const dispatch = jest.fn();

const quizNotifications = [
{
id: 1,
user_id: 'user-id',
object: QUIZ,
event: 'Completed',
timestamp: '2023-10-05T13:35:00+02:00',
quiz_num_correct: 1,
quiz_num_answered: 2,
quiz_id: 'quiz-id',
},
{
id: 2,
user_id: 'user-id',
object: QUIZ,
event: 'Started',
timestamp: '2023-10-05T13:30:00+02:00',
quiz_num_correct: 2,
quiz_num_answered: 3,
quiz_id: 'quiz-id',
},
];

updateWithNotifications({ state, commit, dispatch }, quizNotifications);

expect(commit).toHaveBeenCalledTimes(1);
expect(commit.mock.calls[0][0]).toBe('APPLY_NOTIFICATION_UPDATES');
expect(commit.mock.calls[0][1].examLearnerStatusMapUpdates).toEqual([
{
learner_id: 'user-id',
status: 'Started',
last_activity: new Date('2023-10-05T11:30:00.000Z'),
num_correct: 2,
num_answered: 3,
exam_id: 'quiz-id',
},
{
learner_id: 'user-id',
status: 'Completed',
last_activity: new Date('2023-10-05T11:35:00.000Z'),
num_correct: 1,
num_answered: 2,
exam_id: 'quiz-id',
},
]);
});
});
});
11 changes: 9 additions & 2 deletions kolibri/plugins/coach/assets/src/modules/classSummary/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ export function updateWithNotifications(store, notifications) {
const contentLearnerStatusMapUpdates = [];
const { learnerMap, examMap, contentNodeMap, lessonMap } = store.state;

for (const nIdx in notifications) {
const notification = notifications[nIdx];
if (!notifications || !notifications.length) {
return;
}
const sortedNotifications = notifications.sort((n1, n2) => {
return new Date(n1.timestamp) - new Date(n2.timestamp);
});

for (const nIdx in sortedNotifications) {
const notification = sortedNotifications[nIdx];
const { object } = notification;

// Short-circuit the update if there are missing learners, exams, lessons,
Expand Down