Fix l10n promises

This commit is contained in:
Vincent Petry 2014-11-19 17:02:17 +01:00
parent 1c5933c96c
commit ffe57d89e4
2 changed files with 11 additions and 6 deletions

View File

@ -37,10 +37,11 @@ OC.L10N = {
load: function(appName, callback) { load: function(appName, callback) {
// already available ? // already available ?
if (this._bundles[appName] || OC.getLocale() === 'en') { if (this._bundles[appName] || OC.getLocale() === 'en') {
if (callback) { var deferred = $.Deferred();
callback(); var promise = deferred.promise();
} promise.then(callback);
return; deferred.resolve();
return promise;
} }
var self = this; var self = this;

View File

@ -130,19 +130,23 @@ describe('OC.L10N tests', function() {
localeStub.restore(); localeStub.restore();
}); });
it('calls callback if translation already available', function() { it('calls callback if translation already available', function() {
var promiseStub = sinon.stub();
var callbackStub = sinon.stub(); var callbackStub = sinon.stub();
OC.L10N.register(TEST_APP, { OC.L10N.register(TEST_APP, {
'Hello world!': 'Hallo Welt!' 'Hello world!': 'Hallo Welt!'
}); });
OC.L10N.load(TEST_APP, callbackStub); OC.L10N.load(TEST_APP, callbackStub).then(promiseStub);
expect(callbackStub.calledOnce).toEqual(true); expect(callbackStub.calledOnce).toEqual(true);
expect(promiseStub.calledOnce).toEqual(true);
expect(fakeServer.requests.length).toEqual(0); expect(fakeServer.requests.length).toEqual(0);
}); });
it('calls callback if locale is en', function() { it('calls callback if locale is en', function() {
var localeStub = sinon.stub(OC, 'getLocale').returns('en'); var localeStub = sinon.stub(OC, 'getLocale').returns('en');
var promiseStub = sinon.stub();
var callbackStub = sinon.stub(); var callbackStub = sinon.stub();
OC.L10N.load(TEST_APP, callbackStub); OC.L10N.load(TEST_APP, callbackStub).then(promiseStub);
expect(callbackStub.calledOnce).toEqual(true); expect(callbackStub.calledOnce).toEqual(true);
expect(promiseStub.calledOnce).toEqual(true);
expect(fakeServer.requests.length).toEqual(0); expect(fakeServer.requests.length).toEqual(0);
}); });
}); });