Skip to content

Commit

Permalink
Disable User Actions when userId is falsy SFINT-2453 (#26)
Browse files Browse the repository at this point in the history
* Disable the user actions component when user id is falsy 	SFINT-2453
  • Loading branch information
jeremierobert-coveo committed Aug 16, 2019
1 parent be05b86 commit 8285ba0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
54 changes: 39 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/components/UserActions/UserActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ export class UserActions extends Component {

this.options = ComponentOptions.initComponentOptions(element, UserActions, options);

if (!this.options.userId) {
this.disable();
return;
}

(get(this.root, UserProfileModel) as UserProfileModel)
.getActions(this.options.userId)
.then(actions => (actions.length > 0 ? this.render() : this.renderNoActions()))
Expand Down
22 changes: 21 additions & 1 deletion tests/components/UserActions/UserActions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Mock } from 'coveo-search-ui-tests';
import { UserActions } from '../../../src/components/UserActions/UserActions';
import { Logger, Initialization, QueryEvents } from 'coveo-search-ui';
import { createSandbox, SinonSandbox } from 'sinon';
import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import { UserAction } 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';

describe('UserActions', () => {
let sandbox: SinonSandbox;
Expand Down Expand Up @@ -53,6 +54,25 @@ describe('UserActions', () => {
Logger.enable();
});

it('should disable itself when the userId is falsy', () => {
const responsiveComponentStub = sandbox.stub(ResponsiveUserActions, 'init');
let getActionStub: SinonStub<[HTMLElement, UserActions], void>;

const mock = Mock.advancedComponentSetup<UserActions>(
UserActions,
new Mock.AdvancedComponentSetupOptions(null, { userId: '' }, env => {
getActionStub = fakeUserProfileModel(env.root, sandbox).getActions;
return env;
})
);

return delay(() => {
expect(getActionStub.called).toBe(false);
expect(responsiveComponentStub.called).toBe(false);
expect(mock.cmp.disabled).toBe(true);
});
});

it('should be hidden by defaut', () => {
sandbox.stub(Initialization, 'automaticallyCreateComponentsInside');

Expand Down

0 comments on commit 8285ba0

Please sign in to comment.