diff --git a/src/app/components/home.test.tsx b/src/app/components/home.test.tsx index cce6bf3..fdeaddb 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,9 +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(), - ) + return render(HomePageUIService.createNull({ questionSetRepo }).getElement()) } diff --git a/src/app/components/home.tsx b/src/app/components/home.tsx index c18a1d1..b6328bb 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(), + static createNull({ + 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..2ac48a1 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,11 +18,11 @@ class UIServiceInteractor { constructor({ questionSetName = 'Dummy name', - questionSetRepo = QuestionSetRepoFactory.createTestInstance(), + questionSetRepo = LocalStorageQuestionSetRepo.createNull(), }) { this.questionSetRepo = questionSetRepo render( - QuestionSetEditorUIService.createTestInstance({ + QuestionSetEditorUIService.createNull({ questionSetRepo: this.questionSetRepo, }).getElement(), ) @@ -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..22b9c35 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,14 @@ export class QuestionSetEditorAriaLabel { export class QuestionSetEditorUIService { static create() { return new QuestionSetEditorUIService({ - questionSetRepo: QuestionSetRepoFactory.createLocalStorageInstance(), + questionSetRepo: LocalStorageQuestionSetRepo.create(), }) } - static createTestInstance({ - questionSetRepo = QuestionSetRepoFactory.createTestInstance(), + static createNull({ + questionSetRepo = LocalStorageQuestionSetRepo.createNull(), + }: { + questionSetRepo?: QuestionSetRepo }) { return new QuestionSetEditorUIService({ questionSetRepo }) } diff --git a/src/app/components/mc/quiz.test.tsx b/src/app/components/mc/quiz.test.tsx index e298a7b..b105a05 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 = () => { @@ -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(), ) @@ -123,10 +123,10 @@ function renderMultipleChoicePage({ }: { questionSet: QuestionSet }) { - const questionSetRepo = QuestionSetRepoFactory.createTestInstance() + 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 efbcc7d..3534213 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(), + static createNull({ + 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..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 { /** @@ -38,38 +38,26 @@ export class GetQuestionSetError extends CustomBaseError, - ) { - 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 d44864b..b6aaffc 100644 --- a/src/utils/local_storage.ts +++ b/src/utils/local_storage.ts @@ -27,21 +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 createTestInstance( - storagePath: string, - ): LocalStorageObjectOperator { - return new LocalStorageObjectOperator( - new FakeLocalStorageWrapper(), - storagePath, - ) + static createNull(storagePath: string): LocalStorageOperator { + return new LocalStorageOperator(new FakeLocalStorageWrapper(), storagePath) } private constructor(