Skip to content

Commit

Permalink
Fix tests , refactor & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
khoidt committed Dec 11, 2023
1 parent 0a8e072 commit 41dc0f0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
4 changes: 4 additions & 0 deletions src/afo-register/domain/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export default class AfoRegisterRecord {
this.fragmentNumbers = fragmentNumbers
}

get id(): string {
return this.text + ' ' + this.textNumber
}

toMarkdownString(): string {
const textNumber = this.textNumberToMarkdownString()
const fragments = this.fragmentsToMarkdownString()
Expand Down
53 changes: 27 additions & 26 deletions src/afo-register/infrastructure/AfoRegisterRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { stringify } from 'query-string'
import ApiClient from 'http/ApiClient'
import Bluebird from 'bluebird'
import FragmentService from 'fragmentarium/application/FragmentService'
import { QueryItem } from 'query/QueryResult'

jest.mock('http/ApiClient')
jest.mock('fragmentarium/application/FragmentService')
Expand All @@ -29,8 +28,8 @@ const resultStub = {
}

const query = { afoNumber: resultStub.afoNumber, page: resultStub.page }
const entry = new AfoRegisterRecord(resultStub)
const suggestionEntry = new AfoRegisterRecordSuggestion({
const record = new AfoRegisterRecord(resultStub)
const suggestionRecord = new AfoRegisterRecordSuggestion({
text: 'some text',
textNumbers: undefined,
})
Expand All @@ -45,23 +44,23 @@ const testData: TestData<AfoRegisterRepository>[] = [
}),
],
apiClient.fetchJson,
[entry],
[record],
[`/afo-register?${stringify(query)}`, false],
Promise.resolve([resultStub])
),
new TestData(
'searchTextsAndNumbers',
[['text1', 'number1']],
apiClient.postJson,
[entry],
[record],
['/afo-register/texts-numbers', ['text1', 'number1'], false],
Promise.resolve([resultStub])
),
new TestData(
'searchSuggestions',
['suggestion query'],
apiClient.fetchJson,
[suggestionEntry],
[suggestionRecord],
['/afo-register/suggestions?text_query=suggestion query', false],
Promise.resolve([resultStub])
),
Expand All @@ -71,9 +70,9 @@ describe('afoRegisterService', () =>

describe('AfoRegisterRepository - search', () => {
it('handles search without fragmentService', async () => {
apiClient.fetchJson.mockReturnValue(Bluebird.resolve([entry]))
apiClient.fetchJson.mockReturnValue(Bluebird.resolve([record]))
const response = await afoRegisterRepository.search(stringify(query))
expect(response).toEqual([entry])
expect(response).toEqual([record])
expect(apiClient.fetchJson).toHaveBeenCalledWith(
`/afo-register?${stringify(query)}`,
false
Expand All @@ -82,15 +81,15 @@ describe('AfoRegisterRepository - search', () => {

it('handles different query strings', async () => {
const query2 = { afoNumber: 'AfO 2', page: '3' }
const entry2 = new AfoRegisterRecord({
const record2 = new AfoRegisterRecord({
text: 'Some text',
textNumber: '22',
afoNumber: 'AfO 2',
page: '3',
})
apiClient.fetchJson.mockResolvedValueOnce([resultStub, entry2])
apiClient.fetchJson.mockResolvedValueOnce([resultStub, record2])
const response = await afoRegisterRepository.search(stringify(query2))
expect(response).toEqual([entry, entry2])
expect(response).toEqual([record, record2])
})

it('handles empty response', async () => {
Expand All @@ -110,14 +109,14 @@ describe('AfoRegisterRepository - search', () => {
describe('AfoRegisterRepository - searchTextsAndNumbers', () => {
it('handles various text and number combinations', async () => {
const query2 = ['text2', 'number2']
const entry2 = new AfoRegisterRecord({
const record2 = new AfoRegisterRecord({
...resultStub,
text: 'text2',
textNumber: 'number2',
})
apiClient.postJson.mockResolvedValueOnce([resultStub, entry2])
apiClient.postJson.mockResolvedValueOnce([resultStub, record2])
const response = await afoRegisterRepository.searchTextsAndNumbers(query2)
expect(response).toEqual([entry, entry2])
expect(response).toEqual([record, record2])
})

it('handles empty response', async () => {
Expand All @@ -140,13 +139,13 @@ describe('AfoRegisterRepository - searchTextsAndNumbers', () => {
describe('AfoRegisterRepository - searchSuggestions', () => {
it('handles different query strings', async () => {
const query2 = 'different suggestion query'
const suggestionEntry2 = new AfoRegisterRecordSuggestion({
const suggestionRecord2 = new AfoRegisterRecordSuggestion({
...resultStub,
text: 'different text',
})
apiClient.fetchJson.mockResolvedValueOnce([resultStub, suggestionEntry2])
apiClient.fetchJson.mockResolvedValueOnce([resultStub, suggestionRecord2])
const response = await afoRegisterRepository.searchSuggestions(query2)
expect(response).toEqual([suggestionEntry, suggestionEntry2])
expect(response).toEqual([suggestionRecord, suggestionRecord2])
})

it('handles empty response', async () => {
Expand All @@ -167,13 +166,15 @@ describe('AfoRegisterRepository - searchSuggestions', () => {

describe('AfoRegisterRepository - search with fragmentService', () => {
it('injects fragment references when fragmentService is provided', async () => {
const modifiedEntry = { ...entry, fragmentNumbers: ['Frag1', 'Frag2'] }
fragmentService.query.mockReturnValueOnce(
const modifiedRecord = { ...record, fragmentNumbers: ['Frag1', 'Frag2'] }
fragmentService.queryByTraditionalReferences.mockReturnValueOnce(
Bluebird.resolve({
items: [
{ museumNumber: 'Frag1' },
{ museumNumber: 'Frag2' },
] as QueryItem[],
{
traditionalReference: record.id,
fragmentNumbers: modifiedRecord.fragmentNumbers,
},
],
matchCountTotal: 2,
})
)
Expand All @@ -182,9 +183,9 @@ describe('AfoRegisterRepository - search with fragmentService', () => {
stringify(query),
fragmentService
)
expect(response).toEqual([modifiedEntry])
expect(fragmentService.query).toHaveBeenCalledWith({
traditionalReferences: entry.text + ' ' + entry.textNumber,
})
expect(response).toEqual([modifiedRecord])
expect(fragmentService.queryByTraditionalReferences).toHaveBeenCalledWith([
record.id,
])
})
})
5 changes: 2 additions & 3 deletions src/afo-register/infrastructure/AfoRegisterRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ function injectFragmentReferencesToRecords(
fragmentService: FragmentService
): Promise<AfoRegisterRecord[]> {
const traditionalReferences = records.map(
({ text, textNumber }: AfoRegisterRecord) => text + ' ' + textNumber
(record: AfoRegisterRecord) => record.id
)
return fragmentService
.queryByTraditionalReferences(traditionalReferences)
.then((result) =>
records.map((record) => {
const match = result.items.find(
(item) =>
item.traditionalReference === record.text + ' ' + record.textNumber
(item) => item.traditionalReference === record.id
)
if (match) {
return record.setFragmentNumbers(match.fragmentNumbers)
Expand Down
8 changes: 2 additions & 6 deletions src/afo-register/ui/AfoRegisterFragmentRecords.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ describe('AfoRegisterFragmentRecords', () => {
mockService.searchTextsAndNumbers.mockResolvedValue(mockRecords)

const fragment = fragmentFactory.build({
traditionalReferences: mockRecords.map(
(record) => record.text + ' ' + record.textNumber
),
traditionalReferences: mockRecords.map((record) => record.id),
})
await act(async () => {
render(
Expand All @@ -34,9 +32,7 @@ describe('AfoRegisterFragmentRecords', () => {
})
await waitFor(() => {
mockRecords.forEach(async (record) => {
expect(
await screen.findByText(record.text + ' ' + record.textNumber)
).toBeInTheDocument()
expect(await screen.findByText(record.id)).toBeInTheDocument()
})
})
})
Expand Down
1 change: 1 addition & 0 deletions src/fragmentarium/application/FragmentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const fragmentRepository = {
findInCorpus: jest.fn(),
query: jest.fn(),
listAllFragments: jest.fn(),
queryByTraditionalReferences: jest.fn(),
}

const imageRepository = {
Expand Down
1 change: 0 additions & 1 deletion src/query/FragmentQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type FragmentQuery = Partial<{
scriptPeriod: PeriodString
scriptPeriodModifier: PeriodModifierString
genre: string
traditionalReferences: string
project: keyof typeof ResearchProjects | null
latest: boolean
}>

0 comments on commit 41dc0f0

Please sign in to comment.