diff --git a/src/afo-register/domain/Record.ts b/src/afo-register/domain/Record.ts index 4f1027a44..0ab1fcfa4 100644 --- a/src/afo-register/domain/Record.ts +++ b/src/afo-register/domain/Record.ts @@ -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() diff --git a/src/afo-register/infrastructure/AfoRegisterRepository.test.ts b/src/afo-register/infrastructure/AfoRegisterRepository.test.ts index f7cbbab0f..f472842f1 100644 --- a/src/afo-register/infrastructure/AfoRegisterRepository.test.ts +++ b/src/afo-register/infrastructure/AfoRegisterRepository.test.ts @@ -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') @@ -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, }) @@ -45,7 +44,7 @@ const testData: TestData[] = [ }), ], apiClient.fetchJson, - [entry], + [record], [`/afo-register?${stringify(query)}`, false], Promise.resolve([resultStub]) ), @@ -53,7 +52,7 @@ const testData: TestData[] = [ 'searchTextsAndNumbers', [['text1', 'number1']], apiClient.postJson, - [entry], + [record], ['/afo-register/texts-numbers', ['text1', 'number1'], false], Promise.resolve([resultStub]) ), @@ -61,7 +60,7 @@ const testData: TestData[] = [ 'searchSuggestions', ['suggestion query'], apiClient.fetchJson, - [suggestionEntry], + [suggestionRecord], ['/afo-register/suggestions?text_query=suggestion query', false], Promise.resolve([resultStub]) ), @@ -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 @@ -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 () => { @@ -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 () => { @@ -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 () => { @@ -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, }) ) @@ -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, + ]) }) }) diff --git a/src/afo-register/infrastructure/AfoRegisterRepository.ts b/src/afo-register/infrastructure/AfoRegisterRepository.ts index 365b04330..3aa14d6a8 100644 --- a/src/afo-register/infrastructure/AfoRegisterRepository.ts +++ b/src/afo-register/infrastructure/AfoRegisterRepository.ts @@ -19,15 +19,14 @@ function injectFragmentReferencesToRecords( fragmentService: FragmentService ): Promise { 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) diff --git a/src/afo-register/ui/AfoRegisterFragmentRecords.test.tsx b/src/afo-register/ui/AfoRegisterFragmentRecords.test.tsx index 6487b2f01..837cb032b 100644 --- a/src/afo-register/ui/AfoRegisterFragmentRecords.test.tsx +++ b/src/afo-register/ui/AfoRegisterFragmentRecords.test.tsx @@ -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( @@ -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() }) }) }) diff --git a/src/fragmentarium/application/FragmentService.test.ts b/src/fragmentarium/application/FragmentService.test.ts index 6d321135c..696848b00 100644 --- a/src/fragmentarium/application/FragmentService.test.ts +++ b/src/fragmentarium/application/FragmentService.test.ts @@ -78,6 +78,7 @@ const fragmentRepository = { findInCorpus: jest.fn(), query: jest.fn(), listAllFragments: jest.fn(), + queryByTraditionalReferences: jest.fn(), } const imageRepository = { diff --git a/src/query/FragmentQuery.ts b/src/query/FragmentQuery.ts index 3eef7b078..00b2198b2 100644 --- a/src/query/FragmentQuery.ts +++ b/src/query/FragmentQuery.ts @@ -18,7 +18,6 @@ export type FragmentQuery = Partial<{ scriptPeriod: PeriodString scriptPeriodModifier: PeriodModifierString genre: string - traditionalReferences: string project: keyof typeof ResearchProjects | null latest: boolean }>