Skip to content

Commit

Permalink
Add more unit tests for response body aggregations and collection titles
Browse files Browse the repository at this point in the history
  • Loading branch information
latonv committed Feb 10, 2023
1 parent 9fc309c commit bfa87d3
Showing 1 changed file with 98 additions and 43 deletions.
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;
});
});

0 comments on commit bfa87d3

Please sign in to comment.