Skip to content

Commit

Permalink
Extract repeated filter map builder test prep into helper
Browse files Browse the repository at this point in the history
  • Loading branch information
latonv committed Nov 21, 2022
1 parent 5cedba4 commit 563a895
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions test/filter-map-builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ import { expect } from '@open-wc/testing';
import { FilterMapBuilder } from '../src/filter-map-builder';
import { FilterConstraint, FilterMap } from '../src/search-params';

/**
* Creates a filter map builder with the following structure:
* ```
* {
* 'foo': {
* 'bar': [INCLUDE, GREATER_OR_EQUAL],
* 'beep': GREATER_THAN,
* },
* 'baz': {
* 'boop': EXCLUDE
* },
* }
* ```
*/
const getComplexFilterMapBuilder = (): FilterMapBuilder => {
const builder = new FilterMapBuilder();
builder.addFilter('foo', 'bar', FilterConstraint.INCLUDE);
builder.addFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
builder.addFilter('baz', 'boop', FilterConstraint.EXCLUDE);
builder.addFilter('foo', 'beep', FilterConstraint.GREATER_THAN);
return builder;
};

describe('filter map builder', () => {
it('initializes with empty filter map', () => {
expect(new FilterMapBuilder().build()).to.deep.equal({});
Expand Down Expand Up @@ -37,15 +60,7 @@ describe('filter map builder', () => {
});

it('can remove filters', () => {
const builder = new FilterMapBuilder();
builder.addFilter('foo', 'bar', FilterConstraint.INCLUDE);
builder.addFilter('baz', 'boop', FilterConstraint.EXCLUDE);
builder.addFilter('foo', 'beep', FilterConstraint.GREATER_THAN);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc', beep: 'gt' },
baz: { boop: 'exc' },
});

const builder = getComplexFilterMapBuilder();
builder.removeFilters('foo', 'bar');
expect(builder.build()).to.deep.equal({
foo: { beep: 'gt' },
Expand All @@ -63,32 +78,26 @@ describe('filter map builder', () => {
});

it('can remove single filters by constraint type', () => {
const builder = new FilterMapBuilder();
builder.addFilter('foo', 'bar', FilterConstraint.INCLUDE);
builder.addFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
builder.addFilter('baz', 'boop', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({
foo: { bar: ['inc', 'gte'] },
baz: { boop: 'exc' },
});

const builder = getComplexFilterMapBuilder();
builder.removeSingleFilter('foo', 'bar', FilterConstraint.GREATER_OR_EQUAL);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc' },
foo: { bar: 'inc', beep: 'gt' },
baz: { boop: 'exc' },
});

builder.removeSingleFilter('foo', 'bar', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({
foo: { bar: 'inc' },
foo: { bar: 'inc', beep: 'gt' },
baz: { boop: 'exc' },
});

builder.removeSingleFilter('foo', 'bar', FilterConstraint.INCLUDE);
expect(builder.build()).to.deep.equal({
foo: { beep: 'gt' },
baz: { boop: 'exc' },
});

builder.removeSingleFilter('foo', 'beep', FilterConstraint.GREATER_THAN);
builder.removeSingleFilter('baz', 'boop', FilterConstraint.EXCLUDE);
expect(builder.build()).to.deep.equal({});

Expand Down

0 comments on commit 563a895

Please sign in to comment.