test.util.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* eslint-disable no-console */
  2. const expect = chai.expect;
  3. import * as Log from '../core/util/logging.js';
  4. import { encodeUTF8, decodeUTF8 } from '../core/util/strings.js';
  5. describe('Utils', function () {
  6. "use strict";
  7. describe('logging functions', function () {
  8. beforeEach(function () {
  9. sinon.spy(console, 'log');
  10. sinon.spy(console, 'debug');
  11. sinon.spy(console, 'warn');
  12. sinon.spy(console, 'error');
  13. sinon.spy(console, 'info');
  14. });
  15. afterEach(function () {
  16. console.log.restore();
  17. console.debug.restore();
  18. console.warn.restore();
  19. console.error.restore();
  20. console.info.restore();
  21. Log.initLogging();
  22. });
  23. it('should use noop for levels lower than the min level', function () {
  24. Log.initLogging('warn');
  25. Log.Debug('hi');
  26. Log.Info('hello');
  27. expect(console.log).to.not.have.been.called;
  28. });
  29. it('should use console.debug for Debug', function () {
  30. Log.initLogging('debug');
  31. Log.Debug('dbg');
  32. expect(console.debug).to.have.been.calledWith('dbg');
  33. });
  34. it('should use console.info for Info', function () {
  35. Log.initLogging('debug');
  36. Log.Info('inf');
  37. expect(console.info).to.have.been.calledWith('inf');
  38. });
  39. it('should use console.warn for Warn', function () {
  40. Log.initLogging('warn');
  41. Log.Warn('wrn');
  42. expect(console.warn).to.have.been.called;
  43. expect(console.warn).to.have.been.calledWith('wrn');
  44. });
  45. it('should use console.error for Error', function () {
  46. Log.initLogging('error');
  47. Log.Error('err');
  48. expect(console.error).to.have.been.called;
  49. expect(console.error).to.have.been.calledWith('err');
  50. });
  51. });
  52. describe('string functions', function () {
  53. it('should decode UTF-8 to DOMString correctly', function () {
  54. const utf8string = '\xd0\x9f';
  55. const domstring = decodeUTF8(utf8string);
  56. expect(domstring).to.equal("П");
  57. });
  58. it('should encode DOMString to UTF-8 correctly', function () {
  59. const domstring = "åäöa";
  60. const utf8string = encodeUTF8(domstring);
  61. expect(utf8string).to.equal('\xc3\xa5\xc3\xa4\xc3\xb6\x61');
  62. });
  63. it('should allow Latin-1 strings if allowLatin1 is set when decoding', function () {
  64. const latin1string = '\xe5\xe4\xf6';
  65. expect(() => decodeUTF8(latin1string)).to.throw(Error);
  66. expect(decodeUTF8(latin1string, true)).to.equal('åäö');
  67. });
  68. });
  69. // TODO(directxman12): test the conf_default and conf_defaults methods
  70. // TODO(directxman12): test the event methods (addEvent, removeEvent, stopEvent)
  71. // TODO(directxman12): figure out a good way to test getPosition and getEventPosition
  72. // TODO(directxman12): figure out how to test the browser detection functions properly
  73. // (we can't really test them against the browsers, except for Gecko
  74. // via PhantomJS, the default test driver)
  75. });
  76. /* eslint-enable no-console */