Skip to content

Commit

Permalink
Sfint 2496 - Added falsey check for userId in DocumentClickedList Com…
Browse files Browse the repository at this point in the history
…ponent (#39)

* Added a potential fix for the bug SFINT-2496

* Added test case: userId is falsey SFINT-2496

* Extended scope to UserActivity and QueryList as they had the same issue SFINT-2496
  • Loading branch information
Neilerino authored and jeremierobert-coveo committed Sep 23, 2019
1 parent 5ab73fa commit 37f101a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/components/UserActions/ClickedDocumentList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export class ClickedDocumentList extends Component {

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

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

this.userProfileModel = get(this.root, UserProfileModel) as UserProfileModel;

this.userProfileModel.getActions(this.options.userId).then(actions => {
Expand Down
6 changes: 6 additions & 0 deletions src/components/UserActions/QueryList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ export class QueryList extends Component {
super(element, QueryList.ID, bindings);

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

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

this.userProfileModel = get(this.root, UserProfileModel) as UserProfileModel;
this.userProfileModel.getActions(this.options.userId).then(actions => {
this.sortedQueryList = [...actions]
Expand Down
6 changes: 6 additions & 0 deletions src/components/UserActions/UserActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export class UserActivity extends Component {
super(element, UserActivity.ID, bindings);

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

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

this.userProfileModel = get(this.root, UserProfileModel) as UserProfileModel;

this.userProfileModel.getActions(this.options.userId).then(actions => {
Expand Down
32 changes: 31 additions & 1 deletion tests/components/UserActions/ClickedDocumentList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSandbox, SinonSandbox } from 'sinon';
import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import { Mock, Fake } from 'coveo-search-ui-tests';
import { ClickedDocumentList } from '../../../src/components/UserActions/ClickedDocumentList';
import { UserProfileModel, UserAction } from '../../../src/models/UserProfileModel';
Expand Down Expand Up @@ -234,6 +234,36 @@ describe('ClickedDocumentList', () => {
});
});

it('Should disable itself when the userId is falsey', () => {
let getActionStub: SinonStub<[HTMLElement, ClickedDocumentList], void>;
const mock = Mock.advancedComponentSetup<ClickedDocumentList>(
ClickedDocumentList,
new Mock.AdvancedComponentSetupOptions(null, {userId: null}, env => {
getActionStub = fakeUserProfileModel(env.root, sandbox).getActions;
return env;
})
);
return delay(() => {
expect(getActionStub.called).toBe(false);
expect(mock.cmp.disabled).toBe(true);
});
});

it('Should disable itself when the userId is empty string', () => {
let getActionStub: SinonStub<[HTMLElement, ClickedDocumentList], void>;
const mock = Mock.advancedComponentSetup<ClickedDocumentList>(
ClickedDocumentList,
new Mock.AdvancedComponentSetupOptions(null, {userId: ''}, env => {
getActionStub = fakeUserProfileModel(env.root, sandbox).getActions;
return env;
})
);
return delay(() => {
expect(getActionStub.called).toBe(false);
expect(mock.cmp.disabled).toBe(true);
});
});

describe('template', () => {
it('should use the given template in options', () => {
sandbox.stub(Initialization, 'automaticallyCreateComponentsInsideResult');
Expand Down
32 changes: 31 additions & 1 deletion tests/components/UserActions/QueryList.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSandbox, SinonSandbox } from 'sinon';
import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import { Mock } from 'coveo-search-ui-tests';
import { QueryList } from '../../../src/components/UserActions/QueryList';
import { UserAction } from '../../../src/models/UserProfileModel';
Expand Down Expand Up @@ -292,5 +292,35 @@ describe('QueryList', () => {
expect(logSearchEventStub.args[0][0].type).toBe('User Actions');
});
});

it('Should disable itself when the userId is falsey', () => {
let getActionStub: SinonStub<[HTMLElement, QueryList], void>;
const mock = Mock.advancedComponentSetup<QueryList>(
QueryList,
new Mock.AdvancedComponentSetupOptions(null, {userId: null}, env => {
getActionStub = fakeUserProfileModel(env.root, sandbox).getActions;
return env;
})
);
return delay(() => {
expect(getActionStub.called).toBe(false);
expect(mock.cmp.disabled).toBe(true);
});
});

it('Should disable itself when the userId is empty string', () => {
let getActionStub: SinonStub<[HTMLElement, QueryList], void>;
const mock = Mock.advancedComponentSetup<QueryList>(
QueryList,
new Mock.AdvancedComponentSetupOptions(null, {userId: ''}, env => {
getActionStub = fakeUserProfileModel(env.root, sandbox).getActions;
return env;
})
);
return delay(() => {
expect(getActionStub.called).toBe(false);
expect(mock.cmp.disabled).toBe(true);
});
});
});
});
32 changes: 31 additions & 1 deletion tests/components/UserActions/UserActivity.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SinonSandbox, createSandbox } from 'sinon';
import { SinonSandbox, createSandbox, SinonStub } from 'sinon';
import { UserAction } from '../../../src/models/UserProfileModel';
import { Mock, Fake } from 'coveo-search-ui-tests';
import { UserActionType } from '../../../src/rest/UserProfilingEndpoint';
Expand Down Expand Up @@ -527,5 +527,35 @@ describe('UserActivity', () => {
expect(clickElement.querySelector<HTMLElement>('.coveo-footer').innerText).toMatch(FAKE_CUSTOM_EVENT.raw.origin_level_1);
});
});

it('Should disable itself when the userId is falsey', () => {
let getActionStub: SinonStub<[HTMLElement, UserActivity], void>;
const mock = Mock.advancedComponentSetup<UserActivity>(
UserActivity,
new Mock.AdvancedComponentSetupOptions(null, {userId: null}, env => {
getActionStub = fakeUserProfileModel(env.root, sandbox).getActions;
return env;
})
);
return delay(() => {
expect(getActionStub.called).toBe(false);
expect(mock.cmp.disabled).toBe(true);
});
});

it('Should disable itself when the userId is empty string', () => {
let getActionStub: SinonStub<[HTMLElement, UserActivity], void>;
const mock = Mock.advancedComponentSetup<UserActivity>(
UserActivity,
new Mock.AdvancedComponentSetupOptions(null, {userId: ''}, env => {
getActionStub = fakeUserProfileModel(env.root, sandbox).getActions;
return env;
})
);
return delay(() => {
expect(getActionStub.called).toBe(false);
expect(mock.cmp.disabled).toBe(true);
});
});
});
});

0 comments on commit 37f101a

Please sign in to comment.