From fa485e4d77fe2b324abfd1fb28a90a1697aec141 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Fri, 9 Sep 2016 12:41:06 +0200 Subject: [PATCH] Adding tests for epics service --- .../attachments-full.service.coffee | 2 +- app/modules/epics/epics.service.coffee | 15 +- app/modules/epics/epics.service.spec.coffee | 233 ++++++++++++++++++ 3 files changed, 241 insertions(+), 9 deletions(-) create mode 100644 app/modules/epics/epics.service.spec.coffee diff --git a/app/modules/components/attachments-full/attachments-full.service.coffee b/app/modules/components/attachments-full/attachments-full.service.coffee index 9e1fd874..2635f7c4 100644 --- a/app/modules/components/attachments-full/attachments-full.service.coffee +++ b/app/modules/components/attachments-full/attachments-full.service.coffee @@ -109,7 +109,7 @@ class AttachmentsFullService extends taiga.Service patch = {order: attachment.getIn(['file', 'order'])} promises.push @attachmentsService.patch(attachment.getIn(['file', 'id']), type, patch) - + return Promise.all(promises).then () => @._attachments = attachments diff --git a/app/modules/epics/epics.service.coffee b/app/modules/epics/epics.service.coffee index e5129024..4095bbf1 100644 --- a/app/modules/epics/epics.service.coffee +++ b/app/modules/epics/epics.service.coffee @@ -24,11 +24,10 @@ class EpicsService 'tgProjectService', 'tgAttachmentsService' 'tgResources', - 'tgXhrErrorService', - '$q' + 'tgXhrErrorService' ] - constructor: (@projectService, @attachmentsService, @resources, @xhrError, @q) -> + constructor: (@projectService, @attachmentsService, @resources, @xhrError) -> @._epics = Immutable.List() taiga.defineImmutableProperty @, 'epics', () => return @._epics @@ -52,14 +51,15 @@ class EpicsService .then (epic) => promises = _.map attachments.toJS(), (attachment) => @attachmentsService.upload(attachment.file, epic.get('id'), epic.get('project'), 'epic') - @q.all(promises).then () => + + Promise.all(promises).then () => @.fetchEpics() reorderEpic: (epic, newIndex) -> withoutMoved = @.epics.filter (it) => it.get('id') != epic.get('id') beforeDestination = withoutMoved.slice(0, newIndex) - previous = beforeDestination.last() + previous = beforeDestination.last() newOrder = if !previous then 0 else previous.get('epics_order') + 1 previousWithTheSameOrder = beforeDestination.filter (it) => @@ -72,7 +72,6 @@ class EpicsService epics_order: newOrder, version: epic.get('version') } - return @resources.epics.reorder(epic.get('id'), data, setOrders) .then () => @.fetchEpics() @@ -86,9 +85,9 @@ class EpicsService previousWithTheSameOrder = beforeDestination.filter (it) => it.get('epic_order') == previous.get('epic_order') - - setOrders = Immutable.OrderedMap previousWithTheSameOrder.map (it) => + setOrders = _.fromPairs previousWithTheSameOrder.map((it) => [it.get('id'), it.get('epic_order')] + ).toJS() data = { order: newOrder diff --git a/app/modules/epics/epics.service.spec.coffee b/app/modules/epics/epics.service.spec.coffee new file mode 100644 index 00000000..58efa075 --- /dev/null +++ b/app/modules/epics/epics.service.spec.coffee @@ -0,0 +1,233 @@ +### +# Copyright (C) 2014-2016 Taiga Agile LLC +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# File: epics.service.spec.coffee +### + +describe "tgEpicsService", -> + epicsService = provide = null + mocks = {} + + _mockTgProjectService = () -> + mocks.tgProjectService = { + project: Immutable.Map({ + "id": 1 + }) + } + + provide.value "tgProjectService", mocks.tgProjectService + + _mockTgAttachmentsService = () -> + mocks.tgAttachmentsService = { + upload: sinon.stub() + } + + provide.value "tgAttachmentsService", mocks.tgAttachmentsService + + _mockTgResources = () -> + mocks.tgResources = { + epics: { + list: sinon.stub() + post: sinon.stub() + patch: sinon.stub() + reorder: sinon.stub() + reorderRelatedUserstory: sinon.stub() + } + userstories: { + listInEpic: sinon.stub() + } + } + + provide.value "tgResources", mocks.tgResources + + _mockTgXhrErrorService = () -> + mocks.tgXhrErrorService = { + response: sinon.stub() + } + + provide.value "tgXhrErrorService", mocks.tgXhrErrorService + + _inject = (callback) -> + inject (_tgEpicsService_) -> + epicsService = _tgEpicsService_ + callback() if callback + + _mocks = () -> + module ($provide) -> + provide = $provide + _mockTgProjectService() + _mockTgAttachmentsService() + _mockTgResources() + _mockTgXhrErrorService() + return null + + _setup = -> + _mocks() + + beforeEach -> + module "taigaEpics" + _setup() + _inject() + + it "clear epics", () -> + epicsService._epics = Immutable.List(Immutable.Map({ + 'id': 1 + })) + + epicsService.clear() + expect(epicsService._epics.size).to.be.equal(0) + + it "fetch epics success", () -> + epics = Immutable.fromJS([ + { id: 111 } + { id: 112 } + ]) + promise = mocks.tgResources.epics.list.withArgs(1).promise().resolve(epics) + epicsService.fetchEpics().then () -> + expect(epicsService.epics).to.be.equal(epics) + + it "fetch epics error", () -> + epics = Immutable.fromJS([ + { id: 111 } + { id: 112 } + ]) + promise = mocks.tgResources.epics.list.withArgs(1).promise().reject(new Error("error")) + epicsService.fetchEpics().then () -> + expect(mocks.tgXhrErrorService.response.withArgs(new Error("error"))).have.been.calledOnce + + it "list related userstories", () -> + epic = Immutable.fromJS({ + id: 1 + }) + epicsService.listRelatedUserStories(epic) + expect(mocks.tgResources.userstories.listInEpic.withArgs(epic.get('id'))).have.been.calledOnce + + it "createEpic", () -> + epicData = {} + epic = Immutable.fromJS({ + id: 111 + project: 1 + }) + attachments = Immutable.fromJS([ + {file: "f1"}, + {file: "f2"} + ]) + + mocks.tgResources.epics + .post + .withArgs({project: 1}) + .promise() + .resolve(epic) + + mocks.tgAttachmentsService + .upload + .promise() + .resolve() + + epicsService.fetchEpics = sinon.stub() + epicsService.createEpic(epicData, attachments).then () -> + expect(mocks.tgAttachmentsService.upload.withArgs("f1", 111, 1, "epic")).have.been.calledOnce + expect(mocks.tgAttachmentsService.upload.withArgs("f2", 111, 1, "epic")).have.been.calledOnce + expect(epicsService.fetchEpics).have.been.calledOnce + + it "Update epic status", () -> + epic = Immutable.fromJS({ + id: 1 + version: 1 + }) + + mocks.tgResources.epics + .patch + .withArgs(1, {status: 33, version: 1}) + .promise() + .resolve() + + epicsService.fetchEpics = sinon.stub() + epicsService.updateEpicStatus(epic, 33).then () -> + expect(epicsService.fetchEpics).have.been.calledOnce + + it "Update epic assigned to", () -> + epic = Immutable.fromJS({ + id: 1 + version: 1 + }) + + mocks.tgResources.epics + .patch + .withArgs(1, {assigned_to: 33, version: 1}) + .promise() + .resolve() + + epicsService.fetchEpics = sinon.stub() + epicsService.updateEpicAssignedTo(epic, 33).then () -> + expect(epicsService.fetchEpics).have.been.calledOnce + + it "reorder epic", () -> + epicsService._epics = Immutable.fromJS([ + { + id: 1 + epics_order: 1 + version: 1 + }, + { + id: 2 + epics_order: 2 + version: 1 + }, + { + id: 3 + epics_order: 3 + version: 1 + }, + ]) + + mocks.tgResources.epics.reorder + .withArgs(3, {epics_order: 2, version: 1}, {1: 1}) + .promise() + .resolve() + + epicsService.fetchEpics = sinon.stub() + epicsService.reorderEpic(epicsService._epics.get(2), 1).then () -> + expect(epicsService.fetchEpics).have.been.calledOnce + + it "reorder related userstory in epic", () -> + epic = Immutable.fromJS({ + id: 1 + }) + + epicUserstories = Immutable.fromJS([ + { + id: 1 + epic_order: 1 + }, + { + id: 2 + epic_order: 2 + }, + { + id: 3 + epic_order: 3 + }, + ]) + + mocks.tgResources.epics.reorderRelatedUserstory + .withArgs(1, 3, {order: 2}, {1: 1}) + .promise() + .resolve() + + epicsService.listRelatedUserStories = sinon.stub() + epicsService.reorderRelatedUserstory(epic, epicUserstories, epicUserstories.get(2), 1).then () -> + expect(epicsService.listRelatedUserStories.withArgs(epic)).have.been.calledOnce