Skip to content

Commit

Permalink
wip make reducer tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed May 1, 2023
1 parent 020ea73 commit dfe001a
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 72 deletions.
7 changes: 5 additions & 2 deletions libs/common/domain/src/lib/search/aggregation.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type AggregationParams =
field: FieldName
limit: number
sort: AggregationSort
filter?: string
}
| {
type: 'histogram'
Expand All @@ -15,7 +16,7 @@ export type AggregationParams =
}
| {
type: 'filters'
filters: { name: string; filter: FieldFilter }[]
filters: Record<string, FieldFilter>
}
export type AggregationsParams = Record<FieldName, AggregationParams>

Expand All @@ -37,7 +38,9 @@ export type Bucket = TermBucket | HistogramBucket | FiltersBucket

export type AggregationSort = ['desc' | 'asc', 'key' | 'count']

export interface Aggregation {
interface AggregationBuckets {
buckets: Bucket[]
}
type AggregationCounts = Record<string, number>
export type Aggregation = AggregationBuckets | AggregationCounts
export type Aggregations = Record<FieldName, Aggregation>
1 change: 1 addition & 0 deletions libs/common/fixtures/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './lib/link.fixtures'
export * from './lib/records'
export * from './lib/organisations.fixture'
export * from './lib/elasticsearch'
export * from './lib/search'
export * from './lib/user.fixtures'
62 changes: 62 additions & 0 deletions libs/common/fixtures/src/lib/search/aggregations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { deepFreeze } from '../utils/freeze'
import {
AggregationParams,
Aggregations,
AggregationsParams,
} from '@geonetwork-ui/common/domain/search'

export const TERMS_AGGREGATION: AggregationParams = {
type: 'terms',
field: 'myField',
sort: ['desc', 'count'],
filter: 'abc.*',
limit: 50,
}
export const HISTOGRAM_AGGREGATION: AggregationParams = {
type: 'histogram',
field: 'myValueField',
interval: 100,
}
export const FILTERS_AGGREGATION: AggregationParams = {
type: 'filters',
filters: {
firstValue: 'value1',
secondValueOnly: {
value1: false,
value2: true,
value3: false,
},
},
}
export const AGGREGATIONS_PARAMS: AggregationsParams = deepFreeze({
myField: TERMS_AGGREGATION,
myValueField: HISTOGRAM_AGGREGATION,
myFilters: FILTERS_AGGREGATION,
})

export const AGGREGATIONS_RESULTS: Aggregations = deepFreeze({
myField: {
buckets: [
{ term: 'Hungary', count: 20 },
{
term: 'Austria',
count: 3,
},
{ term: 'Belgium', count: 8 },
{
term: 'Bulgaria',
count: 2,
},
{ term: 'Croatia', count: 15 },
{
term: 'Cyprus',
count: 5,
},
],
},
myValueField: { buckets: [] },
myFilters: {
firstValue: 123,
secondValueOnly: 45,
},
})
1 change: 1 addition & 0 deletions libs/common/fixtures/src/lib/search/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './aggregations'
19 changes: 2 additions & 17 deletions libs/feature/search/src/lib/state/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
AggregationsParams,
FieldFilters,
FieldName,
FieldSort,
SortByField,
} from '@geonetwork-ui/common/domain/search'
import { CatalogRecord } from '@geonetwork-ui/common/domain/record'

Expand Down Expand Up @@ -94,7 +94,7 @@ export class SetFavoritesOnly extends AbstractAction implements Action {
}
export class SetSortBy extends AbstractAction implements Action {
readonly type = SET_SORT_BY
constructor(public sortBy: FieldSort, id?: string) {
constructor(public sortBy: SortByField, id?: string) {
super(id)
}
}
Expand Down Expand Up @@ -218,20 +218,6 @@ export class SetIncludeOnAggregation extends AbstractAction implements Action {
}
}

export class UpdateRequestAggregationTerm
extends AbstractAction
implements Action
{
readonly type = UPDATE_REQUEST_AGGREGATION_TERM
constructor(
public aggregationName: string,
public patch: EsRequestAggTermPatch, // TODO: fix this
id?: string
) {
super(id)
}
}

