Skip to content

Commit

Permalink
fix(SFINT-3295): Add events for show and hide of UserActionsPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-bompart committed Jul 8, 2020
1 parent a3744ac commit 18631ff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/components/UserActions/UserActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export class UserActions extends Component {
* Identifier of the Search-UI component.
*/
static readonly ID = 'UserActions';
static readonly Events = {
Hide: 'userActionsPanelHide',
Show: 'userActionsPanelShow'
};

/**
* Default initialization options of the **UserActions** class.
Expand Down Expand Up @@ -156,33 +160,45 @@ export class UserActions extends Component {
(get(this.root, UserProfileModel) as UserProfileModel).deleteActions(this.options.userId);
this.root.classList.remove(UserActions.USER_ACTION_OPENED);
this.isOpened = false;
this.element.dispatchEvent(new CustomEvent(UserActions.Events.Hide));
}
}

/**
* Open the panel.
*/
public show() {
public async show() {
if (!this.isOpened) {
(get(this.root, UserProfileModel) as UserProfileModel)
.getActions(this.options.userId)
.then(actions => (actions.length > 0 ? this.render() : this.renderNoActions()))
.catch(e => (e && e.statusCode === 404 ? this.renderEnablePrompt() : this.renderNoActions()));

this.element.dispatchEvent(new CustomEvent(UserActions.Events.Show));
this.bindings.usageAnalytics.logCustomEvent({ name: 'openUserActions', type: 'User Actions' }, {}, this.element);
this.root.classList.add(UserActions.USER_ACTION_OPENED);
this.isOpened = true;

try {
const userActions = await (get(this.root, UserProfileModel) as UserProfileModel).getActions(this.options.userId);
if (userActions.length > 0) {
this.render();
} else {
this.renderNoActions();
}
} catch (e) {
if (e && e.statusCode === 404) {
this.renderEnablePrompt();
} else {
this.renderNoActions();
}
}
}
}

/**
* Toggle the visibility of the panel.
*/
public toggle() {
public async toggle() {
if (this.isOpened) {
this.hide();
} else {
this.show();
await this.show();
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/components/UserActions/UserActions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,13 @@ describe('UserActions', () => {
expect(modelMock.getActions.calledWithExactly(someUserId)).toBe(true);
});
});

it('should trigger a userActionsShow event', () => {
const spyDispatchEvent = sandbox.spy(mock.cmp.element, 'dispatchEvent');
mock.cmp.show();

expect(spyDispatchEvent.calledOnceWith(new CustomEvent('userActionsPanelHide')));
});
});

describe('hide', () => {
Expand Down Expand Up @@ -544,6 +551,15 @@ describe('UserActions', () => {
expect(modelMock.deleteActions.calledWithExactly(someUserId)).toBe(true);
});
});

it('should trigger a userActionsHide event', () => {
mock.cmp.show();
const spyDispatchEvent = sandbox.spy(mock.cmp.element, 'dispatchEvent');

mock.cmp.hide();

expect(spyDispatchEvent.calledOnceWith(new CustomEvent('userActionsPanelHide')));
});
});

describe('tagViewsOfUser', () => {
Expand Down

0 comments on commit 18631ff

Please sign in to comment.