From 6e9f49f3974c0e77c2d556004723b6192b3036a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Mon, 24 Apr 2017 21:31:53 +0200 Subject: [PATCH] Add "complete" callback support for addShare MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Calviño Sánchez --- core/js/shareitemmodel.js | 4 ++ core/js/tests/specs/shareitemmodelSpec.js | 53 +++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/core/js/shareitemmodel.js b/core/js/shareitemmodel.js index c2f7c78f6c..a11e785adc 100644 --- a/core/js/shareitemmodel.js +++ b/core/js/shareitemmodel.js @@ -179,6 +179,10 @@ url: this._getUrl('shares'), data: attributes, dataType: 'json' + }).always(function() { + if (_.isFunction(options.complete)) { + options.complete(self); + } }).done(function() { self.fetch().done(function() { if (_.isFunction(options.success)) { diff --git a/core/js/tests/specs/shareitemmodelSpec.js b/core/js/tests/specs/shareitemmodelSpec.js index 5c5aa9608b..771a926370 100644 --- a/core/js/tests/specs/shareitemmodelSpec.js +++ b/core/js/tests/specs/shareitemmodelSpec.js @@ -670,6 +670,30 @@ describe('OC.Share.ShareItemModel', function() { shareWith: 'group1' }); }); + it('calls complete handler before refreshing the model', function() { + var completeStub = sinon.stub(); + model.addShare({ + shareType: OC.Share.SHARE_TYPE_GROUP, + shareWith: 'group1' + }, { + complete: completeStub + }); + + expect(fakeServer.requests.length).toEqual(1); + fakeServer.requests[0].respond( + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify({ }) + ); + + expect(completeStub.calledOnce).toEqual(true); + expect(completeStub.lastCall.args[0]).toEqual(model); + + fetchReshareDeferred.resolve(makeOcsResponse([])); + fetchSharesDeferred.resolve(makeOcsResponse([])); + + expect(completeStub.calledOnce).toEqual(true); + }); it('calls success handler after refreshing the model', function() { var successStub = sinon.stub(); model.addShare({ @@ -694,6 +718,35 @@ describe('OC.Share.ShareItemModel', function() { expect(successStub.calledOnce).toEqual(true); expect(successStub.lastCall.args[0]).toEqual(model); }); + it('calls complete handler before error handler', function() { + var completeStub = sinon.stub(); + var errorStub = sinon.stub(); + model.addShare({ + shareType: OC.Share.SHARE_TYPE_GROUP, + shareWith: 'group1' + }, { + complete: completeStub, + error: errorStub + }); + + expect(fakeServer.requests.length).toEqual(1); + fakeServer.requests[0].respond( + 400, + { 'Content-Type': 'application/json' }, + JSON.stringify({ + ocs: { + meta: { + message: 'Some error message' + } + } + }) + ); + + expect(completeStub.calledOnce).toEqual(true); + expect(completeStub.lastCall.args[0]).toEqual(model); + expect(errorStub.calledOnce).toEqual(true); + expect(completeStub.calledBefore(errorStub)).toEqual(true); + }); it('calls error handler with error message', function() { var errorStub = sinon.stub(); model.addShare({