Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WEBDEV-5842 Add collectionTitles property to match new PPS responses #33

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/responses/search-response-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { SearchHitSchema } from './search-hit-schema';
export interface SearchResponseBody {
hits: SearchResponseHits;
aggregations?: Record<string, Aggregation>;
collection_titles?: Record<string, string>;
}

/**
Expand Down Expand Up @@ -52,6 +53,12 @@ export class SearchResponseDetails {
*/
aggregations?: Record<string, Aggregation>;

/**
* A map from collection identifiers (those present on the hits/aggregations)
* to their titles.
*/
collectionTitles?: Record<string, string>;

/**
* The hit schema for this response
*/
Expand All @@ -78,6 +85,10 @@ export class SearchResponseDetails {
{} as Record<string, Aggregation>
);
}

if (body?.collection_titles) {
this.collectionTitles = body.collection_titles;
}
}

/**
Expand Down
141 changes: 98 additions & 43 deletions test/responses/search-response-details.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
import { expect } from '@open-wc/testing';
import { Aggregation } from '../../src/models/aggregation';
import { HitType } from '../../src/models/hit-types/hit';
import { ItemHit } from '../../src/models/hit-types/item-hit';
import { TextHit } from '../../src/models/hit-types/text-hit';
import { SearchResponseDetails } from '../../src/responses/search-response-details';
import {
SearchResponseBody,
SearchResponseDetails,
} from '../../src/responses/search-response-details';

describe('SearchResponseDetails', () => {
it('constructs item hits', () => {
const responseBody = {
hits: {
total: 2,
returned: 2,
hits: [
{
fields: {
identifier: 'foo',
mediatype: 'texts',
},
},
{
fields: {
identifier: 'bar',
collection: ['baz'],
},
},
],
const responseBody: SearchResponseBody = {
hits: {
total: 2,
returned: 2,
hits: [
{
fields: {
identifier: 'foo',
mediatype: 'texts',
},
},
};
{
fields: {
identifier: 'bar',
collection: ['baz'],
},
},
],
},
collection_titles: {
baz: 'Baz Collection',
},
};

describe('SearchResponseDetails', () => {
it('constructs item hits', () => {
const responseSchema = {
hit_type: 'item' as HitType,
field_properties: {},
Expand All @@ -42,27 +49,6 @@ describe('SearchResponseDetails', () => {
});

it('constructs text hits', () => {
const responseBody = {
hits: {
total: 2,
returned: 2,
hits: [
{
fields: {
identifier: 'foo',
mediatype: 'texts',
},
},
{
fields: {
identifier: 'bar',
collection: ['baz'],
},
},
],
},
};

const responseSchema = {
hit_type: 'text' as HitType,
field_properties: {},
Expand All @@ -76,4 +62,73 @@ describe('SearchResponseDetails', () => {
expect(details.results[1].identifier).to.equal('bar');
expect(details.results[1].collection?.value).to.equal('baz');
});

it('includes aggregations', () => {
const aggsResponseBody: SearchResponseBody = {
hits: {
total: 0,
returned: 0,
hits: [],
},
aggregations: {
foo: new Aggregation({
buckets: [
{
key: 'bar',
doc_count: 10,
},
],
}),
},
};

const responseSchema = {
hit_type: 'item' as HitType,
field_properties: {},
};

const details = new SearchResponseDetails(aggsResponseBody, responseSchema);
expect(details.results.length).to.equal(0);
expect(details.aggregations).to.deep.equal({
foo: new Aggregation({
buckets: [
{
key: 'bar',
doc_count: 10,
},
],
}),
});
});

it('provides access to collection titles map', () => {
const responseSchema = {
hit_type: 'item' as HitType,
field_properties: {},
};

const details = new SearchResponseDetails(responseBody, responseSchema);
expect(details.results.length).to.equal(2);
expect(details.collectionTitles).to.deep.equal({ baz: 'Baz Collection' });
});

it('collection titles map is optional', () => {
const responseBodyWithoutTitles = Object.assign(
{},
responseBody
) as SearchResponseBody;
delete responseBodyWithoutTitles.collection_titles;

const responseSchema = {
hit_type: 'item' as HitType,
field_properties: {},
};

const details = new SearchResponseDetails(
responseBodyWithoutTitles,
responseSchema
);
expect(details.results.length).to.equal(2);
expect(details.collectionTitles).to.be.undefined;
});
});