Skip to content

Commit

Permalink
Merge pull request #31 from leung018/refactor-test-constructor
Browse files Browse the repository at this point in the history
Refactor creation methods for tests
  • Loading branch information
leung018 authored Feb 21, 2024
2 parents 6b26be1 + 4ec8b13 commit 7e53921
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 58 deletions.
8 changes: 3 additions & 5 deletions src/app/components/home.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
8 changes: 4 additions & 4 deletions src/app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ 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'

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 })
}
Expand Down
8 changes: 4 additions & 4 deletions src/app/components/mc/editor.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fireEvent, render, screen } from '@testing-library/react'
import {
QuestionSetRepo,
QuestionSetRepoFactory,
LocalStorageQuestionSetRepo,
} from '../../../repo/question_set'
import {
QuestionSetEditorAriaLabel,
Expand All @@ -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(),
)
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 6 additions & 4 deletions src/app/components/mc/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useState } from 'react'
import {
QuestionSetRepo,
QuestionSetRepoFactory,
LocalStorageQuestionSetRepo,
AddQuestionSetError,
} from '../../../repo/question_set'
import { Question, QuestionSet } from '../../../model/question_set'
Expand Down Expand Up @@ -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 })
}
Expand Down
8 changes: 4 additions & 4 deletions src/app/components/mc/quiz.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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(),
)
Expand All @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions src/app/components/mc/quiz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ 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'

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
Expand Down
2 changes: 1 addition & 1 deletion src/repo/question_set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('LocalStorageQuestionSetRepo', () => {
let questionSet: QuestionSet

beforeEach(() => {
repo = LocalStorageQuestionSetRepo.createTestInstance()
repo = LocalStorageQuestionSetRepo.createNull()
questionSet = new QuestionSetBuilderForTest()
.appendQuestion({
mc: new MultipleChoiceBuilder()
Expand Down
26 changes: 7 additions & 19 deletions src/repo/question_set.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down Expand Up @@ -38,38 +38,26 @@ export class GetQuestionSetError extends CustomBaseError<GetQuestionSetErrorCode
}
}

export class QuestionSetRepoFactory {
static createTestInstance(): QuestionSetRepo {
return LocalStorageQuestionSetRepo.createTestInstance()
}

static createLocalStorageInstance(): QuestionSetRepo {
return LocalStorageQuestionSetRepo.create()
}
}

export class LocalStorageQuestionSetRepo implements QuestionSetRepo {
static readonly STORAGE_PATH = 'questionSets'

static createTestInstance(): LocalStorageQuestionSetRepo {
static createNull(): LocalStorageQuestionSetRepo {
return new LocalStorageQuestionSetRepo(
LocalStorageObjectOperator.createTestInstance(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<QuestionSet>,
) {
this.localStorageOperator = localStorageObjectOperator
private constructor(localStorageOperator: LocalStorageOperator<QuestionSet>) {
this.localStorageOperator = localStorageOperator
}

private localStorageOperator: LocalStorageObjectOperator<QuestionSet>
private localStorageOperator: LocalStorageOperator<QuestionSet>

addQuestionSet(questionSet: QuestionSet): void {
if (
Expand Down
18 changes: 5 additions & 13 deletions src/utils/local_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,13 @@ class LocalStorageWrapperImpl implements LocalStorageWrapper {
}
}

export class LocalStorageObjectOperator<T> {
static create<T>(storagePath: string): LocalStorageObjectOperator<T> {
return new LocalStorageObjectOperator(
new LocalStorageWrapperImpl(),
storagePath,
)
export class LocalStorageOperator<T> {
static create<T>(storagePath: string): LocalStorageOperator<T> {
return new LocalStorageOperator(new LocalStorageWrapperImpl(), storagePath)
}

static createTestInstance<T>(
storagePath: string,
): LocalStorageObjectOperator<T> {
return new LocalStorageObjectOperator(
new FakeLocalStorageWrapper(),
storagePath,
)
static createNull<T>(storagePath: string): LocalStorageOperator<T> {
return new LocalStorageOperator(new FakeLocalStorageWrapper(), storagePath)
}

private constructor(
Expand Down

0 comments on commit 7e53921

Please sign in to comment.