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

Viewed By Customer #20

Merged
merged 7 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions img/user.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7,708 changes: 3,115 additions & 4,593 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/jasmine": "^3.3.9",
"@types/node": "^11.11.3",
"@types/sinon": "^7.0.13",
"coveo-search-ui": "^2.6459.0",
"coveo-search-ui": "2.0.342",
louis-bompart marked this conversation as resolved.
Show resolved Hide resolved
"coveo-search-ui-tests": "0.0.5",
"coveralls": "^3.0.3",
"istanbul-instrumenter-loader": "^3.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export { ClickedDocumentList } from './components/UserActions/ClickedDocumentLis
export { QueryList } from './components/UserActions/QueryList';
export { UserProfilingEndpoint } from './rest/UserProfilingEndpoint';
export { ResultsFilter } from './components/ResultsFilter/ResultsFilter';
export { ViewedByCustomer } from './components/ViewedByCustomer/ViewedByCustomer';
5 changes: 5 additions & 0 deletions src/components/ViewedByCustomer/Strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Translation, Language } from '../../utils/translation';

Translation.register(Language.English, {
ViewedByCustomer_DefaultLabel: 'Viewed by Customer'
});
18 changes: 18 additions & 0 deletions src/components/ViewedByCustomer/ViewedByCustomer.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@import '../../sass/Variables.scss';
@import '../../sass/Text.scss';

.CoveoViewedByCustomer {
color: black;
font-weight: bold;
font-size: 14px;
jeremierobert-coveo marked this conversation as resolved.
Show resolved Hide resolved
.viewed-by-customer-icon {
vertical-align: middle;
svg {
height: 1em;
width: 1em;
}
}
.viewed-by-customer-label {
vertical-align: baseline;
}
}
70 changes: 70 additions & 0 deletions src/components/ViewedByCustomer/ViewedByCustomer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Component, ComponentOptions, IComponentBindings, Initialization, l } from 'coveo-search-ui';
import { user } from '../../utils/icons';
import './Strings';

/**
* The options for the ViewedByCustomerComponent
*/
export interface IViewedByCustomerOptions {
/**
* If true, will display an icon when the component is displayed itself.
*/
showIcon?: boolean;
/**
* The label that should be displayed when the component is displayed.
*/
label?: string;
}

/**
* Result component indicating if a search result have been viewed by the targeted user.
*/
export class ViewedByCustomer extends Component {
/**
* Unique Identifier used by the Search-UI.
*/
public static readonly ID = 'ViewedByCustomer';

/**
* Default options used by the component.
*/
public static readonly options: IViewedByCustomerOptions = {
showIcon: ComponentOptions.buildBooleanOption({ defaultValue: true }),
louis-bompart marked this conversation as resolved.
Show resolved Hide resolved
label: ComponentOptions.buildStringOption({ defaultValue: l(`${ViewedByCustomer.ID}_DefaultLabel`) })
};

// Internal CSS selectors.
private static readonly ICON_CLASS = 'viewed-by-customer-icon';
private static readonly LABEL_CLASS = 'viewed-by-customer-label';

/**
* Create an instance of {@link ViewedByCustomer}.
* @param element Element on which to bind the component.
* @param options Initialization options of the component.
* @param bindings Bindings of the Search-UI environment.
*/
public constructor(public element: HTMLElement, public options: IViewedByCustomerOptions, public bindings: IComponentBindings) {
super(element, ViewedByCustomer.ID, bindings) /* istanbul ignore next Issue with Istanbul and super calls*/;
this.options = ComponentOptions.initComponentOptions(element, ViewedByCustomer, options);

if (this.resolveResult().isUserActionView) {
this.render();
}
}

private render() {
if (this.options.showIcon) {
const iconElement = document.createElement('span');
iconElement.classList.add(ViewedByCustomer.ICON_CLASS);
iconElement.innerHTML = user;
this.element.appendChild(iconElement);
}

const labelElement = document.createElement('span');
labelElement.classList.add(ViewedByCustomer.LABEL_CLASS);
labelElement.innerText = this.options.label;
this.element.appendChild(labelElement);
}
}

