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

new Events and localization for AttachResult and ResultsFilter components #28

Merged
merged 4 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 9 additions & 4 deletions src/components/AttachResult/AttachResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {
IAnalyticsCaseAttachMeta,
IAnalyticsCaseDetachMeta,
analyticsActionCauseList,
IAnalyticsActionCause
IAnalyticsActionCause,
l
} from 'coveo-search-ui';
import { paperclipIcon } from '../../utils/icons';
import { AttachResultEvents, IAttachResultEventArgs } from './Events';
import './Strings';

export interface IAttachResultOptions {
mikegron marked this conversation as resolved.
Show resolved Hide resolved
/** Specifies the tooltip displayed when the result is not attached. */
Expand All @@ -37,12 +40,12 @@ export class AttachResult extends Component {
private buttonElement: HTMLElement;
private tooltipElement: HTMLElement;

static options: IAttachResultOptions = {
public static readonly options: IAttachResultOptions = {
mikegron marked this conversation as resolved.
Show resolved Hide resolved
attachCaption: ComponentOptions.buildStringOption({
defaultValue: 'Attach Result'
defaultValue: l(`${AttachResult.ID}_DefaultAttachCaption`)
}),
detachCaption: ComponentOptions.buildStringOption({
defaultValue: 'Detach Result'
defaultValue: l(`${AttachResult.ID}_DefaultDetachCaption`)
}),
attach: ComponentOptions.buildCustomOption(
name => (result: IQueryResult) =>
Expand Down Expand Up @@ -111,6 +114,7 @@ export class AttachResult extends Component {
.then(() => {
this.attached = true;
this.logAnalyticsCaseEvent(analyticsActionCauseList.caseAttach);
Coveo.$$(this.root).trigger(AttachResultEvents.Attach, { queryResult: this.queryResult } as IAttachResultEventArgs);
})
.finally(() => {
this.setLoading(false);
Expand All @@ -131,6 +135,7 @@ export class AttachResult extends Component {
.then(() => {
this.attached = false;
this.logAnalyticsCaseEvent(analyticsActionCauseList.caseDetach);
Coveo.$$(this.root).trigger(AttachResultEvents.Detach, { queryResult: this.queryResult } as IAttachResultEventArgs);
})
.finally(() => {
this.setLoading(false);
Expand Down
10 changes: 10 additions & 0 deletions src/components/AttachResult/Events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IQueryResult } from 'coveo-search-ui';

export enum AttachResultEvents {
Attach = 'attach',
Detach = 'detach'
}

export interface IAttachResultEventArgs {
queryResult: IQueryResult;
}
6 changes: 6 additions & 0 deletions src/components/AttachResult/Strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Translation, Language } from '../../utils/translation';
mikegron marked this conversation as resolved.
Show resolved Hide resolved

Translation.register(Language.English, {
AttachResult_DefaultAttachCaption: 'Attach Result',
AttachResult_DefaultDetachCaption: 'Detach Result'
});
7 changes: 7 additions & 0 deletions src/components/ResultsFilter/Events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export enum ResultsFilterEvents {
Click = 'click'
}

export interface IResultsFilterEventArgs {
checked: boolean;
}
8 changes: 6 additions & 2 deletions src/components/ResultsFilter/ResultsFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import {
IBuildingQueryEventArgs,
QueryStateModel,
load,
IAttributesChangedEventArg
IAttributesChangedEventArg,
l
} from 'coveo-search-ui';
import { ResultsFilterEvents, IResultsFilterEventArgs } from './Events';
import './Strings';

export interface IAnalyticsFilteredResultsMeta {
mikegron marked this conversation as resolved.
Show resolved Hide resolved
filteredResults: boolean;
Expand All @@ -36,7 +39,7 @@ export class ResultsFilter extends Component {

static options: IResultsFilterOptions = {
text: ComponentOptions.buildStringOption({
defaultValue: 'Filter Results'
defaultValue: l(`${ResultsFilter.ID}_DefaultText`)
}),
field: ComponentOptions.buildStringOption({
defaultValue: 'urihash'
Expand Down Expand Up @@ -97,6 +100,7 @@ export class ResultsFilter extends Component {
private handleCheckboxChange(checkbox: Checkbox) {
this.queryStateModel.set(QueryStateModel.getFacetId(ResultsFilter.ID), this.checkbox.isSelected());
this.triggerQuery();
Coveo.$$(this.root).trigger(ResultsFilterEvents.Click, { checked: this.checkbox.isSelected() } as IResultsFilterEventArgs);
}

private triggerQuery() {
Expand Down
5 changes: 5 additions & 0 deletions src/components/ResultsFilter/Strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Translation, Language } from '../../utils/translation';
mikegron marked this conversation as resolved.
Show resolved Hide resolved

Translation.register(Language.English, {
ResultsFilter_DefaultText: 'Filter Results'
});
17 changes: 17 additions & 0 deletions tests/components/AttachResult/AttachResult.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AttachResult, IAttachResultOptions } from '../../../src/components/AttachResult/AttachResult';
import { Mock, Fake } from 'coveo-search-ui-tests';
import { IQueryResult, Logger } from 'coveo-search-ui';
import { AttachResultEvents, IAttachResultEventArgs } from '../../../src/components/AttachResult/Events';
mikegron marked this conversation as resolved.
Show resolved Hide resolved

describe('AttachResult', () => {
let attachResult: Mock.IBasicComponentSetup<AttachResult>;
Expand Down Expand Up @@ -129,6 +130,14 @@ describe('AttachResult', () => {
await attachResult.cmp.attach();
expect(attachResult.cmp.element.querySelector('.coveo-caption-for-icon').innerHTML).toBe('detach me');
});

it('should trigger the attach event', done => {
Coveo.$$(attachResult.env.root).on(AttachResultEvents.Attach, (evt: Event, args: IAttachResultEventArgs) => {
expect(args.queryResult).not.toBeNull();
done();
});
attachResult.cmp.attach();
});
});

describe('detach', () => {
Expand Down Expand Up @@ -186,6 +195,14 @@ describe('AttachResult', () => {
await attachResult.cmp.detach();
expect(attachResult.cmp.element.querySelector('.coveo-caption-for-icon').innerHTML).toBe('attach me');
});

it('should trigger the detach event', done => {
Coveo.$$(attachResult.env.root).on(AttachResultEvents.Detach, (evt: Event, args: IAttachResultEventArgs) => {
expect(args.queryResult).not.toBeNull();
done();
});
attachResult.cmp.detach();
});
});

describe('click', () => {
Expand Down
20 changes: 19 additions & 1 deletion tests/components/ResultsFilter/ResultsFilter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ResultsFilter, IResultsFilterOptions } from '../../../src/components/ResultsFilter/ResultsFilter';
import { Mock, Simulate } from 'coveo-search-ui-tests';
import { QueryStateModel } from 'coveo-search-ui';
import { QueryStateModel, Assert } from 'coveo-search-ui';
mikegron marked this conversation as resolved.
Show resolved Hide resolved
import { ResultsFilterEvents, IResultsFilterEventArgs } from '../../../src/components/ResultsFilter/Events';

describe('ResultsFilter', () => {
let filter: Mock.IBasicComponentSetup<ResultsFilter>;
Expand Down Expand Up @@ -42,6 +43,23 @@ describe('ResultsFilter', () => {
expect(filter.cmp.isSelected()).toBeFalsy();
});

it('should trigger events on toggling', done => {
mikegron marked this conversation as resolved.
Show resolved Hide resolved
let cpt = 0;
mikegron marked this conversation as resolved.
Show resolved Hide resolved
Coveo.$$(filter.env.root).on(ResultsFilterEvents.Click, (evt: Event, args: IResultsFilterEventArgs) => {
mikegron marked this conversation as resolved.
Show resolved Hide resolved
expect(evt.type).toBe(ResultsFilterEvents.Click);
if (args.checked) {
mikegron marked this conversation as resolved.
Show resolved Hide resolved
cpt++;
} else {
cpt++;
}
if (cpt == 2) {
done();
}
});
filter.cmp.toggle();
mikegron marked this conversation as resolved.
Show resolved Hide resolved
filter.cmp.toggle();
});

describe('when setting options', () => {
beforeEach(() => {
filter = Mock.optionsComponentSetup<ResultsFilter, IResultsFilterOptions>(ResultsFilter, {
Expand Down