From ea227da488773b75679b5a2e1044635593ef9843 Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Wed, 21 Feb 2024 18:06:11 +0800 Subject: [PATCH 1/6] Refactor constructor of QuestionSetRepo --- src/app/components/home.test.tsx | 4 ++-- src/app/components/home.tsx | 6 +++--- src/app/components/mc/editor.test.tsx | 6 +++--- src/app/components/mc/editor.tsx | 6 +++--- src/app/components/mc/quiz.test.tsx | 4 ++-- src/app/components/mc/quiz.tsx | 6 +++--- src/repo/question_set.test.ts | 2 +- src/repo/question_set.ts | 14 ++------------ src/utils/local_storage.ts | 4 +--- 9 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/app/components/home.test.tsx b/src/app/components/home.test.tsx index cce6bf3..61f8005 100644 --- a/src/app/components/home.test.tsx +++ b/src/app/components/home.test.tsx @@ -5,7 +5,7 @@ import { import { render } from '@testing-library/react' import '@testing-library/jest-dom' import { HomePageUIService } from './home' -import { QuestionSetRepoFactory } from '../../repo/question_set' +import { LocalStorageQuestionSetRepo } from '../../repo/question_set' describe('HomePage', () => { // Detail of testing of the navigation of this page should be in the integration test combine with saving question set @@ -51,7 +51,7 @@ function renderHomePage({ }: { questionSets: readonly QuestionSet[] }) { - const questionSetRepo = QuestionSetRepoFactory.createTestInstance() + const questionSetRepo = LocalStorageQuestionSetRepo.createNull() questionSets.forEach((set) => questionSetRepo.addQuestionSet(set)) return render( HomePageUIService.createTestInstance({ questionSetRepo }).getElement(), diff --git a/src/app/components/home.tsx b/src/app/components/home.tsx index c18a1d1..0741b7f 100644 --- a/src/app/components/home.tsx +++ b/src/app/components/home.tsx @@ -4,7 +4,7 @@ import { useRouter } from 'next/navigation' import { QuestionSet } from '../../model/question_set' import { QuestionSetRepo, - QuestionSetRepoFactory, + LocalStorageQuestionSetRepo, } from '../../repo/question_set' import { useEffect, useState } from 'react' import LoadingSpinner from './loading' @@ -12,12 +12,12 @@ import LoadingSpinner from './loading' export class HomePageUIService { static create() { return new HomePageUIService({ - questionSetRepo: QuestionSetRepoFactory.createLocalStorageInstance(), + questionSetRepo: LocalStorageQuestionSetRepo.create(), }) } static createTestInstance({ - questionSetRepo = QuestionSetRepoFactory.createTestInstance(), + questionSetRepo = LocalStorageQuestionSetRepo.createNull(), }) { return new HomePageUIService({ questionSetRepo }) } diff --git a/src/app/components/mc/editor.test.tsx b/src/app/components/mc/editor.test.tsx index 18e3d91..e3da93d 100644 --- a/src/app/components/mc/editor.test.tsx +++ b/src/app/components/mc/editor.test.tsx @@ -1,7 +1,7 @@ import { fireEvent, render, screen } from '@testing-library/react' import { QuestionSetRepo, - QuestionSetRepoFactory, + LocalStorageQuestionSetRepo, } from '../../../repo/question_set' import { QuestionSetEditorAriaLabel, @@ -18,7 +18,7 @@ class UIServiceInteractor { constructor({ questionSetName = 'Dummy name', - questionSetRepo = QuestionSetRepoFactory.createTestInstance(), + questionSetRepo = LocalStorageQuestionSetRepo.createNull(), }) { this.questionSetRepo = questionSetRepo render( @@ -465,7 +465,7 @@ describe('QuestionSetEditorUIService', () => { }) it('should not save if same name as existing question set', () => { - const questionSetRepo = QuestionSetRepoFactory.createTestInstance() + const questionSetRepo = LocalStorageQuestionSetRepo.createNull() const questionSet = new QuestionSetBuilderForTest() .setName('Test name') .appendQuestion() diff --git a/src/app/components/mc/editor.tsx b/src/app/components/mc/editor.tsx index 0be5da9..dcf2957 100644 --- a/src/app/components/mc/editor.tsx +++ b/src/app/components/mc/editor.tsx @@ -3,7 +3,7 @@ import { useState } from 'react' import { QuestionSetRepo, - QuestionSetRepoFactory, + LocalStorageQuestionSetRepo, AddQuestionSetError, } from '../../../repo/question_set' import { Question, QuestionSet } from '../../../model/question_set' @@ -49,12 +49,12 @@ export class QuestionSetEditorAriaLabel { export class QuestionSetEditorUIService { static create() { return new QuestionSetEditorUIService({ - questionSetRepo: QuestionSetRepoFactory.createLocalStorageInstance(), + questionSetRepo: LocalStorageQuestionSetRepo.create(), }) } static createTestInstance({ - questionSetRepo = QuestionSetRepoFactory.createTestInstance(), + questionSetRepo = LocalStorageQuestionSetRepo.createNull(), }) { return new QuestionSetEditorUIService({ questionSetRepo }) } diff --git a/src/app/components/mc/quiz.test.tsx b/src/app/components/mc/quiz.test.tsx index e298a7b..500fdf2 100644 --- a/src/app/components/mc/quiz.test.tsx +++ b/src/app/components/mc/quiz.test.tsx @@ -6,7 +6,7 @@ import { QuestionSetBuilderForTest, QuestionSet, } from '../../../model/question_set' -import { QuestionSetRepoFactory } from '../../../repo/question_set' +import { LocalStorageQuestionSetRepo } from '../../../repo/question_set' describe('MultipleChoiceQuiz', () => { const presetCorrectChoiceMcBuilder = () => { @@ -123,7 +123,7 @@ function renderMultipleChoicePage({ }: { questionSet: QuestionSet }) { - const questionSetRepo = QuestionSetRepoFactory.createTestInstance() + const questionSetRepo = LocalStorageQuestionSetRepo.createNull() questionSetRepo.addQuestionSet(questionSet) return render( MultipleChoiceQuizUIService.createTestInstance({ diff --git a/src/app/components/mc/quiz.tsx b/src/app/components/mc/quiz.tsx index efbcc7d..0344898 100644 --- a/src/app/components/mc/quiz.tsx +++ b/src/app/components/mc/quiz.tsx @@ -5,7 +5,7 @@ import { Question, QuestionSet } from '../../../model/question_set' import { GetQuestionSetError, QuestionSetRepo, - QuestionSetRepoFactory, + LocalStorageQuestionSetRepo, } from '../../../repo/question_set' import LoadingSpinner from '../loading' import Error from 'next/error' @@ -13,13 +13,13 @@ import Error from 'next/error' export class MultipleChoiceQuizUIService { static create({ questionSetId }: { questionSetId: string }) { return new MultipleChoiceQuizUIService({ - questionSetRepo: QuestionSetRepoFactory.createLocalStorageInstance(), + questionSetRepo: LocalStorageQuestionSetRepo.create(), questionSetId, }) } static createTestInstance({ - questionSetRepo = QuestionSetRepoFactory.createTestInstance(), + questionSetRepo = LocalStorageQuestionSetRepo.createNull(), questionSetId, }: { questionSetRepo?: QuestionSetRepo diff --git a/src/repo/question_set.test.ts b/src/repo/question_set.test.ts index 1353294..3635651 100644 --- a/src/repo/question_set.test.ts +++ b/src/repo/question_set.test.ts @@ -14,7 +14,7 @@ describe('LocalStorageQuestionSetRepo', () => { let questionSet: QuestionSet beforeEach(() => { - repo = LocalStorageQuestionSetRepo.createTestInstance() + repo = LocalStorageQuestionSetRepo.createNull() questionSet = new QuestionSetBuilderForTest() .appendQuestion({ mc: new MultipleChoiceBuilder() diff --git a/src/repo/question_set.ts b/src/repo/question_set.ts index 8384c50..723971c 100644 --- a/src/repo/question_set.ts +++ b/src/repo/question_set.ts @@ -38,22 +38,12 @@ export class GetQuestionSetError extends CustomBaseError { ) } - static createTestInstance( - storagePath: string, - ): LocalStorageObjectOperator { + static createNull(storagePath: string): LocalStorageObjectOperator { return new LocalStorageObjectOperator( new FakeLocalStorageWrapper(), storagePath, From 1cc6e6dfd15c0df25904414d6bd31dc6298344e9 Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Wed, 21 Feb 2024 18:13:41 +0800 Subject: [PATCH 2/6] Rename to createNull for HomePageUIService --- src/app/components/home.test.tsx | 4 +--- src/app/components/home.tsx | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/app/components/home.test.tsx b/src/app/components/home.test.tsx index 61f8005..fdeaddb 100644 --- a/src/app/components/home.test.tsx +++ b/src/app/components/home.test.tsx @@ -53,7 +53,5 @@ function renderHomePage({ }) { const questionSetRepo = LocalStorageQuestionSetRepo.createNull() questionSets.forEach((set) => questionSetRepo.addQuestionSet(set)) - return render( - HomePageUIService.createTestInstance({ questionSetRepo }).getElement(), - ) + return render(HomePageUIService.createNull({ questionSetRepo }).getElement()) } diff --git a/src/app/components/home.tsx b/src/app/components/home.tsx index 0741b7f..b6328bb 100644 --- a/src/app/components/home.tsx +++ b/src/app/components/home.tsx @@ -16,7 +16,7 @@ export class HomePageUIService { }) } - static createTestInstance({ + static createNull({ questionSetRepo = LocalStorageQuestionSetRepo.createNull(), }) { return new HomePageUIService({ questionSetRepo }) From 8cce9eddb64dff1424803609d061b6c1db1b85aa Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Wed, 21 Feb 2024 18:16:36 +0800 Subject: [PATCH 3/6] Rename to createNull in QuestionSetEditorUIService --- src/app/components/mc/editor.test.tsx | 2 +- src/app/components/mc/editor.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/components/mc/editor.test.tsx b/src/app/components/mc/editor.test.tsx index e3da93d..2ac48a1 100644 --- a/src/app/components/mc/editor.test.tsx +++ b/src/app/components/mc/editor.test.tsx @@ -22,7 +22,7 @@ class UIServiceInteractor { }) { this.questionSetRepo = questionSetRepo render( - QuestionSetEditorUIService.createTestInstance({ + QuestionSetEditorUIService.createNull({ questionSetRepo: this.questionSetRepo, }).getElement(), ) diff --git a/src/app/components/mc/editor.tsx b/src/app/components/mc/editor.tsx index dcf2957..5906745 100644 --- a/src/app/components/mc/editor.tsx +++ b/src/app/components/mc/editor.tsx @@ -53,7 +53,7 @@ export class QuestionSetEditorUIService { }) } - static createTestInstance({ + static createNull({ questionSetRepo = LocalStorageQuestionSetRepo.createNull(), }) { return new QuestionSetEditorUIService({ questionSetRepo }) From a5441e3836b7eafb8ea3b25fe7309bb51b85edc9 Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Wed, 21 Feb 2024 18:17:54 +0800 Subject: [PATCH 4/6] Rename to createNull for multiple choice quiz ui --- src/app/components/mc/quiz.test.tsx | 4 ++-- src/app/components/mc/quiz.tsx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/components/mc/quiz.test.tsx b/src/app/components/mc/quiz.test.tsx index 500fdf2..b105a05 100644 --- a/src/app/components/mc/quiz.test.tsx +++ b/src/app/components/mc/quiz.test.tsx @@ -109,7 +109,7 @@ describe('MultipleChoiceQuiz', () => { it("should render not found when question set doesn't exist", () => { const { getByText } = render( - MultipleChoiceQuizUIService.createTestInstance({ + MultipleChoiceQuizUIService.createNull({ questionSetId: 'unknown', }).getElement(), ) @@ -126,7 +126,7 @@ function renderMultipleChoicePage({ const questionSetRepo = LocalStorageQuestionSetRepo.createNull() questionSetRepo.addQuestionSet(questionSet) return render( - MultipleChoiceQuizUIService.createTestInstance({ + MultipleChoiceQuizUIService.createNull({ questionSetRepo, questionSetId: questionSet.id, }).getElement(), diff --git a/src/app/components/mc/quiz.tsx b/src/app/components/mc/quiz.tsx index 0344898..3534213 100644 --- a/src/app/components/mc/quiz.tsx +++ b/src/app/components/mc/quiz.tsx @@ -18,7 +18,7 @@ export class MultipleChoiceQuizUIService { }) } - static createTestInstance({ + static createNull({ questionSetRepo = LocalStorageQuestionSetRepo.createNull(), questionSetId, }: { From 69fb49dccb6ca1bfc46c5bbd8d2e43d87fce585a Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Wed, 21 Feb 2024 18:25:03 +0800 Subject: [PATCH 5/6] FIx typing error --- src/app/components/mc/editor.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/components/mc/editor.tsx b/src/app/components/mc/editor.tsx index 5906745..22b9c35 100644 --- a/src/app/components/mc/editor.tsx +++ b/src/app/components/mc/editor.tsx @@ -55,6 +55,8 @@ export class QuestionSetEditorUIService { static createNull({ questionSetRepo = LocalStorageQuestionSetRepo.createNull(), + }: { + questionSetRepo?: QuestionSetRepo }) { return new QuestionSetEditorUIService({ questionSetRepo }) } From 4ec8b131fe140328a460619b98c33e7eb7bc99d9 Mon Sep 17 00:00:00 2001 From: Leung Cheng Date: Wed, 21 Feb 2024 18:27:49 +0800 Subject: [PATCH 6/6] Sync naming to localStorageOperator --- src/repo/question_set.ts | 14 ++++++-------- src/utils/local_storage.ts | 16 +++++----------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/repo/question_set.ts b/src/repo/question_set.ts index 723971c..2ce9d7a 100644 --- a/src/repo/question_set.ts +++ b/src/repo/question_set.ts @@ -1,6 +1,6 @@ import { QuestionSet } from '../model/question_set' import { CustomBaseError } from '../utils/err' -import { LocalStorageObjectOperator } from '../utils/local_storage' +import { LocalStorageOperator } from '../utils/local_storage' export interface QuestionSetRepo { /** @@ -43,23 +43,21 @@ export class LocalStorageQuestionSetRepo implements QuestionSetRepo { static createNull(): LocalStorageQuestionSetRepo { return new LocalStorageQuestionSetRepo( - LocalStorageObjectOperator.createNull(this.STORAGE_PATH), + LocalStorageOperator.createNull(this.STORAGE_PATH), ) } static create(): LocalStorageQuestionSetRepo { return new LocalStorageQuestionSetRepo( - LocalStorageObjectOperator.create(this.STORAGE_PATH), + LocalStorageOperator.create(this.STORAGE_PATH), ) } - private constructor( - localStorageObjectOperator: LocalStorageObjectOperator, - ) { - this.localStorageOperator = localStorageObjectOperator + private constructor(localStorageOperator: LocalStorageOperator) { + this.localStorageOperator = localStorageOperator } - private localStorageOperator: LocalStorageObjectOperator + private localStorageOperator: LocalStorageOperator addQuestionSet(questionSet: QuestionSet): void { if ( diff --git a/src/utils/local_storage.ts b/src/utils/local_storage.ts index 6e59aaf..b6aaffc 100644 --- a/src/utils/local_storage.ts +++ b/src/utils/local_storage.ts @@ -27,19 +27,13 @@ class LocalStorageWrapperImpl implements LocalStorageWrapper { } } -export class LocalStorageObjectOperator { - static create(storagePath: string): LocalStorageObjectOperator { - return new LocalStorageObjectOperator( - new LocalStorageWrapperImpl(), - storagePath, - ) +export class LocalStorageOperator { + static create(storagePath: string): LocalStorageOperator { + return new LocalStorageOperator(new LocalStorageWrapperImpl(), storagePath) } - static createNull(storagePath: string): LocalStorageObjectOperator { - return new LocalStorageObjectOperator( - new FakeLocalStorageWrapper(), - storagePath, - ) + static createNull(storagePath: string): LocalStorageOperator { + return new LocalStorageOperator(new FakeLocalStorageWrapper(), storagePath) } private constructor(