Skip to content

Commit

Permalink
Updates logger to extend warnings outside of Okta's window.okta (#447)
Browse files Browse the repository at this point in the history
Resolves: OKTA-169946
  • Loading branch information
jmelberg-okta committed May 11, 2018
1 parent b834946 commit ab15d31
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@
"optionalDependencies": {
"fsevents": "*"
}
}
}
66 changes: 57 additions & 9 deletions src/util/Logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,65 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

// Note: The reason to create a separate logger (instead of placing these
// functions in util/Util) is because this is used in places like Bundles that
// need to be loaded before Okta is defined.
define(['okta/underscore', 'shared/util/Logger'], function (_, Logger) {
define(function () {

return _.extend(Logger, {

deprecate: function (msg) {
Logger.warn('[okta-signin-widget] DEPRECATED:', msg);
function log(level, args) {
if (window.console) {
window.console[level].apply(window.console, args);
}
}

/**
* Utility library of logging functions.
*/
return {

trace: function () {
return log('trace', arguments);
},

dir: function () {
return log('dir', arguments);
},

time: function () {
return log('time', arguments);
},

timeEnd: function () {
return log('timeEnd', arguments);
},

group: function () {
return log('group', arguments);
},

});
groupEnd: function () {
return log('groupEnd', arguments);
},

assert: function () {
return log('assert', arguments);
},

log: function () {
return log('log', arguments);
},

info: function () {
return log('info', arguments);
},

warn: function () {
return log('warn', arguments);
},

error: function () {
return log('error', arguments);
},

deprecate: function (msg) {
return log('warn', ['[okta-signin-widget] DEPRECATED:', msg]);
}
};
});
16 changes: 10 additions & 6 deletions test/unit/spec/LoginRouter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ define([
'backbone',
'shared/util/Util',
'util/CryptoUtil',
'util/Logger',
'shared/util/Logger',
'@okta/okta-auth-js/jquery',
'helpers/mocks/Util',
'helpers/util/Expect',
Expand Down Expand Up @@ -171,10 +171,13 @@ function (Okta, Q, Backbone, SharedUtil, CryptoUtil, Logger, OktaAuth, Util, Exp
}

function expectUnexpectedFieldLog(arg1) {
// These console warnings are called from Courage's Logger class, not
// the Widget's. We need to assert that the following is called in specific
// environments (window.okta && window.okta.debug are defined).
expect(Logger.warn).toHaveBeenCalledWith('Field not defined in schema', arg1);
}

it('throws a ConfigError if unknown option is passed as a widget param', function () {
it('logs a ConfigError error if unknown option is passed as a widget param', function () {
spyOn(Logger, 'warn');
var fn = function () { setup({ foo: 'bla' }); };
expect(fn).not.toThrow(Errors.ConfigError);
Expand Down Expand Up @@ -290,7 +293,7 @@ function (Okta, Q, Backbone, SharedUtil, CryptoUtil, Logger, OktaAuth, Util, Exp
);
});
});
it('throws an error on unrecoverable errors if no globalErrorFn is defined', function () {
it('logs an error on unrecoverable errors if no globalErrorFn is defined', function () {
var fn = function () {
setup({ foo: 'bar' });
};
Expand All @@ -300,12 +303,13 @@ function (Okta, Q, Backbone, SharedUtil, CryptoUtil, Logger, OktaAuth, Util, Exp
});
it('calls globalErrorFn on unrecoverable errors if it is defined', function () {
var errorSpy = jasmine.createSpy('errorSpy');
spyOn(Logger, 'warn');
var fn = function () {
setup({ globalErrorFn: errorSpy, foo: 'bar' });
setup({ globalErrorFn: errorSpy, baseUrl: undefined });
};
expect(fn).not.toThrow();
expectUnexpectedFieldLog('foo');
var err = errorSpy.calls.mostRecent().args[0];
expect(err.name).toBe('CONFIG_ERROR');
expect(err.message).toEqual('"baseUrl" is a required widget parameter');
});
it('calls globalErrorFn if cors is not supported by the browser', function () {
var errorSpy = jasmine.createSpy('errorSpy');
Expand Down

0 comments on commit ab15d31

Please sign in to comment.