test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. describe("vConsole", function() {
  2. var assert = require('chai').assert;
  3. var util = require('util');
  4. var jsdom = require('jsdom');
  5. it('touch event', function(done) {
  6. jsdom.env('test/log.html', function(err, window) {
  7. var document = window.document;
  8. window.localStorage = {
  9. getItem: function (key) {
  10. return this[key];
  11. },
  12. setItem: function (key, value) {
  13. this[key] = String(value);
  14. }
  15. };
  16. global.localStorage = window.localStorage;
  17. var vcSwitch = document.querySelector('.vc-switch');
  18. var eventTouchstart = document.createEvent('Event');
  19. eventTouchstart.initEvent('touchstart', true, true);
  20. var point = { x: 10, y: 10 };
  21. eventTouchstart.touches = [{
  22. identifier: Math.random(),
  23. pageX: point.x,
  24. pageY: point.y,
  25. screenX: point.x,
  26. screenY: point.y,
  27. clientX: point.x,
  28. clientY: point.y
  29. }];
  30. vcSwitch.dispatchEvent(eventTouchstart);
  31. var eventTouchmove = document.createEvent('Event');
  32. eventTouchmove.initEvent('touchmove', true, true);
  33. var point = { x: 12, y: 12 };
  34. eventTouchmove.touches = [{
  35. identifier: Math.random(),
  36. pageX: point.x,
  37. pageY: point.y,
  38. screenX: point.x,
  39. screenY: point.y,
  40. clientX: point.x,
  41. clientY: point.y
  42. }];
  43. vcSwitch.dispatchEvent(eventTouchmove);
  44. var eventTouchend = document.createEvent('Event');
  45. eventTouchend.initEvent('touchend', true, true);
  46. vcSwitch.dispatchEvent(eventTouchend);
  47. setTimeout(function () {
  48. assert.equal(localStorage.vConsole_switch_x, '8');
  49. global.localStorage = null;
  50. window.localStorage = null;
  51. done();
  52. }, 100);
  53. }, {
  54. features: {
  55. FetchExternalResources: ["link", "script"]
  56. }
  57. });
  58. });
  59. it('log.html', function(done) {
  60. jsdom.env('test/log.html', function(err, window) {
  61. var document = window.document;
  62. assert.equal(document.querySelector('#__vconsole') !== null, true);
  63. document.querySelector('.weui_btn.weui_btn_default:nth-of-type(1)').click(); // formattedLog
  64. assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content').innerHTML, ' formattedLog() Start');
  65. document.querySelector('.vc-toolbar .vc-tool.vc-tool-default:nth-of-type(1)').click(); // clear
  66. assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content'), null);
  67. document.querySelector('.weui_btn.weui_btn_default:nth-of-type(2)').click(); // normalObject
  68. assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content').innerHTML, ' normalObject() Start');
  69. document.querySelector('.vc-toolbar .vc-tool.vc-tool-default:nth-of-type(1)').click(); // clear
  70. assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-item .vc-item-content'), null);
  71. done();
  72. }, {
  73. features: {
  74. FetchExternalResources: ["link", "script"]
  75. }
  76. });
  77. });
  78. it('plugin.html', function(done) {
  79. jsdom.env('test/plugin.html', function(err, window) {
  80. var document = window.document;
  81. assert.equal(document.querySelector('#__vconsole') !== null, true);
  82. document.querySelector('.page a:nth-of-type(1)').click(); // newTab
  83. assert.equal(document.querySelector('.vc-tabbar .vc-tab:nth-of-type(4)').innerHTML, 'Tab1');
  84. done();
  85. }, {
  86. features: {
  87. FetchExternalResources: ["link", "script"]
  88. }
  89. });
  90. });
  91. it('ajax.html', function(done) {
  92. this.timeout(2000);
  93. jsdom.env('test/ajax.html', function(err, window) {
  94. var document = window.document;
  95. assert.equal(document.querySelector('#__vconsole') !== null, true);
  96. document.querySelector('.page a:nth-of-type(1)').click(); // asyncAjax
  97. setTimeout(function () {
  98. assert.equal(document.querySelector('.vc-logbox.vc-actived .vc-log .vc-fold-outer').innerHTML, 'Object {ret: 0, msg: "ok"}');
  99. done();
  100. }, 10)
  101. }, {
  102. features: {
  103. FetchExternalResources: ["link", "script"]
  104. }
  105. });
  106. });
  107. });