From 7e9813032328d3d4384116e77e4cd575f66175e4 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Thu, 30 Apr 2015 11:38:36 +0200 Subject: [PATCH] Adding tests for home service --- app/modules/home/home.service.coffee | 8 +- app/modules/home/home.service.spec.coffee | 119 ++++++++++++++++++++++ 2 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 app/modules/home/home.service.spec.coffee diff --git a/app/modules/home/home.service.coffee b/app/modules/home/home.service.coffee index a3b97fce..42f245ba 100644 --- a/app/modules/home/home.service.coffee +++ b/app/modules/home/home.service.coffee @@ -3,11 +3,11 @@ class HomeService extends taiga.Service constructor: (@q, @rs, @rootScope, @projectUrl) -> @.workInProgress = Immutable.Map() - @.inProgress = false + @._inProgress = false fetchWorkInProgress: (userId) -> - if not @.inProgress - @.inProgress = true + if not @._inProgress + @._inProgress = true params = { status__is_closed: false assigned_to: userId @@ -52,7 +52,7 @@ class HomeService extends taiga.Service } }) - @.inProgress = false + @._inProgress = false return workPromise diff --git a/app/modules/home/home.service.spec.coffee b/app/modules/home/home.service.spec.coffee new file mode 100644 index 00000000..22a0f3d6 --- /dev/null +++ b/app/modules/home/home.service.spec.coffee @@ -0,0 +1,119 @@ +describe "tgHome", -> + homeService = provide = timeout = null + mocks = {} + + _mockResources = () -> + mocks.resources = {} + + mocks.resources.userstories = { + listInAllProjects: sinon.stub() + } + + mocks.resources.tasks = { + listInAllProjects: sinon.stub() + } + + mocks.resources.issues = { + listInAllProjects: sinon.stub() + } + + paramsAssignedTo = { + status__is_closed: false + assigned_to: 1 + } + + paramsWatching = { + status__is_closed: false + watchers: 1 + } + + mocks.thenStubAssignedToUserstories = sinon.stub() + mocks.resources.userstories.listInAllProjects.withArgs(paramsAssignedTo).returns({ + then: mocks.thenStubAssignedToUserstories + }) + + mocks.thenStubAssignedToTasks = sinon.stub() + mocks.resources.tasks.listInAllProjects.withArgs(paramsAssignedTo).returns({ + then: mocks.thenStubAssignedToTasks + }) + + mocks.thenStubAssignedToIssues = sinon.stub() + mocks.resources.issues.listInAllProjects.withArgs(paramsAssignedTo).returns({ + then: mocks.thenStubAssignedToIssues + }) + + + mocks.thenStubWatchingUserstories = sinon.stub() + mocks.resources.userstories.listInAllProjects.withArgs(paramsWatching).returns({ + then: mocks.thenStubWatchingUserstories + }) + + mocks.thenStubWatchingTasks = sinon.stub() + mocks.resources.tasks.listInAllProjects.withArgs(paramsWatching).returns({ + then: mocks.thenStubWatchingTasks + }) + + mocks.thenStubWatchingIssues = sinon.stub() + mocks.resources.issues.listInAllProjects.withArgs(paramsWatching).returns({ + then: mocks.thenStubWatchingIssues + }) + + provide.value "$tgResources", mocks.resources + + _mockProjectUrl = () -> + mocks.projectUrl = {get: sinon.stub()} + mocks.projectUrl.get = (project) -> + return "url-" + project.id + + provide.value "$projectUrl", mocks.projectUrl + + _inject = (callback) -> + inject (_$q_, _$tgResources_, _$rootScope_, _$projectUrl_, _$timeout_, _tgHomeService_) -> + timeout = _$timeout_ + homeService = _tgHomeService_ + callback() if callback + + _mocks = () -> + module ($provide) -> + provide = $provide + _mockResources() + _mockProjectUrl() + return null + + _setup = -> + _mocks() + + beforeEach -> + module "taigaHome" + _setup() + _inject() + + describe "fetch items", -> + it "work in progress filled", () -> + homeService.fetchWorkInProgress(1) + mocks.thenStubAssignedToUserstories.callArg(0, [{"id": 1}]) + mocks.thenStubAssignedToTasks.callArg(0, [{"id": 2}]) + mocks.thenStubAssignedToIssues.callArg(0, [{"id": 3}]) + mocks.thenStubWatchingUserstories.callArg(0, [{"id": 4}]) + mocks.thenStubWatchingTasks.callArg(0, [{"id": 5}]) + mocks.thenStubWatchingIssues.callArg(0, [{"id": 6}]) + + timeout.flush() + expect(homeService.workInProgress.toJS()).to.be.eql({ + assignedTo: { + userStories: [{"id": 1}] + tasks: [{"id": 2}] + issues: [{"id": 3}] + } + watching: { + userStories: [{"id": 4}] + tasks: [{"id": 5}] + issues: [{"id": 6}] + } + }) + + it "_inProgress change to false when tgResources end", () -> + homeService.fetchWorkInProgress(1) + expect(homeService._inProgress).to.be.true + timeout.flush() + expect(homeService._inProgress).to.be.false