Extend search by including documentation content
This commit broadens the search functionality within privacy.sexy by including documentation text in the search scope. Users can now find scripts and categories not only by their names but also by content in their documentation. This improvement aims to make the discovery of relevant scripts and information more intuitive and comprehensive. Key changes: - Documentation text is now searchable, enhancing the ability to discover scripts and categories based on content details. Other supporting changes: - Remove interface prefixes (`I`) from related interfaces to adhere to naming conventions, enhancing code readability. - Refactor filtering to separate actual filtering logic from filter state management, improving the structure for easier maintenance. - Improve test coverage to ensure relability of existing and new search capabilities. - Test coverage expanded to ensure the reliability of the new search capabilities.
This commit is contained in:
@@ -11,8 +11,8 @@ import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
||||
import { ReadonlyScriptSelection } from '@/application/Context/State/Selection/Script/ScriptSelection';
|
||||
import { ScriptSelectionStub } from '@tests/unit/shared/Stubs/ScriptSelectionStub';
|
||||
import { SelectedScript } from '@/application/Context/State/Selection/Script/SelectedScript';
|
||||
import { UserFilterStub } from '@tests/unit/shared/Stubs/UserFilterStub';
|
||||
import type { CodeFactory, FilterFactory, SelectionFactory } from '@/application/Context/State/CategoryCollectionState';
|
||||
import { FilterContextStub } from '@tests/unit/shared/Stubs/FilterContextStub';
|
||||
|
||||
describe('CategoryCollectionState', () => {
|
||||
describe('code', () => {
|
||||
@@ -113,7 +113,7 @@ describe('CategoryCollectionState', () => {
|
||||
let actualCollection: ICategoryCollection | undefined;
|
||||
const filterFactoryMock: FilterFactory = (collection) => {
|
||||
actualCollection = collection;
|
||||
return new UserFilterStub();
|
||||
return new FilterContextStub();
|
||||
};
|
||||
// act
|
||||
new CategoryCollectionStateBuilder()
|
||||
@@ -134,7 +134,7 @@ class CategoryCollectionStateBuilder {
|
||||
|
||||
private selectionFactory: SelectionFactory = () => new UserSelectionStub();
|
||||
|
||||
private filterFactory: FilterFactory = () => new UserFilterStub();
|
||||
private filterFactory: FilterFactory = () => new FilterContextStub();
|
||||
|
||||
public withCollection(collection: ICategoryCollection): this {
|
||||
this.collection = collection;
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { FilterChangeDetailsStub } from '@tests/unit/shared/Stubs/FilterChangeDetailsStub';
|
||||
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
||||
import { FilterChangeDetails } from '@/application/Context/State/Filter/Event/FilterChangeDetails';
|
||||
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
||||
import { AdaptiveFilterContext } from '@/application/Context/State/Filter/AdaptiveFilterContext';
|
||||
import { FilterResultStub } from '@tests/unit/shared/Stubs/FilterResultStub';
|
||||
import { FilterStrategyStub } from '@tests/unit/shared/Stubs/FilterStrategyStub';
|
||||
import type { FilterStrategy } from '@/application/Context/State/Filter/Strategy/FilterStrategy';
|
||||
import { FilterResult } from '@/application/Context/State/Filter/Result/FilterResult';
|
||||
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
||||
|
||||
describe('AdaptiveFilterContext', () => {
|
||||
describe('clearFilter', () => {
|
||||
it('emits clear event on filter removal', () => {
|
||||
// arrange
|
||||
const expectedChange = FilterChangeDetailsStub.forClear();
|
||||
let actualChange: FilterChangeDetails | undefined;
|
||||
const context = new ContextBuilder().build();
|
||||
context.filterChanged.on((change) => {
|
||||
actualChange = change;
|
||||
});
|
||||
// act
|
||||
context.clearFilter();
|
||||
// assert
|
||||
expectExists(actualChange);
|
||||
expect(actualChange).to.deep.equal(expectedChange);
|
||||
});
|
||||
it('clears current filter', () => {
|
||||
// arrange
|
||||
const context = new ContextBuilder().build();
|
||||
// act
|
||||
context.applyFilter('non-important');
|
||||
context.clearFilter();
|
||||
// assert
|
||||
expect(context.currentFilter).to.be.equal(undefined);
|
||||
});
|
||||
});
|
||||
describe('applyFilter', () => {
|
||||
it('updates current filter correctly', () => {
|
||||
// arrange
|
||||
const expectedFilter = new FilterResultStub();
|
||||
const strategy = new FilterStrategyStub()
|
||||
.withApplyFilterResult(expectedFilter);
|
||||
const context = new ContextBuilder()
|
||||
.withStrategy(strategy)
|
||||
.build();
|
||||
// act
|
||||
context.applyFilter('non-important');
|
||||
// assert
|
||||
const actualFilter = context.currentFilter;
|
||||
expect(actualFilter).to.equal(expectedFilter);
|
||||
});
|
||||
it('emits apply event with correct filter', () => {
|
||||
// arrange
|
||||
const expectedFilter = new FilterResultStub();
|
||||
const strategy = new FilterStrategyStub()
|
||||
.withApplyFilterResult(expectedFilter);
|
||||
const context = new ContextBuilder()
|
||||
.withStrategy(strategy)
|
||||
.build();
|
||||
let actualFilter: FilterResult | undefined;
|
||||
context.filterChanged.on((filterResult) => {
|
||||
filterResult.visit({
|
||||
onApply: (result) => {
|
||||
actualFilter = result;
|
||||
},
|
||||
});
|
||||
});
|
||||
// act
|
||||
context.applyFilter('non-important');
|
||||
// assert
|
||||
expect(actualFilter).to.equal(expectedFilter);
|
||||
});
|
||||
it('applies filter using current collection', () => {
|
||||
// arrange
|
||||
const expectedCollection = new CategoryCollectionStub();
|
||||
const strategy = new FilterStrategyStub();
|
||||
const context = new ContextBuilder()
|
||||
.withStrategy(strategy)
|
||||
.withCategoryCollection(expectedCollection)
|
||||
.build();
|
||||
// act
|
||||
context.applyFilter('non-important');
|
||||
// assert
|
||||
const applyFilterCalls = strategy.callHistory.filter((c) => c.methodName === 'applyFilter');
|
||||
expect(applyFilterCalls).to.have.lengthOf(1);
|
||||
const [, actualCollection] = applyFilterCalls[0].args;
|
||||
expect(actualCollection).to.equal(expectedCollection);
|
||||
});
|
||||
it('applies filter with given query', () => {
|
||||
// arrange
|
||||
const expectedQuery = 'expected-query';
|
||||
const strategy = new FilterStrategyStub();
|
||||
const context = new ContextBuilder()
|
||||
.withStrategy(strategy)
|
||||
.build();
|
||||
// act
|
||||
context.applyFilter(expectedQuery);
|
||||
// assert
|
||||
const applyFilterCalls = strategy.callHistory.filter((c) => c.methodName === 'applyFilter');
|
||||
expect(applyFilterCalls).to.have.lengthOf(1);
|
||||
const [actualQuery] = applyFilterCalls[0].args;
|
||||
expect(actualQuery).to.equal(expectedQuery);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class ContextBuilder {
|
||||
private categoryCollection: ICategoryCollection = new CategoryCollectionStub();
|
||||
|
||||
private filterStrategy: FilterStrategy = new FilterStrategyStub();
|
||||
|
||||
public build(): AdaptiveFilterContext {
|
||||
return new AdaptiveFilterContext(
|
||||
this.categoryCollection,
|
||||
this.filterStrategy,
|
||||
);
|
||||
}
|
||||
|
||||
public withStrategy(strategy: FilterStrategy): this {
|
||||
this.filterStrategy = strategy;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withCategoryCollection(categoryCollection: ICategoryCollection): this {
|
||||
this.categoryCollection = categoryCollection;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,8 @@ import { FilterChange } from '@/application/Context/State/Filter/Event/FilterCha
|
||||
import { FilterResultStub } from '@tests/unit/shared/Stubs/FilterResultStub';
|
||||
import { FilterActionType } from '@/application/Context/State/Filter/Event/FilterActionType';
|
||||
import { FilterChangeDetailsVisitorStub } from '@tests/unit/shared/Stubs/FilterChangeDetailsVisitorStub';
|
||||
import { ApplyFilterAction } from '@/application/Context/State/Filter/Event/IFilterChangeDetails';
|
||||
import { ApplyFilterAction } from '@/application/Context/State/Filter/Event/FilterChangeDetails';
|
||||
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
||||
|
||||
describe('FilterChange', () => {
|
||||
describe('forApply', () => {
|
||||
@@ -48,14 +49,48 @@ describe('FilterChange', () => {
|
||||
});
|
||||
describe('visit', () => {
|
||||
describe('onClear', () => {
|
||||
itVisitsOnce(
|
||||
() => FilterChange.forClear(),
|
||||
);
|
||||
it('visits once', () => {
|
||||
// arrange
|
||||
const sut = FilterChange.forClear();
|
||||
const visitor = new FilterChangeDetailsVisitorStub();
|
||||
// act
|
||||
sut.visit(visitor);
|
||||
// assert
|
||||
expect(visitor.callHistory).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it('visits onClear', () => {
|
||||
// arrange
|
||||
const sut = FilterChange.forClear();
|
||||
const visitor = new FilterChangeDetailsVisitorStub();
|
||||
// act
|
||||
sut.visit(visitor);
|
||||
// assert
|
||||
const call = visitor.callHistory.find((c) => c.methodName === 'onClear');
|
||||
expect(call).toBeDefined();
|
||||
});
|
||||
});
|
||||
describe('onApply', () => {
|
||||
itVisitsOnce(
|
||||
() => FilterChange.forApply(new FilterResultStub()),
|
||||
);
|
||||
it('visits once', () => {
|
||||
// arrange
|
||||
const sut = FilterChange.forApply(new FilterResultStub());
|
||||
const visitor = new FilterChangeDetailsVisitorStub();
|
||||
// act
|
||||
sut.visit(visitor);
|
||||
// assert
|
||||
expect(visitor.callHistory).to.have.lengthOf(1);
|
||||
});
|
||||
|
||||
it('visits onApply', () => {
|
||||
// arrange
|
||||
const sut = FilterChange.forApply(new FilterResultStub());
|
||||
const visitor = new FilterChangeDetailsVisitorStub();
|
||||
// act
|
||||
sut.visit(visitor);
|
||||
// assert
|
||||
const call = visitor.callHistory.find((c) => c.methodName === 'onApply');
|
||||
expect(call).toBeDefined();
|
||||
});
|
||||
|
||||
it('visits with expected filter', () => {
|
||||
// arrange
|
||||
@@ -65,34 +100,11 @@ describe('FilterChange', () => {
|
||||
// act
|
||||
sut.visit(visitor);
|
||||
// assert
|
||||
expect(visitor.visitedResults).to.have.lengthOf(1);
|
||||
expect(visitor.visitedResults).to.include(expectedFilter);
|
||||
const call = visitor.callHistory.find((c) => c.methodName === 'onApply');
|
||||
expectExists(call);
|
||||
const [actualFilter] = call.args;
|
||||
expect(actualFilter).to.equal(expectedFilter);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function itVisitsOnce(sutFactory: () => FilterChange) {
|
||||
it('visits', () => {
|
||||
// arrange
|
||||
const sut = sutFactory();
|
||||
const expectedType = sut.action.type;
|
||||
const visitor = new FilterChangeDetailsVisitorStub();
|
||||
// act
|
||||
sut.visit(visitor);
|
||||
// assert
|
||||
expect(visitor.visitedEvents).to.include(expectedType);
|
||||
});
|
||||
it('visits once', () => {
|
||||
// arrange
|
||||
const sut = sutFactory();
|
||||
const expectedType = sut.action.type;
|
||||
const visitor = new FilterChangeDetailsVisitorStub();
|
||||
// act
|
||||
sut.visit(visitor);
|
||||
// assert
|
||||
expect(
|
||||
visitor.visitedEvents.filter((action) => action === expectedType),
|
||||
).to.have.lengthOf(1);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { FilterResult } from '@/application/Context/State/Filter/FilterResult';
|
||||
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
|
||||
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
|
||||
|
||||
describe('FilterResult', () => {
|
||||
describe('hasAnyMatches', () => {
|
||||
it('false when no matches', () => {
|
||||
const sut = new FilterResult(
|
||||
/* scriptMatches */ [],
|
||||
/* categoryMatches */ [],
|
||||
'query',
|
||||
);
|
||||
const actual = sut.hasAnyMatches();
|
||||
expect(actual).to.equal(false);
|
||||
});
|
||||
it('true when script matches', () => {
|
||||
const sut = new FilterResult(
|
||||
/* scriptMatches */ [new ScriptStub('id')],
|
||||
/* categoryMatches */ [],
|
||||
'query',
|
||||
);
|
||||
const actual = sut.hasAnyMatches();
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
it('true when category matches', () => {
|
||||
const sut = new FilterResult(
|
||||
/* scriptMatches */ [],
|
||||
/* categoryMatches */ [new CategoryStub(5)],
|
||||
'query',
|
||||
);
|
||||
const actual = sut.hasAnyMatches();
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
it('true when script + category matches', () => {
|
||||
const sut = new FilterResult(
|
||||
/* scriptMatches */ [new ScriptStub('id')],
|
||||
/* categoryMatches */ [new CategoryStub(5)],
|
||||
'query',
|
||||
);
|
||||
const actual = sut.hasAnyMatches();
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
|
||||
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
|
||||
import { AppliedFilterResult } from '@/application/Context/State/Filter/Result/AppliedFilterResult';
|
||||
import { ICategory, IScript } from '@/domain/ICategory';
|
||||
|
||||
describe('AppliedFilterResult', () => {
|
||||
describe('constructor', () => {
|
||||
it('initializes query correctly', () => {
|
||||
// arrange
|
||||
const expectedQuery = 'expected query';
|
||||
const builder = new ResultBuilder()
|
||||
.withQuery(expectedQuery);
|
||||
// act
|
||||
const result = builder.build();
|
||||
// assert
|
||||
const actualQuery = result.query;
|
||||
expect(actualQuery).to.equal(expectedQuery);
|
||||
});
|
||||
});
|
||||
describe('hasAnyMatches', () => {
|
||||
it('returns false with no matches', () => {
|
||||
// arrange
|
||||
const expected = false;
|
||||
const result = new ResultBuilder()
|
||||
.withScriptMatches([])
|
||||
.withCategoryMatches([])
|
||||
.build();
|
||||
// act
|
||||
const actual = result.hasAnyMatches();
|
||||
// assert
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
it('returns true with script matches', () => {
|
||||
// arrange
|
||||
const expected = true;
|
||||
const result = new ResultBuilder()
|
||||
.withScriptMatches([new ScriptStub('id')])
|
||||
.withCategoryMatches([])
|
||||
.build();
|
||||
// act
|
||||
const actual = result.hasAnyMatches();
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
it('returns true with category matches', () => {
|
||||
// arrange
|
||||
const expected = true;
|
||||
const result = new ResultBuilder()
|
||||
.withScriptMatches([])
|
||||
.withCategoryMatches([new CategoryStub(5)])
|
||||
.build();
|
||||
// act
|
||||
const actual = result.hasAnyMatches();
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
it('returns true with script and category matches', () => {
|
||||
// arrange
|
||||
const expected = true;
|
||||
const result = new ResultBuilder()
|
||||
.withScriptMatches([new ScriptStub('id')])
|
||||
.withCategoryMatches([new CategoryStub(5)])
|
||||
.build();
|
||||
// act
|
||||
const actual = result.hasAnyMatches();
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class ResultBuilder {
|
||||
private scriptMatches: readonly IScript[] = [new ScriptStub('id')];
|
||||
|
||||
private categoryMatches: readonly ICategory[] = [new CategoryStub(5)];
|
||||
|
||||
private query: string = `[${ResultBuilder.name}]query`;
|
||||
|
||||
public withScriptMatches(scriptMatches: readonly IScript[]): this {
|
||||
this.scriptMatches = scriptMatches;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withCategoryMatches(categoryMatches: readonly ICategory[]): this {
|
||||
this.categoryMatches = categoryMatches;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withQuery(query: string) {
|
||||
this.query = query;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): AppliedFilterResult {
|
||||
return new AppliedFilterResult(
|
||||
this.scriptMatches,
|
||||
this.categoryMatches,
|
||||
this.query,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
|
||||
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
|
||||
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
||||
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
||||
import { ICategory } from '@/domain/ICategory';
|
||||
import { IScript } from '@/domain/IScript';
|
||||
import { FilterResult } from '@/application/Context/State/Filter/Result/FilterResult';
|
||||
import { LinearFilterStrategy } from '@/application/Context/State/Filter/Strategy/LinearFilterStrategy';
|
||||
|
||||
describe('LinearFilterStrategy', () => {
|
||||
describe('applyFilter', () => {
|
||||
describe('query', () => {
|
||||
it('returns provided filter', () => {
|
||||
// arrange
|
||||
const expectedQuery = 'non matching filter';
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter(expectedQuery);
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expect(result.query).to.equal(expectedQuery);
|
||||
});
|
||||
});
|
||||
describe('hasAnyMatches', () => {
|
||||
it('returns false when there are no matches', () => {
|
||||
// arrange
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter('non matching filter')
|
||||
.withCollection(new CategoryCollectionStub());
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expect(result.hasAnyMatches()).be.equal(false);
|
||||
});
|
||||
it('returns true for a script match', () => {
|
||||
// arrange
|
||||
const matchingFilter = 'matching filter';
|
||||
const collection = new CategoryCollectionStub()
|
||||
.withAction(new CategoryStub(2).withScript(createMatchingScript(matchingFilter)));
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter(matchingFilter)
|
||||
.withCollection(collection);
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expect(result.hasAnyMatches()).be.equal(true);
|
||||
});
|
||||
it('returns true for a category match', () => {
|
||||
// arrange
|
||||
const matchingFilter = 'matching filter';
|
||||
const collection = new CategoryCollectionStub()
|
||||
.withAction(createMatchingCategory(matchingFilter));
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter(matchingFilter)
|
||||
.withCollection(collection);
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expect(result.hasAnyMatches()).be.equal(true);
|
||||
});
|
||||
it('returns true for script and category matches', () => {
|
||||
// arrange
|
||||
const matchingFilter = 'matching filter';
|
||||
const collection = new CategoryCollectionStub()
|
||||
.withAction(createMatchingCategory(matchingFilter))
|
||||
.withAction(new CategoryStub(2).withScript(createMatchingScript(matchingFilter)));
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter(matchingFilter)
|
||||
.withCollection(collection);
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expect(result.hasAnyMatches()).be.equal(true);
|
||||
});
|
||||
});
|
||||
describe('scriptMatches', () => {
|
||||
it('returns empty when there are no matches', () => {
|
||||
// arrange
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter('non matching filter')
|
||||
.withCollection(new CategoryCollectionStub());
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expect(result.scriptMatches).to.have.lengthOf(0);
|
||||
});
|
||||
describe('returns single matching script', () => {
|
||||
interface ScriptMatchTestScenario {
|
||||
readonly description: string;
|
||||
readonly filter: string;
|
||||
readonly matchingScript: IScript;
|
||||
}
|
||||
const testScenarios: readonly ScriptMatchTestScenario[] = [
|
||||
{
|
||||
description: 'case-insensitive code',
|
||||
filter: 'Hello WoRLD',
|
||||
matchingScript: new ScriptStub('id').withCode('HELLO world'),
|
||||
},
|
||||
{
|
||||
description: 'case-insensitive revert code',
|
||||
filter: 'Hello WoRLD',
|
||||
matchingScript: new ScriptStub('id').withRevertCode('HELLO world'),
|
||||
},
|
||||
{
|
||||
description: 'case-insensitive name',
|
||||
filter: 'Hello WoRLD',
|
||||
matchingScript: new ScriptStub('id').withName('HELLO world'),
|
||||
},
|
||||
{
|
||||
description: 'case-insensitive documentation',
|
||||
filter: 'MaTChing doC',
|
||||
matchingScript: new ScriptStub('id').withDocs(['unrelated docs', 'matching Docs']),
|
||||
},
|
||||
];
|
||||
testScenarios.forEach(({
|
||||
description, filter, matchingScript,
|
||||
}) => {
|
||||
it(description, () => {
|
||||
// arrange
|
||||
const expectedMatches = [matchingScript];
|
||||
const collection = new CategoryCollectionStub()
|
||||
.withAction(new CategoryStub(33).withScript(matchingScript));
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter(filter)
|
||||
.withCollection(collection);
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expectScriptMatches(result, expectedMatches);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('returns multiple matching scripts', () => {
|
||||
// arrange
|
||||
const filter = 'matching filter';
|
||||
const matchingScripts: readonly IScript[] = [
|
||||
createMatchingScript(filter),
|
||||
createMatchingScript(filter),
|
||||
];
|
||||
const expectedMatches = [...matchingScripts];
|
||||
const collection = new CategoryCollectionStub()
|
||||
.withAction(new CategoryStub(0).withScripts(...matchingScripts));
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter(filter)
|
||||
.withCollection(collection);
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expectScriptMatches(result, expectedMatches);
|
||||
});
|
||||
});
|
||||
describe('categoryMatches', () => {
|
||||
it('returns empty when there are no matches', () => {
|
||||
// arrange
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter('non matching filter')
|
||||
.withCollection(new CategoryCollectionStub());
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expect(result.categoryMatches).to.have.lengthOf(0);
|
||||
});
|
||||
describe('returns single matching category', () => {
|
||||
interface CategoryMatchTestScenario {
|
||||
readonly description: string;
|
||||
readonly filter: string;
|
||||
readonly matchingCategory: ICategory;
|
||||
}
|
||||
const testScenarios: readonly CategoryMatchTestScenario[] = [
|
||||
{
|
||||
description: 'match with case-insensitive name',
|
||||
filter: 'Hello WoRLD',
|
||||
matchingCategory: new CategoryStub(55).withName('HELLO world'),
|
||||
},
|
||||
{
|
||||
description: 'case-sensitive documentation',
|
||||
filter: 'Hello WoRLD',
|
||||
matchingCategory: new CategoryStub(55).withDocs(['unrelated-docs', 'HELLO world']),
|
||||
},
|
||||
];
|
||||
testScenarios.forEach(({
|
||||
description, filter, matchingCategory,
|
||||
}) => {
|
||||
it(description, () => {
|
||||
// arrange
|
||||
const expectedMatches = [matchingCategory];
|
||||
const collection = new CategoryCollectionStub()
|
||||
.withAction(matchingCategory);
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter(filter)
|
||||
.withCollection(collection);
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expectCategoryMatches(result, expectedMatches);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('returns multiple matching categories', () => {
|
||||
// arrange
|
||||
const filter = 'matching filter';
|
||||
const matchingCategories: readonly ICategory[] = [
|
||||
createMatchingCategory(filter),
|
||||
createMatchingCategory(filter),
|
||||
];
|
||||
const expectedMatches = [...matchingCategories];
|
||||
const collection = new CategoryCollectionStub()
|
||||
.withActions(...matchingCategories);
|
||||
const strategy = new FilterStrategyTestBuilder()
|
||||
.withFilter(filter)
|
||||
.withCollection(collection);
|
||||
// act
|
||||
const result = strategy.applyFilter();
|
||||
// assert
|
||||
expectCategoryMatches(result, expectedMatches);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function createMatchingScript(
|
||||
matchingFilter: string,
|
||||
): ScriptStub {
|
||||
return new ScriptStub('matching-script')
|
||||
.withCode(matchingFilter)
|
||||
.withName(matchingFilter);
|
||||
}
|
||||
|
||||
function createMatchingCategory(
|
||||
matchingFilter: string,
|
||||
): CategoryStub {
|
||||
return new CategoryStub(1)
|
||||
.withName(matchingFilter)
|
||||
.withDocs([matchingFilter]);
|
||||
}
|
||||
|
||||
function expectCategoryMatches(
|
||||
actualFilter: FilterResult,
|
||||
expectedMatches: readonly ICategory[],
|
||||
): void {
|
||||
expect(actualFilter.hasAnyMatches()).be.equal(true);
|
||||
expect(actualFilter.categoryMatches).to.have.lengthOf(expectedMatches.length);
|
||||
expect(actualFilter.categoryMatches).to.have.members(expectedMatches);
|
||||
}
|
||||
|
||||
function expectScriptMatches(
|
||||
actualFilter: FilterResult,
|
||||
expectedMatches: readonly IScript[],
|
||||
): void {
|
||||
expect(actualFilter.hasAnyMatches()).be.equal(true);
|
||||
expect(actualFilter.scriptMatches).to.have.lengthOf(expectedMatches.length);
|
||||
expect(actualFilter.scriptMatches).to.have.members(expectedMatches);
|
||||
}
|
||||
|
||||
class FilterStrategyTestBuilder {
|
||||
private filter: string = `[${FilterStrategyTestBuilder.name}]filter`;
|
||||
|
||||
private collection: ICategoryCollection = new CategoryCollectionStub();
|
||||
|
||||
public withCollection(collection: ICategoryCollection): this {
|
||||
this.collection = collection;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withFilter(filter: string): this {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public applyFilter(): ReturnType<LinearFilterStrategy['applyFilter']> {
|
||||
const strategy = new LinearFilterStrategy();
|
||||
return strategy.applyFilter(
|
||||
this.filter,
|
||||
this.collection,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,194 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { IFilterResult } from '@/application/Context/State/Filter/IFilterResult';
|
||||
import { UserFilter } from '@/application/Context/State/Filter/UserFilter';
|
||||
import { CategoryStub } from '@tests/unit/shared/Stubs/CategoryStub';
|
||||
import { ScriptStub } from '@tests/unit/shared/Stubs/ScriptStub';
|
||||
import { FilterChangeDetailsStub } from '@tests/unit/shared/Stubs/FilterChangeDetailsStub';
|
||||
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
|
||||
import { IFilterChangeDetails } from '@/application/Context/State/Filter/Event/IFilterChangeDetails';
|
||||
import { ICategoryCollection } from '@/domain/ICategoryCollection';
|
||||
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
||||
|
||||
describe('UserFilter', () => {
|
||||
describe('clearFilter', () => {
|
||||
it('signals when removing filter', () => {
|
||||
// arrange
|
||||
const expectedChange = FilterChangeDetailsStub.forClear();
|
||||
let actualChange: IFilterChangeDetails | undefined;
|
||||
const sut = new UserFilter(new CategoryCollectionStub());
|
||||
sut.filterChanged.on((change) => {
|
||||
actualChange = change;
|
||||
});
|
||||
// act
|
||||
sut.clearFilter();
|
||||
// assert
|
||||
expectExists(actualChange);
|
||||
expect(actualChange).to.deep.equal(expectedChange);
|
||||
});
|
||||
it('sets currentFilter to undefined', () => {
|
||||
// arrange
|
||||
const sut = new UserFilter(new CategoryCollectionStub());
|
||||
// act
|
||||
sut.applyFilter('non-important');
|
||||
sut.clearFilter();
|
||||
// assert
|
||||
expect(sut.currentFilter).to.be.equal(undefined);
|
||||
});
|
||||
});
|
||||
describe('applyFilter', () => {
|
||||
interface IApplyFilterTestCase {
|
||||
readonly name: string;
|
||||
readonly filter: string;
|
||||
readonly collection: ICategoryCollection;
|
||||
readonly assert: (result: IFilterResult) => void;
|
||||
}
|
||||
const testCases: readonly IApplyFilterTestCase[] = [
|
||||
(() => {
|
||||
const nonMatchingFilter = 'non matching filter';
|
||||
return {
|
||||
name: 'given no matches',
|
||||
filter: nonMatchingFilter,
|
||||
collection: new CategoryCollectionStub(),
|
||||
assert: (filter) => {
|
||||
expect(filter.hasAnyMatches()).be.equal(false);
|
||||
expect(filter.query).to.equal(nonMatchingFilter);
|
||||
},
|
||||
};
|
||||
})(),
|
||||
(() => {
|
||||
const code = 'HELLO world';
|
||||
const matchingFilter = 'Hello WoRLD';
|
||||
const script = new ScriptStub('id').withCode(code);
|
||||
return {
|
||||
name: 'given script match with case-insensitive code',
|
||||
filter: matchingFilter,
|
||||
collection: new CategoryCollectionStub()
|
||||
.withAction(new CategoryStub(33).withScript(script)),
|
||||
assert: (filter) => {
|
||||
expect(filter.hasAnyMatches()).be.equal(true);
|
||||
expect(filter.categoryMatches).to.have.lengthOf(0);
|
||||
expect(filter.scriptMatches).to.have.lengthOf(1);
|
||||
expect(filter.scriptMatches[0]).to.deep.equal(script);
|
||||
expect(filter.query).to.equal(matchingFilter);
|
||||
},
|
||||
};
|
||||
})(),
|
||||
(() => {
|
||||
const revertCode = 'HELLO world';
|
||||
const matchingFilter = 'Hello WoRLD';
|
||||
const script = new ScriptStub('id').withRevertCode(revertCode);
|
||||
return {
|
||||
name: 'given script match with case-insensitive revertCode',
|
||||
filter: matchingFilter,
|
||||
collection: new CategoryCollectionStub()
|
||||
.withAction(new CategoryStub(33).withScript(script)),
|
||||
assert: (filter) => {
|
||||
expect(filter.hasAnyMatches()).be.equal(true);
|
||||
expect(filter.categoryMatches).to.have.lengthOf(0);
|
||||
expect(filter.scriptMatches).to.have.lengthOf(1);
|
||||
expect(filter.scriptMatches[0]).to.deep.equal(script);
|
||||
expect(filter.query).to.equal(matchingFilter);
|
||||
},
|
||||
};
|
||||
})(),
|
||||
(() => {
|
||||
const name = 'HELLO world';
|
||||
const matchingFilter = 'Hello WoRLD';
|
||||
const script = new ScriptStub('id').withName(name);
|
||||
return {
|
||||
name: 'given script match with case-insensitive name',
|
||||
filter: matchingFilter,
|
||||
collection: new CategoryCollectionStub()
|
||||
.withAction(new CategoryStub(33).withScript(script)),
|
||||
assert: (filter) => {
|
||||
expect(filter.hasAnyMatches()).be.equal(true);
|
||||
expect(filter.categoryMatches).to.have.lengthOf(0);
|
||||
expect(filter.scriptMatches).to.have.lengthOf(1);
|
||||
expect(filter.scriptMatches[0]).to.deep.equal(script);
|
||||
expect(filter.query).to.equal(matchingFilter);
|
||||
},
|
||||
};
|
||||
})(),
|
||||
(() => {
|
||||
const categoryName = 'HELLO world';
|
||||
const matchingFilter = 'Hello WoRLD';
|
||||
const category = new CategoryStub(55).withName(categoryName);
|
||||
return {
|
||||
name: 'given category match with case-insensitive name',
|
||||
filter: matchingFilter,
|
||||
collection: new CategoryCollectionStub()
|
||||
.withAction(category),
|
||||
assert: (filter) => {
|
||||
expect(filter.hasAnyMatches()).be.equal(true);
|
||||
expect(filter.categoryMatches).to.have.lengthOf(1);
|
||||
expect(filter.categoryMatches[0]).to.deep.equal(category);
|
||||
expect(filter.scriptMatches).to.have.lengthOf(0);
|
||||
expect(filter.query).to.equal(matchingFilter);
|
||||
},
|
||||
};
|
||||
})(),
|
||||
(() => {
|
||||
const matchingText = 'HELLO world';
|
||||
const matchingFilter = 'Hello WoRLD';
|
||||
const script = new ScriptStub('script')
|
||||
.withName(matchingText);
|
||||
const category = new CategoryStub(55)
|
||||
.withName(matchingText)
|
||||
.withScript(script);
|
||||
return {
|
||||
name: 'given category and script matches with case-insensitive names',
|
||||
filter: matchingFilter,
|
||||
collection: new CategoryCollectionStub()
|
||||
.withAction(category),
|
||||
assert: (filter) => {
|
||||
expect(filter.hasAnyMatches()).be.equal(true);
|
||||
expect(filter.categoryMatches).to.have.lengthOf(1);
|
||||
expect(filter.categoryMatches[0]).to.deep.equal(category);
|
||||
expect(filter.scriptMatches).to.have.lengthOf(1);
|
||||
expect(filter.scriptMatches[0]).to.deep.equal(script);
|
||||
expect(filter.query).to.equal(matchingFilter);
|
||||
},
|
||||
};
|
||||
})(),
|
||||
];
|
||||
describe('sets currentFilter as expected', () => {
|
||||
testCases.forEach(({
|
||||
name, filter, collection, assert,
|
||||
}) => {
|
||||
it(name, () => {
|
||||
// arrange
|
||||
const sut = new UserFilter(collection);
|
||||
// act
|
||||
sut.applyFilter(filter);
|
||||
// assert
|
||||
const actual = sut.currentFilter;
|
||||
expectExists(actual);
|
||||
assert(actual);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('signals as expected', () => {
|
||||
testCases.forEach(({
|
||||
name, filter, collection, assert,
|
||||
}) => {
|
||||
it(name, () => {
|
||||
// arrange
|
||||
const sut = new UserFilter(collection);
|
||||
let actualFilterResult: IFilterResult | undefined;
|
||||
sut.filterChanged.on((filterResult) => {
|
||||
filterResult.visit({
|
||||
onApply: (result) => {
|
||||
actualFilterResult = result;
|
||||
},
|
||||
});
|
||||
});
|
||||
// act
|
||||
sut.applyFilter(filter);
|
||||
// assert
|
||||
expectExists(actualFilterResult);
|
||||
assert(actualFilterResult);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user