export class PatchResultsAggregations extends AbstractAction implements Action {
readonly type = PATCH_RESULTS_AGGREGATIONS

Expand Down Expand Up @@ -289,7 +275,6 @@ export type SearchActions =
| SetConfigRequestFields
| RequestMoreOnAggregation
| SetIncludeOnAggregation
| UpdateRequestAggregationTerm
| PatchResultsAggregations
| SetError
| ClearError
Expand Down
87 changes: 46 additions & 41 deletions libs/feature/search/src/lib/state/reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
ES_FIXTURE_AGGS_REQUEST,
ES_FIXTURE_AGGS_RESPONSE,
ES_FIXTURE_AGGS_RESPONSE_MORE,
AGGREGATIONS_PARAMS,
AGGREGATIONS_RESULTS,
} from '@geonetwork-ui/common/fixtures'
import { DEFAULT_PAGE_SIZE } from '../constants'
import * as fromActions from './actions'
import { DEFAULT_SEARCH_KEY } from './actions'
import {
Expand All @@ -11,7 +11,6 @@ import {
reducerSearch,
SearchStateParams,
} from './reducer'
import { RESULTS_PAGE_SIZE } from '@geonetwork-ui/util/shared'

const initialStateSearch = initialState[DEFAULT_SEARCH_KEY]

Expand Down Expand Up @@ -50,13 +49,13 @@ describe('Search Reducer', () => {
describe('SetConfigFilters action', () => {
it('set config filters', () => {
const action = new fromActions.SetConfigFilters({
custom: { any: 'blah', other: 'Some value' },
elastic: {},
any: 'blah',
other: 'Some value',
})
const state = reducerSearch(initialStateSearch, action)
expect(state.config.filters).toEqual({
custom: { any: 'blah', other: 'Some value' },
elastic: {},
any: 'blah',
other: 'Some value',
})
})
})
Expand Down Expand Up @@ -124,8 +123,9 @@ describe('Search Reducer', () => {
describe('SetSearch action', () => {
it('should set search params', () => {
const searchParams: SearchStateParams = {
size: 12,
sortBy: 'asc',
limit: 12,
offset: 0,
sort: ['asc', 'tag'],
filters: {
any: 'tag:river',
},
Expand All @@ -139,14 +139,14 @@ describe('Search Reducer', () => {

describe('SetSortBy action', () => {
it('should set sort by params', () => {
const action = new fromActions.SetSortBy('fieldA')
const action = new fromActions.SetSortBy(['desc', 'fieldA'])
const state = reducerSearch(initialStateSearch, action)
expect(state.params.sortBy).toEqual('fieldA')
expect(state.params.sort).toEqual(['desc', 'fieldA'])
})
})

describe('SetFavoritesOnly action', () => {
it('should set favoritsOnly param to true', () => {
it('should set favoritesOnly param to true', () => {
const action = new fromActions.SetFavoritesOnly(true)
const state = reducerSearch(initialStateSearch, action)
expect(state.params.favoritesOnly).toEqual(true)
Expand All @@ -157,25 +157,25 @@ describe('Search Reducer', () => {
it('should set from and size', () => {
const action = new fromActions.SetPagination(12, 15)
const state = reducerSearch(initialStateSearch, action)
expect(state.params.from).toEqual(12)
expect(state.params.size).toEqual(15)
expect(state.params.offset).toEqual(12)
expect(state.params.limit).toEqual(15)
})
})

describe('Paginate action', () => {
it('should set from property and keep size', () => {
const action = new fromActions.Paginate(30)
const state = reducerSearch(initialStateSearch, action)
expect(state.params.from).toEqual(30)
expect(state.params.size).toEqual(RESULTS_PAGE_SIZE)
expect(state.params.offset).toEqual(30)
expect(state.params.limit).toEqual(DEFAULT_PAGE_SIZE)
})
})
describe('Scroll action', () => {
it('increment `from` property with `size` value', () => {
const action = new fromActions.Scroll()
const state = reducerSearch(initialStateSearch, action)
expect(state.params.from).toEqual(RESULTS_PAGE_SIZE)
expect(state.params.size).toEqual(RESULTS_PAGE_SIZE)
expect(state.params.offset).toEqual(DEFAULT_PAGE_SIZE)
expect(state.params.limit).toEqual(DEFAULT_PAGE_SIZE)
})
})

Expand Down Expand Up @@ -228,17 +228,15 @@ describe('Search Reducer', () => {
...initialStateSearch,
results: {
...initialStateSearch.results,
hits: {
value: 200,
},
count: 200,
records: [{ title: 'abcd' } as any],
},
},
action
)
expect(state.results).toEqual({
aggregations: {},
hits: { value: 200 },
count: 200,
records: [],
})
})
Expand All @@ -254,37 +252,44 @@ describe('Search Reducer', () => {

describe('SetResultsAggregations action', () => {
it('should replace the aggregations in the result', () => {
const payload = ES_FIXTURE_AGGS_RESPONSE
const payload = AGGREGATIONS_RESULTS
const action = new fromActions.SetResultsAggregations(payload)
const state = reducerSearch(
{
...initialStateSearch,
results: {
...initialStateSearch.results,
aggregations: { someKey: 'someValue' },
aggregations: { someKey: { buckets: [] } },
},
},
action
)
expect(state.results.aggregations).toEqual(ES_FIXTURE_AGGS_RESPONSE)
expect(state.results.aggregations).toEqual(AGGREGATIONS_RESULTS)
})
})

describe('SetConfigAggregations action', () => {
it('should replace the aggregations in the config', () => {
const payload = ES_FIXTURE_AGGS_REQUEST
const payload = AGGREGATIONS_PARAMS
const action = new fromActions.SetConfigAggregations(payload)
const state = reducerSearch(
{
...initialStateSearch,
config: {
...initialStateSearch.config,
aggregations: { someKey: 'someValue' },
aggregations: {
someKey: {
type: 'terms',
field: 'someKey',
limit: 10,
sort: ['desc', 'key'],
},
},
},
},
action
)
expect(state.config.aggregations).toEqual(ES_FIXTURE_AGGS_REQUEST)
expect(state.config.aggregations).toEqual(AGGREGATIONS_PARAMS)
})
})

Expand Down Expand Up @@ -326,28 +331,28 @@ describe('Search Reducer', () => {
describe('UpdateRequestAggregationTerm action', () => {
describe('RequestMoreOnAggregation action', () => {
it('should replace the aggregations in the config with an updated size', () => {
const action = new fromActions.UpdateRequestAggregationTerm(
const action = new fromActions.RequestMoreOnAggregation(
'tag.default',
{ increment: 20 }
20
)
const state = reducerSearch(
{
...initialStateSearch,
config: {
...initialStateSearch.config,
aggregations: ES_FIXTURE_AGGS_REQUEST,
aggregations: AGGREGATIONS_PARAMS,
},
},
action
)
const clone = JSON.parse(JSON.stringify(ES_FIXTURE_AGGS_REQUEST))
const clone = JSON.parse(JSON.stringify(AGGREGATIONS_PARAMS))
clone['tag.default'].terms.size = 30
expect(state.config.aggregations).toEqual(clone)
})
it('when intial size is Nan', () => {
const action = new fromActions.UpdateRequestAggregationTerm(
it('when intial size is NaN', () => {
const action = new fromActions.RequestMoreOnAggregation(
'tag.default',
{ increment: 20 }
20
)
const state = reducerSearch(
{
Expand All @@ -369,21 +374,21 @@ describe('Search Reducer', () => {

describe('SetIncludeOnAggregation action', () => {
it('should replace the aggregations in the config with an updated include', () => {
const action = new fromActions.UpdateRequestAggregationTerm(
const action = new fromActions.SetIncludeOnAggregation(
'tag.default',
{ include: '.*Land.*' }
'.*Land.*'
)
const state = reducerSearch(
{
...initialStateSearch,
config: {
...initialStateSearch.config,
aggregations: ES_FIXTURE_AGGS_REQUEST,
aggregations: AGGREGATIONS_PARAMS,
},
},
action
)
const clone = JSON.parse(JSON.stringify(ES_FIXTURE_AGGS_REQUEST))
const clone = JSON.parse(JSON.stringify(AGGREGATIONS_PARAMS))
clone['tag.default'].terms.include = '.*Land.*'
expect(state.config.aggregations).toEqual(clone)
})
Expand All @@ -402,7 +407,7 @@ describe('Search Reducer', () => {
...initialStateSearch,
results: {
...initialStateSearch.results,
aggregations: ES_FIXTURE_AGGS_RESPONSE,
aggregations: AGGREGATIONS_RESULTS,
},
},
action
Expand Down
Loading

0 comments on commit dfe001a

Please sign in to comment.