Initialization.registerAutoCreateComponent(ViewedByCustomer);
1 change: 1 addition & 0 deletions src/sass/Index.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@import '../components/AttachResult/AttachResult.scss';
@import '../components/UserActions/UserActions.scss';
@import '../components/ViewedByCustomer/ViewedByCustomer.scss';
2 changes: 2 additions & 0 deletions src/utils/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import * as SEARCH from '../../img/search.svg';
import * as VIEW from '../../img/view.svg';
import * as DOT from '../../img/dot.svg';
import * as PAPER_CLIP from '../../img/paperclip.svg';
import * as USER from '../../img/user.svg';

export const arrowDown = ARROW_DOWN;
export const duplicate = DUPLICATE;
export const search = SEARCH;
export const view = VIEW;
export const dot = DOT;
export const paperclipIcon = PAPER_CLIP;
export const user = USER;
49 changes: 49 additions & 0 deletions tests/components/ViewedByCustomer/ViewedByCustomer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ViewedByCustomer } from '../../../src/Index';
louis-bompart marked this conversation as resolved.
Show resolved Hide resolved
import { Mock, Fake } from 'coveo-search-ui-tests';
import { IViewedByCustomerOptions } from '../../../src/components/ViewedByCustomer/ViewedByCustomer';
import { IQueryResult } from 'coveo-search-ui';
import { AdvancedComponentSetupOptions } from 'coveo-search-ui-tests/MockEnvironment';

describe('ViewedByCustomer', () => {
const LABEL_CSS_CLASS = 'viewed-by-customer-label';
const ICON_CSS_CLASS = 'viewed-by-customer-icon';

describe('when the field isUserActionView of the result true', () => {
const fakeResult: IQueryResult = { ...Fake.createFakeResult(), isUserActionView: true };

it('should display the default label and the icon when no options are given', () => {
const testComponent = Mock.advancedResultComponentSetup<ViewedByCustomer>(ViewedByCustomer, fakeResult).cmp;
expect(testComponent.element.getElementsByClassName(ICON_CSS_CLASS).length).toBe(1);
expect(testComponent.element.getElementsByClassName(LABEL_CSS_CLASS).length).toBe(1);
expect((testComponent.element.getElementsByClassName(LABEL_CSS_CLASS).item(0) as HTMLSpanElement).innerText).toBe('Viewed by Customer');
});

it('should display the label given in the option if any is given', () => {
const option = new Mock.AdvancedComponentSetupOptions();
(option.cmpOptions as IViewedByCustomerOptions).label = 'test label';
const testComponent = Mock.advancedResultComponentSetup<ViewedByCustomer>(ViewedByCustomer, fakeResult, option).cmp;
expect(testComponent.element.getElementsByClassName(LABEL_CSS_CLASS).length).toBe(1);
expect(testComponent.element.getElementsByClassName(LABEL_CSS_CLASS).item(0).innerHTML).toBe('test label');
});

it('should hide the icon if showIcon option is set to false', () => {
const option = new Mock.AdvancedComponentSetupOptions();
(option.cmpOptions as IViewedByCustomerOptions).showIcon = false;
const testComponent = Mock.advancedResultComponentSetup<ViewedByCustomer>(ViewedByCustomer, fakeResult, option).cmp;
expect(testComponent.element.getElementsByClassName(ICON_CSS_CLASS).length).toBe(0);
});
});

[false, undefined, null].forEach(falsyValue => {
describe(`when the field isUserActionView of the result is ${falsyValue}`, () => {
const fakeResult: IQueryResult = { ...Fake.createFakeResult(), isUserActionView: falsyValue };

it('should not display the component or add anything to its own DOM', () => {
const option: AdvancedComponentSetupOptions = new Mock.AdvancedComponentSetupOptions();
(option.cmpOptions as IViewedByCustomerOptions).showIcon = false;
const testComponent = Mock.advancedResultComponentSetup<ViewedByCustomer>(ViewedByCustomer, fakeResult, option).cmp;
expect(testComponent.element.children.length).toBe(0);
});
});
});
});