From 859c491a34949590964c871f414adfc042587f8c Mon Sep 17 00:00:00 2001 From: Vito Albano Date: Tue, 1 Nov 2016 15:40:36 +0000 Subject: [PATCH] Added test for people component --- .../components/activiti-people.component.html | 12 +- .../activiti-people.component.spec.ts | 237 +++++++++++++++++- .../components/activiti-people.component.ts | 4 +- .../ng2-activiti-tasklist/src/i18n/it.json | 7 +- 4 files changed, 237 insertions(+), 23 deletions(-) diff --git a/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.html b/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.html index c2c359dec4..4104152578 100644 --- a/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.html +++ b/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.html @@ -1,4 +1,4 @@ -{{ 'TASK_DETAILS.LABELS.PEOPLE' | translate }}
add
@@ -9,7 +9,7 @@
  • face - {{user.firstName}} {{user.lastName}} + {{user.firstName}} {{user.lastName}} delete @@ -17,12 +17,12 @@
  • -
    +
    {{ 'TASK_DETAILS.PEOPLE.NONE' | translate }}
    - -

    Involve User

    + +

    Involve User

    - +
    diff --git a/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.spec.ts b/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.spec.ts index f8047fbef2..1c5737713f 100644 --- a/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.spec.ts +++ b/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.spec.ts @@ -15,36 +15,247 @@ * limitations under the License. */ -/* - import { - AlfrescoAuthenticationService, - AlfrescoSettingsService, - AlfrescoApiService - } from 'ng2-alfresco-core';*/ -import { AlfrescoTranslationService } from 'ng2-alfresco-core'; +import { + CoreModule, + AlfrescoTranslationService +} from 'ng2-alfresco-core'; import { ActivitiPeopleService } from '../services/activiti-people.service'; import { ActivitiPeople } from './activiti-people.component'; -import { ComponentFixture, TestBed, async } from '@angular/core/testing'; +import { ActivitiPeopleSearch } from './activiti-people-search.component'; +import { TranslationMock } from '../assets/translation.service.mock'; +import { ComponentFixture, TestBed, async, fakeAsync, tick } from '@angular/core/testing'; +import { User } from '../models/user.model'; + +declare let jasmine: any; + +const fakeUser: User = new User({ + id: 'fake-id', + firstName: 'fake-name', + lastName: 'fake-last', + email: 'fake@mail.com' +}); + +const fakeUserToInvolve: User = new User({ + id: 'fake-involve-id', + firstName: 'fake-involve-name', + lastName: 'fake-involve-last', + email: 'fake-involve@mail.com' +}); describe('Activiti People Component', () => { let activitiPeopleComponent: ActivitiPeople; let fixture: ComponentFixture; let element: HTMLElement; + let componentHandler; beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ActivitiPeople], - providers: [AlfrescoTranslationService, ActivitiPeopleService] + imports: [CoreModule], + declarations: [ActivitiPeople, ActivitiPeopleSearch], + providers: [ + {provide: AlfrescoTranslationService, useClass: TranslationMock}, + ActivitiPeopleService] }).compileComponents().then(() => { fixture = TestBed.createComponent(ActivitiPeople); activitiPeopleComponent = fixture.componentInstance; element = fixture.nativeElement; + componentHandler = jasmine.createSpyObj('componentHandler', [ + 'upgradeAllRegistered' + ]); + + window['componentHandler'] = componentHandler; }); })); - it('should not show any image if the user is not logged in', () => { - expect(element.querySelector('#userinfo_container')).toBeDefined(); - expect(element.querySelector('#logged-user-img')).toBeNull(); + afterAll(() => { + fixture.destroy(); + TestBed.resetTestingModule(); + }); + + it('should show people component title', () => { + expect(element.querySelector('#people-title')).toBeDefined(); + expect(element.querySelector('#people-title')).not.toBeNull(); + }); + + it('should show no people involved message', () => { + fixture.detectChanges(); + fixture.whenStable() + .then(() => { + expect(element.querySelector('#no-people-label')).not.toBeNull(); + expect(element.querySelector('#no-people-label').textContent).toContain('TASK_DETAILS.PEOPLE.NONE'); + }); + }); + + describe('when interact with people dialog', () => { + + beforeEach(() => { + activitiPeopleComponent.taskId = 'fake-task-id'; + activitiPeopleComponent.people = []; + fixture.detectChanges(); + }); + + it('should show dialog when clicked on add', () => { + expect(element.querySelector('#addPeople')).not.toBeNull(); + activitiPeopleComponent.showDialog(); + + expect(element.querySelector('#add-people-dialog')).not.toBeNull(); + expect(element.querySelector('#add-people-dialog-title')).not.toBeNull(); + expect(element.querySelector('#add-people-dialog-title').textContent).toContain('Involve User'); + }); + + it('should close dialog when clicked on cancel', () => { + activitiPeopleComponent.showDialog(); + expect(element.querySelector('#addPeople')).not.toBeNull(); + activitiPeopleComponent.cancel(); + let dialogWindow = element.querySelector('#add-people-dialog'); + expect(dialogWindow.getAttribute('open')).toBeNull(); + }); + }); + + describe('when there are involved people', () => { + + beforeEach(() => { + activitiPeopleComponent.taskId = 'fake-task-id'; + activitiPeopleComponent.people.push(fakeUser); + fixture.detectChanges(); + }); + + beforeEach(() => { + jasmine.Ajax.install(); + }); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + it('should show people involved', () => { + expect(element.querySelector('#user-fake-id')).not.toBeNull(); + expect(element.querySelector('#user-fake-id').textContent).toContain('fake-name'); + expect(element.querySelector('#user-fake-id').textContent).toContain('fake-last'); + }); + + it('should remove pepole involved', fakeAsync(() => { + activitiPeopleComponent.removeInvolvedUser(fakeUser); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200 + }); + tick(); + fixture.detectChanges(); + fixture.whenStable() + .then(() => { + expect(element.querySelector('#user-fake-id')).toBeNull(); + }); + })); + + it('should involve pepole', fakeAsync(() => { + activitiPeopleComponent.involveUser(fakeUserToInvolve); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200 + }); + tick(); + fixture.detectChanges(); + fixture.whenStable() + .then(() => { + expect(element.querySelector('#user-fake-involve-id')).not.toBeNull(); + expect(element.querySelector('#user-fake-involve-id').textContent) + .toBe('fake-involve-name fake-involve-last'); + }); + })); + + it('should return an observable with user search results', (done) => { + activitiPeopleComponent.people$.subscribe((users) => { + expect(users.length).toBe(2); + expect(users[0].firstName).toBe('fake-test-1'); + expect(users[0].lastName).toBe('fake-last-1'); + expect(users[0].email).toBe('fake-test-1@test.com'); + expect(users[0].id).toBe(1); + done(); + }); + activitiPeopleComponent.searchUser('fake-search-word'); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: { + data: [{ + id: 1, + firstName: 'fake-test-1', + lastName: 'fake-last-1', + email: 'fake-test-1@test.com' + }, { + id: 2, + firstName: 'fake-test-2', + lastName: 'fake-last-2', + email: 'fake-test-2@test.com' + }] + } + }); + }); + + it('should return an empty list for not valid search', (done) => { + activitiPeopleComponent.people$.subscribe((users) => { + expect(users.length).toBe(0); + done(); + }); + activitiPeopleComponent.searchUser('fake-search-word'); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + contentType: 'json', + responseText: {} + }); + }); + }); + + describe('when there are errors on service call', () => { + + beforeEach(() => { + jasmine.Ajax.install(); + }); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + it('should log error message when search fails', fakeAsync(() => { + console.log = jasmine.createSpy('log'); + activitiPeopleComponent.searchUser('fake-search'); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 403 + }); + tick(); + + expect(console.log).toHaveBeenCalledWith('Could not load users'); + })); + + it('should not remove user if remove involved user fail', fakeAsync(() => { + activitiPeopleComponent.people.push(fakeUser); + fixture.detectChanges(); + activitiPeopleComponent.removeInvolvedUser(fakeUser); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 403 + }); + tick(); + fixture.detectChanges(); + fixture.whenStable() + .then(() => { + expect(element.querySelector('#user-fake-id')).not.toBeNull(); + expect(element.querySelector('#user-fake-id').textContent) + .toBe('fake-name fake-last'); + }); + })); + + it('should not involve user if involve user fail', fakeAsync(() => { + activitiPeopleComponent.involveUser(fakeUserToInvolve); + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 403 + }); + tick(); + fixture.detectChanges(); + fixture.whenStable() + .then(() => { + expect(element.querySelector('#user-fake-id')).toBeNull(); + expect(element.querySelector('#no-people-label').textContent).toContain('TASK_DETAILS.PEOPLE.NONE'); + }); + })); }); }); diff --git a/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.ts b/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.ts index 5795275b45..f8b57d7762 100644 --- a/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.ts +++ b/ng2-components/ng2-activiti-tasklist/src/components/activiti-people.component.ts @@ -39,7 +39,7 @@ export class ActivitiPeople { dialog: any; private peopleObserver: Observer; - people$: Observable; + people$: Observable; /** * Constructor @@ -52,7 +52,7 @@ export class ActivitiPeople { if (translate) { translate.addTranslationFolder('node_modules/ng2-activiti-tasklist/src'); } - this.people$ = new Observable(observer => this.peopleObserver = observer).share(); + this.people$ = new Observable(observer => this.peopleObserver = observer).share(); } public showDialog() { diff --git a/ng2-components/ng2-activiti-tasklist/src/i18n/it.json b/ng2-components/ng2-activiti-tasklist/src/i18n/it.json index 62021c250d..66528d70dc 100644 --- a/ng2-components/ng2-activiti-tasklist/src/i18n/it.json +++ b/ng2-components/ng2-activiti-tasklist/src/i18n/it.json @@ -8,7 +8,10 @@ "LABELS": { "ASSIGNEE": "Assegnatario", "DUE": "Scadenza", - "FORM": "Form" + "FORM": "Form", + "PEOPLE": "Persone", + "COMMENTS": "Commenti", + "CHECKLIST": "Checklist" }, "MESSAGES": { "NONE": "Nessun dettaglio task trovato." @@ -25,4 +28,4 @@ "NONE": "Nessun filtro task selezionato." } } -} \ No newline at end of file +}