Skip to content

Commit

Permalink
Refactoring submissions-store
Browse files Browse the repository at this point in the history
  • Loading branch information
flbulgarelli committed Aug 10, 2020
1 parent b0f8921 commit e5242b2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ mumuki.bridge = (() => {
* @returns {JQuery.Promise<SubmissionResult>}
*/
_submitSolution(submission) {
const lastSubmission = mumuki.SubmissionsStore.getCachedResultFor(mumuki.currentExerciseId, submission);
const lastSubmission = mumuki.SubmissionsStore.getSubmissionResultFor(mumuki.currentExerciseId, submission);
if (lastSubmission) {
return $.Deferred().resolve(lastSubmission);
} else {
return this._sendNewSolution(submission).done((result) => {
mumuki.SubmissionsStore.setLastSubmission(mumuki.currentExerciseId, {submission, result});
mumuki.SubmissionsStore.setSubmissionResultFor(mumuki.currentExerciseId, {submission, result});
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@
mumuki.SubmissionsStore = (() => {
const SubmissionsStore = new class {
/**
* Returns the submission's result status for the last submission to
* the given exercise, or pending, if not present
*
* @param {number} exerciseId
* @returns {SubmissionStatus}
*/
getLastSubmissionStatus(exerciseId) {
const submission = this.getLastSubmission(exerciseId);
const submission = this.getLastSubmissionAndResult(exerciseId);
return submission ? submission.result.status : 'pending';
}

/**
* Returns the submission and result for the last submission to
* the given exercise
*
* @param {number} exerciseId
* @returns {SubmissionAndResult}
*/
getLastSubmission(exerciseId) {
getLastSubmissionAndResult(exerciseId) {
const submissionAndResult = window.localStorage.getItem(this._keyFor(exerciseId));
if (!submissionAndResult) return null;
return JSON.parse(submissionAndResult);
}

/**
* Saves the result for the given exercise
*
* @param {number} exerciseId
* @param {SubmissionAndResult} submissionAndResult
*/
setLastSubmission(exerciseId, submissionAndResult) {
setSubmissionResultFor(exerciseId, submissionAndResult) {
window.localStorage.setItem(this._keyFor(exerciseId), this._asString(submissionAndResult));
}

/**
* Retrieves the last cached, non-aborted result for the given submission
* Retrieves the last cached, non-aborted result for the given submission of the given exercise
*
* @param {number} exerciseId
* @param {Submission} submission
* @returns {SubmissionResult} the cached result for this submission
*/
getCachedResultFor(exerciseId, submission) {
const lastSubmission = this.getLastSubmission(exerciseId);
if (!lastSubmission
|| lastSubmission.result.status === 'aborted'
|| !this.submissionSolutionEquals(lastSubmission.submission, submission)) {
getSubmissionResultFor(exerciseId, submission) {
const last = this.getLastSubmissionAndResult(exerciseId);
if (!last
|| last.result.status === 'aborted'
|| !this.submissionSolutionEquals(last.submission, submission)) {
return null;
}
return lastSubmission.result;
return last.result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mumuki.syncMode = (() => {
}

syncEditorContent() {
const lastSubmission = mumuki.SubmissionsStore.getLastSubmission(mumuki.currentExerciseId);
const lastSubmission = mumuki.SubmissionsStore.getLastSubmissionAndResult(mumuki.currentExerciseId);
if (lastSubmission) {
/** @todo extract core module */
const content = mumuki.SubmissionsStore.submissionSolutionContent(lastSubmission.submission);
Expand Down
44 changes: 44 additions & 0 deletions spec/javascripts/submissions-store-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
describe("SubmissionsStore", () => {
const emptyProgramSubmission = {"solution[content]": "program {}"};
/** @type {SubmissionResult} */
const passedSubmissionResult = {status: 'passed'};
/** @type {SubmissionAndResult} */
const passedEmptyProgramSubmissionAndResult = { submission: emptyProgramSubmission, result: passedSubmissionResult };

beforeEach(() => {
window.localStorage.clear()
});

describe('getLastSubmission', () => {
it("answers null if submission not present", () => {
expect(mumuki.SubmissionsStore.getLastSubmissionAndResult(1)).toBe(null)
})

it("answers the last submission result if already sent", () => {
mumuki.SubmissionsStore.setSubmissionResultFor(1, passedEmptyProgramSubmissionAndResult)
expect(mumuki.SubmissionsStore.getLastSubmissionAndResult(1)).toEqual(passedEmptyProgramSubmissionAndResult)
})
})

describe('getLastSubmissionStatus', () => {
it("answers pending if submission not present", () => {
expect(mumuki.SubmissionsStore.getLastSubmissionStatus(1)).toBe('pending')
})

it("answers the last submission status if previously sent", () => {
mumuki.SubmissionsStore.setSubmissionResultFor(1, passedEmptyProgramSubmissionAndResult)
expect(mumuki.SubmissionsStore.getLastSubmissionStatus(1)).toBe('passed')
})
});

describe('getCachedResultFor', () => {
it("answers null if submission not present", () => {
expect(mumuki.SubmissionsStore.getSubmissionResultFor(1, emptyProgramSubmission)).toBe(null)
})

it("answers the last submission if previously sent", () => {
mumuki.SubmissionsStore.setSubmissionResultFor(1, passedEmptyProgramSubmissionAndResult)
expect(mumuki.SubmissionsStore.getSubmissionResultFor(1, emptyProgramSubmission)).toEqual(passedSubmissionResult)
})
});
})

0 comments on commit e5242b2

Please sign in to comment.