Skip to content

Commit

Permalink
Implement repository & tests (WiP)
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Oct 25, 2023
1 parent 521885b commit 6b090e8
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 1 deletion.
Empty file.
Empty file.
Empty file.
67 changes: 67 additions & 0 deletions src/afo-register/domain/Record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { immerable } from 'immer'
import { Fragment } from 'fragmentarium/domain/fragment'

interface Record {
readonly afoNumber: string
readonly page: string
readonly text: string
readonly textNumber: string
readonly linesDiscussed?: string
readonly discussedBy?: string
readonly discussedByNotes?: string
}

export default class AfoRegisterRecord {
[immerable] = true

readonly afoNumber: string
readonly page: string
readonly text: string
readonly textNumber: string
readonly linesDiscussed?: string
readonly discussedBy?: string
readonly discussedByNotes?: string

constructor({
afoNumber,
page,
text,
textNumber,
linesDiscussed,
discussedBy,
discussedByNotes,
}: Record) {
this.afoNumber = afoNumber
this.page = page
this.text = text
this.textNumber = textNumber
this.linesDiscussed = linesDiscussed
this.discussedBy = discussedBy
this.discussedByNotes = discussedByNotes
}

toMarkdownString(fragments: Fragment[]): string {
let result = this.text + (this.textNumber ? ' ' + this.textNumber : '')
const linkToFragment = this.findLinkToFragment(fragments)
if (linkToFragment) result += `(${linkToFragment})`
if (this.linesDiscussed) result += ', ' + this.linesDiscussed
if (this.discussedBy) result += ': ' + this.discussedBy
if (this.discussedByNotes) result += ' ' + this.discussedByNotes

result += `<small class="text-black-50 ml-3">${
this.afoNumber + this.page
}</small>`

result = result.replace(/\^([^^]+)\^/g, '<sup>$1</sup>')
return result
}

findLinkToFragment(fragments: Fragment[]): string {
const reference = this.text + ' ' + this.textNumber
const matchingFragments = fragments.filter((fragment) =>
fragment.traditionalReferences.includes(reference)
)
if (matchingFragments.length === 0) return ''
return matchingFragments.map((fragment) => fragment.number).join(', ')
}
}
34 changes: 34 additions & 0 deletions src/afo-register/infrastructure/AfoRegisterRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import AfoRegisterRecord from 'afo-register/domain/Record'
import ApiClient from 'http/ApiClient'
import AfoRegisterRepository from './AfoRegisterRepository'
import Promise from 'bluebird'
import { testDelegation, TestData } from 'test-support/utils'

jest.mock('afo-register/domain/Record')
jest.mock('http/ApiClient')

const apiClient = new (ApiClient as jest.Mock<jest.Mocked<ApiClient>>)()

const afoRegisterRepository = new AfoRegisterRepository(apiClient)
const query = '{"afoNumber": "AfO 12", "page": "321"}'
const resultStub = {
afoNumber: 'AfO 12',
page: '321',
text: 'text',
textNumber: 'text number',
} as AfoRegisterRecord
const entry = new AfoRegisterRecord(resultStub)

const testData: TestData<AfoRegisterRepository>[] = [
new TestData(
'search',
[query],
apiClient.fetchJson,
[entry],
[`/afo-register?${encodeURIComponent(query)}`, false],
Promise.resolve(entry)
),
]

describe('AfoRegisterRepository', () =>
testDelegation(afoRegisterRepository, testData))
17 changes: 17 additions & 0 deletions src/afo-register/infrastructure/AfoRegisterRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import AfoRegisterRecord from 'afo-register/domain/Record'
import Promise from 'bluebird'
import ApiClient from 'http/ApiClient'

export default class BibliographyRepository {
private readonly apiClient: ApiClient

constructor(apiClient: ApiClient) {
this.apiClient = apiClient
}

search(query: string): Promise<AfoRegisterRecord> {
return this.apiClient
.fetchJson(`/afo-register?${encodeURIComponent(query)}`, false)
.then((result) => new AfoRegisterRecord(result))
}
}
4 changes: 4 additions & 0 deletions src/fragmentarium/domain/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class Fragment {
readonly museum: Museum,
readonly references: ReadonlyArray<Reference>,
readonly uncuratedReferences: ReadonlyArray<UncuratedReference> | null,
readonly traditionalReferences: readonly string[],
readonly atf: string,
readonly hasPhoto: boolean,
readonly genres: Genres,
Expand Down Expand Up @@ -117,6 +118,7 @@ export class Fragment {
museum,
references,
uncuratedReferences,
traditionalReferences,
atf,
hasPhoto,
genres,
Expand Down Expand Up @@ -144,6 +146,7 @@ export class Fragment {
museum: Museum
references: ReadonlyArray<Reference>
uncuratedReferences?: ReadonlyArray<UncuratedReference> | null
traditionalReferences: readonly string[]
atf: string
hasPhoto: boolean
genres: Genres
Expand Down Expand Up @@ -172,6 +175,7 @@ export class Fragment {
museum,
references,
uncuratedReferences ?? null,
traditionalReferences,
atf,
hasPhoto,
genres,
Expand Down
5 changes: 4 additions & 1 deletion src/test-support/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ export function testDelegation<S>(

it('Returns', async () => {
if (result instanceof Bluebird || result instanceof Promise) {
await expect(result).resolves.toEqual(expectedResult)
const resolvedResult = await result
console.log('!!!', resolvedResult.afoNumber)
console.log('!11', expectedResult.afoNumber)
await expect(resolvedResult).toEqual(expectedResult)
} else {
expect(result).toEqual(expectedResult)
}
Expand Down

0 comments on commit 6b090e8

Please sign in to comment.