Skip to content

Commit

Permalink
Merge 58dc8df into 0b40b14
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanlb committed Oct 22, 2019
2 parents 0b40b14 + 58dc8df commit 2cfa434
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/components/UserActions/Strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Translation, Language } from '../../utils/translation';

Translation.register(Language.English, {
UserActions_no_actions: 'No actions available for this user',
UserActions_enable_prompt: 'The User Action feature is not activated for your organization.\nTo activate it, contact Coveo Support.',

QueryList_more: 'Show More',
QueryList_less: 'Show Less',
Expand Down
14 changes: 13 additions & 1 deletion src/components/UserActions/UserActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ export class UserActions extends Component {
(get(this.root, UserProfileModel) as UserProfileModel)
.getActions(this.options.userId)
.then(actions => (actions.length > 0 ? this.render() : this.renderNoActions()))
.catch(() => this.renderNoActions());
.catch(e => {
e.statusCode === 404 ? this.renderEnablePrompt() : this.renderNoActions();
});

this.bindings.usageAnalytics.logCustomEvent({ name: 'openUserActions', type: 'User Actions' }, {}, this.element);
this.root.classList.add(UserActions.USER_ACTION_OPENED);
Expand Down Expand Up @@ -264,6 +266,16 @@ export class UserActions extends Component {
this.element.appendChild(element);
}

private renderEnablePrompt() {
const element = document.createElement('div');
element.classList.add('coveo-no-actions');
element.innerText = l(`${UserActions.ID}_enable_prompt`);
element.style.padding = '1.5em';

this.element.innerHTML = '';
this.element.appendChild(element);
}

private showViewedByCustomer() {
this.bind.onRootElement(ResultListEvents.newResultDisplayed, (args: IDisplayedNewResultEventArgs) => {
if (Boolean(args.item.getElementsByClassName('CoveoViewedByCustomer').length)) {
Expand Down
25 changes: 22 additions & 3 deletions tests/components/UserActions/UserActions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Mock, Fake, Simulate } from 'coveo-search-ui-tests';
import { UserActions } from '../../../src/components/UserActions/UserActions';
import { Logger, Initialization, QueryEvents, ResultListEvents, IQueryResult } from 'coveo-search-ui';
import { Logger, Initialization, QueryEvents, ResultListEvents, IQueryResult, l } from 'coveo-search-ui';
import { createSandbox, SinonSandbox, SinonStub, SinonStubbedInstance } from 'sinon';
import { UserAction, UserProfileModel } from '../../../src/models/UserProfileModel';
import { delay, fakeUserProfileModel } from '../../utils';
import { ClickedDocumentList, QueryList, UserActivity } from '../../../src/Index';
import { UserActionType } from '../../../src/rest/UserProfilingEndpoint';
import { ResponsiveUserActions } from '../../../src/components/UserActions/ResponsiveUserActions';
import '../../../src/components/UserActions/Strings';

describe('UserActions', () => {
let sandbox: SinonSandbox;
Expand Down Expand Up @@ -190,6 +191,24 @@ describe('UserActions', () => {
expect(hideSpy.called).toBe(true);
});

it('should show a message when user actions is not enabled', () => {
sandbox.stub(Initialization, 'automaticallyCreateComponentsInside');

const mock = Mock.advancedComponentSetup<UserActions>(
UserActions,
new Mock.AdvancedComponentSetupOptions(null, { userId: 'testuserId' }, env => {
fakeUserProfileModel(env.root, sandbox).getActions.returns(Promise.reject({ statusCode: 404 }));
return env;
})
);
mock.cmp.show();

return delay(() => {
expect(mock.cmp.element.querySelector<HTMLElement>('.coveo-no-actions')).not.toBeNull();
expect(mock.cmp.element.querySelector<HTMLElement>('.coveo-no-actions').innerText).toBe(l(`${UserActions.ID}_enable_prompt`).replace("\n", ""));
});
});

it('should show a message when no actions are available', () => {
sandbox.stub(Initialization, 'automaticallyCreateComponentsInside');

Expand All @@ -204,7 +223,7 @@ describe('UserActions', () => {

return delay(() => {
expect(mock.cmp.element.querySelector<HTMLElement>('.coveo-no-actions')).not.toBeNull();
expect(mock.cmp.element.querySelector<HTMLElement>('.coveo-no-actions').innerText).toBe('No actions available for this user');
expect(mock.cmp.element.querySelector<HTMLElement>('.coveo-no-actions').innerText).toBe(l(`${UserActions.ID}_no_actions`));
});
});

Expand All @@ -221,7 +240,7 @@ describe('UserActions', () => {
mock.cmp.show();

return delay(() => {
expect(mock.cmp.element.querySelector<HTMLElement>('.coveo-no-actions').innerText).toBe('No actions available for this user');
expect(mock.cmp.element.querySelector<HTMLElement>('.coveo-no-actions').innerText).toBe(l(`${UserActions.ID}_no_actions`));
});
});

Expand Down

0 comments on commit 2cfa434

Please sign in to comment.