Commit 51eac1ba26efdcb81902fa378ebed7b95e555973
1 parent
ef871ad6
Add tests to task components
Showing
11 changed files
with
301 additions
and
17 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,29 @@ |
| 1 | +import { Provider, provide, Component } from 'ng-forward'; | |
| 2 | +import * as helpers from "../../../spec/helpers"; | |
| 3 | +import { TaskAcceptComponent } from './task-accept.component'; | |
| 4 | + | |
| 5 | +const htmlTemplate: string = '<task-accept [task]="ctrl.task"></task-accept>'; | |
| 6 | + | |
| 7 | +describe("Components", () => { | |
| 8 | + describe("Task Accept Component", () => { | |
| 9 | + | |
| 10 | + let task = { id: 1, type: "AddMember" }; | |
| 11 | + | |
| 12 | + beforeEach(angular.mock.module("templates")); | |
| 13 | + | |
| 14 | + function createComponent() { | |
| 15 | + return helpers.quickCreateComponent({ | |
| 16 | + template: htmlTemplate, | |
| 17 | + directives: [TaskAcceptComponent], | |
| 18 | + properties: { task: task } | |
| 19 | + }); | |
| 20 | + } | |
| 21 | + | |
| 22 | + it("replace element with the specific task accept component", (done: Function) => { | |
| 23 | + createComponent().then(fixture => { | |
| 24 | + expect(fixture.debugElement.queryAll("add-member-task-accept").length).toBe(1); | |
| 25 | + done(); | |
| 26 | + }); | |
| 27 | + }); | |
| 28 | + }); | |
| 29 | +}); | ... | ... |
| ... | ... | @@ -0,0 +1,104 @@ |
| 1 | +import { Provider, provide, Component } from 'ng-forward'; | |
| 2 | +import * as helpers from "../../../spec/helpers"; | |
| 3 | +import { ComponentTestHelper, createClass } from '../../../spec/component-test-helper'; | |
| 4 | +import { TaskListComponent } from './task-list.component'; | |
| 5 | + | |
| 6 | +const htmlTemplate: string = '<task-list [task]="ctrl.task"></task-list>'; | |
| 7 | + | |
| 8 | +describe("Components", () => { | |
| 9 | + describe("Task List Component", () => { | |
| 10 | + | |
| 11 | + let helper: ComponentTestHelper<TaskListComponent>; | |
| 12 | + let taskService = jasmine.createSpyObj("taskService", ["getAllPending"]); | |
| 13 | + let tasks = [{ id: 1 }, { id: 2 }]; | |
| 14 | + let modal = helpers.mocks.$modal; | |
| 15 | + taskService.getAllPending = jasmine.createSpy("getAllPending").and.returnValue(Promise.resolve({ headers: () => { }, data: tasks })); | |
| 16 | + | |
| 17 | + beforeEach(angular.mock.module("templates")); | |
| 18 | + | |
| 19 | + beforeEach((done) => { | |
| 20 | + let cls = createClass({ | |
| 21 | + template: htmlTemplate, | |
| 22 | + directives: [TaskListComponent], | |
| 23 | + providers: [ | |
| 24 | + helpers.createProviderToValue("TaskService", taskService), | |
| 25 | + helpers.createProviderToValue('NotificationService', helpers.mocks.notificationService), | |
| 26 | + helpers.createProviderToValue('$uibModal', modal), | |
| 27 | + ].concat(helpers.provideFilters("groupByFilter")), | |
| 28 | + properties: { tasks: tasks } | |
| 29 | + }); | |
| 30 | + helper = new ComponentTestHelper<TaskListComponent>(cls, done); | |
| 31 | + }); | |
| 32 | + | |
| 33 | + it("return specific template for a task", () => { | |
| 34 | + let task = { type: "AddMember" }; | |
| 35 | + expect(helper.component.getTaskTemplate(<any>task)).toEqual("app/task/types/add-member/add-member.html"); | |
| 36 | + }); | |
| 37 | + | |
| 38 | + it("return the default template for a task", () => { | |
| 39 | + let task = { type: "" }; | |
| 40 | + expect(helper.component.getTaskTemplate(<any>task)).toEqual("app/task/types/default.html"); | |
| 41 | + }); | |
| 42 | + | |
| 43 | + it("open confirmation modal when it has details to accept a task", () => { | |
| 44 | + let task = { accept_details: true }; | |
| 45 | + helper.component.accept(<any>task); | |
| 46 | + expect(modal.open).toHaveBeenCalled(); | |
| 47 | + }); | |
| 48 | + | |
| 49 | + it("open confirmation modal when it has details to reject a task", () => { | |
| 50 | + let task = { reject_details: true }; | |
| 51 | + helper.component.reject(<any>task); | |
| 52 | + expect(modal.open).toHaveBeenCalled(); | |
| 53 | + }); | |
| 54 | + | |
| 55 | + it("call api directly when it has no details to accept a task", () => { | |
| 56 | + let task = { accept_details: false }; | |
| 57 | + helper.component.callAccept = jasmine.createSpy("callAccept"); | |
| 58 | + helper.component.accept(<any>task); | |
| 59 | + expect(helper.component.callAccept).toHaveBeenCalled(); | |
| 60 | + }); | |
| 61 | + | |
| 62 | + it("call api directly when it has no details to reject a task", () => { | |
| 63 | + let task = { accept_details: false }; | |
| 64 | + helper.component.callReject = jasmine.createSpy("callReject"); | |
| 65 | + helper.component.reject(<any>task); | |
| 66 | + expect(helper.component.callReject).toHaveBeenCalled(); | |
| 67 | + }); | |
| 68 | + | |
| 69 | + it("call cancel and remove the current task when accept was called successfully", () => { | |
| 70 | + helper.component.currentTask = <any>{ id: 1 }; | |
| 71 | + let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); | |
| 72 | + taskService.finishTask = jasmine.createSpy("finishTask").and.returnValue(result); | |
| 73 | + helper.component.cancel = jasmine.createSpy("cancel"); | |
| 74 | + helper.component.callAccept(); | |
| 75 | + expect(helper.component.cancel).toHaveBeenCalled(); | |
| 76 | + expect(helper.component.tasks).toEqual([{ id: 2 }]); | |
| 77 | + }); | |
| 78 | + | |
| 79 | + it("call cancel and remove the current task when reject was called successfully", () => { | |
| 80 | + helper.component.currentTask = <any>{ id: 1 }; | |
| 81 | + let result = helpers.mocks.promiseResultTemplate({ data: { id: 1 } }); | |
| 82 | + taskService.cancelTask = jasmine.createSpy("cancelTask").and.returnValue(result); | |
| 83 | + helper.component.cancel = jasmine.createSpy("cancel"); | |
| 84 | + helper.component.callReject(); | |
| 85 | + expect(helper.component.cancel).toHaveBeenCalled(); | |
| 86 | + expect(helper.component.tasks).toEqual([{ id: 2 }]); | |
| 87 | + }); | |
| 88 | + | |
| 89 | + it("reset currentTask and close modal when call cancel", () => { | |
| 90 | + let modalInstance = jasmine.createSpyObj("modalInstance", ["close"]); | |
| 91 | + helper.component["modalInstance"] = modalInstance; | |
| 92 | + helper.component.currentTask = <any>{ id: 1 }; | |
| 93 | + helper.component.cancel(); | |
| 94 | + expect(modalInstance.close).toHaveBeenCalled(); | |
| 95 | + expect(helper.component.currentTask).toBeNull(); | |
| 96 | + }); | |
| 97 | + | |
| 98 | + it("not fail when call cancel with no modalInstance", () => { | |
| 99 | + helper.component["modalInstance"] = null; | |
| 100 | + helper.component.currentTask = null; | |
| 101 | + helper.component.cancel(); | |
| 102 | + }); | |
| 103 | + }); | |
| 104 | +}); | ... | ... |
src/app/task/task-list/task-list.component.ts
| ... | ... | @@ -89,11 +89,11 @@ export class TaskListComponent { |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | private getTemplateName(task: noosfero.Task) { |
| 92 | - return task.type.replace(/::/, '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase()); | |
| 92 | + return task.type.replace(/::/, '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | private removeTask(task: noosfero.Task) { |
| 96 | - let index = this.tasks.indexOf(task, 0); | |
| 96 | + let index = this.tasks.map((t: noosfero.Task) => { return t.id; }).indexOf(task.id); | |
| 97 | 97 | if (index > -1) { |
| 98 | 98 | this.tasks.splice(index, 1); |
| 99 | 99 | } | ... | ... |
| ... | ... | @@ -0,0 +1,42 @@ |
| 1 | +import { Provider, provide, Component } from 'ng-forward'; | |
| 2 | +import * as helpers from "../../../spec/helpers"; | |
| 3 | +import { ComponentTestHelper, createClass } from '../../../spec/component-test-helper'; | |
| 4 | +import { TasksMenuComponent } from './tasks-menu.component'; | |
| 5 | +import { AuthEvents } from "./../../login"; | |
| 6 | + | |
| 7 | +const htmlTemplate: string = '<tasks-menu></tasks-menu>'; | |
| 8 | + | |
| 9 | +describe("Components", () => { | |
| 10 | + describe("Task Menu Component", () => { | |
| 11 | + | |
| 12 | + let helper: ComponentTestHelper<TasksMenuComponent>; | |
| 13 | + let taskService = jasmine.createSpyObj("taskService", ["getAllPending"]); | |
| 14 | + let tasks = [{ id: 1 }, { id: 2 }]; | |
| 15 | + taskService.getAllPending = jasmine.createSpy("getAllPending").and.returnValue(Promise.resolve({ headers: () => { }, data: tasks })); | |
| 16 | + | |
| 17 | + beforeEach(angular.mock.module("templates")); | |
| 18 | + | |
| 19 | + beforeEach((done) => { | |
| 20 | + let cls = createClass({ | |
| 21 | + template: htmlTemplate, | |
| 22 | + directives: [TasksMenuComponent], | |
| 23 | + providers: [ | |
| 24 | + helpers.createProviderToValue("TaskService", taskService), | |
| 25 | + helpers.createProviderToValue('SessionService', helpers.mocks.sessionWithCurrentUser({})) | |
| 26 | + ] | |
| 27 | + }); | |
| 28 | + helper = new ComponentTestHelper<TasksMenuComponent>(cls, done); | |
| 29 | + }); | |
| 30 | + | |
| 31 | + it("load person tasks", () => { | |
| 32 | + expect(taskService.getAllPending).toHaveBeenCalled(); | |
| 33 | + }); | |
| 34 | + | |
| 35 | + it("load person tasks when receive a login event", () => { | |
| 36 | + helper.component.loadTasks = jasmine.createSpy("loadTasks"); | |
| 37 | + helper.component.ngOnInit(); | |
| 38 | + (<any>helper.component['authService'])[AuthEvents[AuthEvents.loginSuccess]].next({}); | |
| 39 | + expect(helper.component.loadTasks).toHaveBeenCalled(); | |
| 40 | + }); | |
| 41 | + }); | |
| 42 | +}); | ... | ... |
src/app/task/tasks-menu/tasks-menu.component.ts
| 1 | 1 | import { Component, Inject } from "ng-forward"; |
| 2 | 2 | import { TaskService } from "../../../lib/ng-noosfero-api/http/task.service"; |
| 3 | -import { SessionService } from "./../../login"; | |
| 3 | +import { AuthService, SessionService, AuthEvents } from "./../../login"; | |
| 4 | 4 | |
| 5 | 5 | @Component({ |
| 6 | 6 | selector: "tasks-menu", |
| 7 | 7 | templateUrl: "app/task/tasks-menu/tasks-menu.html" |
| 8 | 8 | }) |
| 9 | -@Inject(TaskService, SessionService) | |
| 9 | +@Inject(TaskService, SessionService, AuthService) | |
| 10 | 10 | export class TasksMenuComponent { |
| 11 | 11 | |
| 12 | 12 | tasks: noosfero.Task[]; |
| ... | ... | @@ -14,14 +14,21 @@ export class TasksMenuComponent { |
| 14 | 14 | perPage = 5; |
| 15 | 15 | person: noosfero.Person; |
| 16 | 16 | |
| 17 | - constructor(private taskService: TaskService, private session: SessionService) { } | |
| 17 | + constructor(private taskService: TaskService, private session: SessionService, private authService: AuthService) { } | |
| 18 | 18 | |
| 19 | 19 | ngOnInit() { |
| 20 | - this.person = this.session.currentUser() ? this.session.currentUser().person : null; | |
| 20 | + this.authService.subscribe(AuthEvents[AuthEvents.loginSuccess], () => { | |
| 21 | + this.loadTasks(); | |
| 22 | + }); | |
| 23 | + this.loadTasks(); | |
| 24 | + } | |
| 25 | + | |
| 26 | + loadTasks() { | |
| 27 | + if (!this.session.currentUser()) return; | |
| 28 | + this.person = this.session.currentUser().person; | |
| 21 | 29 | this.taskService.getAllPending({ per_page: this.perPage }).then((result: noosfero.RestResult<noosfero.Task[]>) => { |
| 22 | 30 | this.total = result.headers('total'); |
| 23 | 31 | this.tasks = result.data; |
| 24 | 32 | }); |
| 25 | 33 | } |
| 26 | - | |
| 27 | 34 | } | ... | ... |
| ... | ... | @@ -0,0 +1,34 @@ |
| 1 | +import { Provider, provide, Component } from 'ng-forward'; | |
| 2 | +import * as helpers from "../../../spec/helpers"; | |
| 3 | +import { ComponentTestHelper, createClass } from '../../../spec/component-test-helper'; | |
| 4 | +import { TasksComponent } from './tasks.component'; | |
| 5 | +import { AuthEvents } from "./../../login"; | |
| 6 | + | |
| 7 | +const htmlTemplate: string = '<tasks></tasks>'; | |
| 8 | + | |
| 9 | +describe("Components", () => { | |
| 10 | + describe("Task Menu Component", () => { | |
| 11 | + | |
| 12 | + let helper: ComponentTestHelper<TasksComponent>; | |
| 13 | + let taskService = jasmine.createSpyObj("taskService", ["getAllPending"]); | |
| 14 | + let tasks = [{ id: 1 }, { id: 2 }]; | |
| 15 | + taskService.getAllPending = jasmine.createSpy("getAllPending").and.returnValue(Promise.resolve({ headers: () => { }, data: tasks })); | |
| 16 | + | |
| 17 | + beforeEach(angular.mock.module("templates")); | |
| 18 | + | |
| 19 | + beforeEach((done) => { | |
| 20 | + let cls = createClass({ | |
| 21 | + template: htmlTemplate, | |
| 22 | + directives: [TasksComponent], | |
| 23 | + providers: [ | |
| 24 | + helpers.createProviderToValue("TaskService", taskService) | |
| 25 | + ] | |
| 26 | + }); | |
| 27 | + helper = new ComponentTestHelper<TasksComponent>(cls, done); | |
| 28 | + }); | |
| 29 | + | |
| 30 | + it("load person tasks", () => { | |
| 31 | + expect(taskService.getAllPending).toHaveBeenCalled(); | |
| 32 | + }); | |
| 33 | + }); | |
| 34 | +}); | ... | ... |
src/app/task/tasks/tasks.component.ts
| ... | ... | @@ -2,7 +2,7 @@ import { Component, Inject, provide } from "ng-forward"; |
| 2 | 2 | import { TaskService } from "../../../lib/ng-noosfero-api/http/task.service"; |
| 3 | 3 | |
| 4 | 4 | @Component({ |
| 5 | - selector: "tasks-component", | |
| 5 | + selector: "tasks", | |
| 6 | 6 | templateUrl: "app/task/tasks/tasks.html", |
| 7 | 7 | providers: [ |
| 8 | 8 | provide('taskService', { useClass: TaskService }) | ... | ... |
src/app/task/types/add-member/add-member-task-accept.component.ts
| ... | ... | @@ -0,0 +1,56 @@ |
| 1 | +import { TaskService } from "./task.service"; | |
| 2 | + | |
| 3 | + | |
| 4 | +describe("Services", () => { | |
| 5 | + | |
| 6 | + describe("Task Service", () => { | |
| 7 | + | |
| 8 | + let $httpBackend: ng.IHttpBackendService; | |
| 9 | + let taskService: TaskService; | |
| 10 | + | |
| 11 | + beforeEach(angular.mock.module("main", ($translateProvider: angular.translate.ITranslateProvider) => { | |
| 12 | + $translateProvider.translations('en', {}); | |
| 13 | + })); | |
| 14 | + | |
| 15 | + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _TaskService_: TaskService) => { | |
| 16 | + $httpBackend = _$httpBackend_; | |
| 17 | + taskService = _TaskService_; | |
| 18 | + })); | |
| 19 | + | |
| 20 | + | |
| 21 | + describe("Succesfull requests", () => { | |
| 22 | + | |
| 23 | + it("list pending tasks", (done) => { | |
| 24 | + $httpBackend.expectGET(`/api/v1/tasks?all_pending=true`).respond(200, { tasks: [{ id: 1 }] }); | |
| 25 | + taskService.getAllPending().then((result: noosfero.RestResult<noosfero.Task[]>) => { | |
| 26 | + expect(result.data).toEqual([{ id: 1 }]); | |
| 27 | + done(); | |
| 28 | + }); | |
| 29 | + $httpBackend.flush(); | |
| 30 | + }); | |
| 31 | + | |
| 32 | + it("finish a task", (done) => { | |
| 33 | + let taskId = 1; | |
| 34 | + let task: noosfero.Task = <any>{ id: taskId }; | |
| 35 | + $httpBackend.expectPUT(`/api/v1/tasks/${taskId}/finish`).respond(200, { task: { id: taskId } }); | |
| 36 | + taskService.finishTask(task).then((result: noosfero.RestResult<noosfero.Task>) => { | |
| 37 | + expect(result.data).toEqual({ id: 1 }); | |
| 38 | + done(); | |
| 39 | + }); | |
| 40 | + $httpBackend.flush(); | |
| 41 | + }); | |
| 42 | + | |
| 43 | + it("cancel a task", (done) => { | |
| 44 | + let taskId = 1; | |
| 45 | + let task: noosfero.Task = <any>{ id: taskId }; | |
| 46 | + $httpBackend.expectPUT(`/api/v1/tasks/${taskId}/cancel`).respond(200, { task: { id: taskId } }); | |
| 47 | + taskService.cancelTask(task).then((result: noosfero.RestResult<noosfero.Task>) => { | |
| 48 | + expect(result.data).toEqual({ id: 1 }); | |
| 49 | + done(); | |
| 50 | + }); | |
| 51 | + $httpBackend.flush(); | |
| 52 | + }); | |
| 53 | + }); | |
| 54 | + | |
| 55 | + }); | |
| 56 | +}); | ... | ... |
src/lib/ng-noosfero-api/http/task.service.ts
| ... | ... | @@ -20,18 +20,25 @@ export class TaskService extends RestangularService<noosfero.Task> { |
| 20 | 20 | }; |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | - getAllPending(params: any) { | |
| 23 | + getAllPending(params: any = {}) { | |
| 24 | 24 | params['all_pending'] = true; |
| 25 | 25 | return this.list(null, params); |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | finishTask(task: noosfero.Task) { |
| 29 | - let element = this.getElement(task.id); | |
| 30 | - return element.customPUT(null, "finish"); | |
| 29 | + return this.closeTask(task, "finish"); | |
| 31 | 30 | } |
| 32 | 31 | |
| 33 | 32 | cancelTask(task: noosfero.Task) { |
| 33 | + return this.closeTask(task, "cancel"); | |
| 34 | + } | |
| 35 | + | |
| 36 | + private closeTask(task: noosfero.Task, action: string) { | |
| 34 | 37 | let element = this.getElement(task.id); |
| 35 | - return element.customPUT(null, "cancel"); | |
| 38 | + let put = element.customPUT(null, action); | |
| 39 | + let deferred = this.$q.defer<noosfero.RestResult<noosfero.Task>>(); | |
| 40 | + put.then(this.getHandleSuccessFunction<noosfero.RestResult<noosfero.Task>>(deferred)); | |
| 41 | + put.catch(this.getHandleErrorFunction<noosfero.RestResult<noosfero.Task>>(deferred)); | |
| 42 | + return deferred.promise; | |
| 36 | 43 | } |
| 37 | 44 | } | ... | ... |
src/spec/mocks.ts
| ... | ... | @@ -223,11 +223,16 @@ export var mocks: any = { |
| 223 | 223 | } |
| 224 | 224 | }, |
| 225 | 225 | promiseResultTemplate: (response?: {}) => { |
| 226 | - | |
| 227 | - return { | |
| 228 | - then: (func?: (response: any) => void) => { | |
| 229 | - if (func) { return func(response); } | |
| 226 | + let callback = (func?: (response: any) => any) => { | |
| 227 | + if (func) { | |
| 228 | + let ret = func(response); | |
| 229 | + if (ret && typeof ret.then === "function") return ret; | |
| 230 | 230 | } |
| 231 | + return mocks.promiseResultTemplate(response); | |
| 232 | + }; | |
| 233 | + return { | |
| 234 | + then: callback, | |
| 235 | + finally: callback | |
| 231 | 236 | }; |
| 232 | 237 | }, |
| 233 | 238 | $log: { | ... | ... |