Front end of the Slack clone application.

test.serviceworkers.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* global navigator:true, window:true, Modernizr:true, describe:true, expect:true, it:true, xit:true, before:true, beforeEach:true, after:true*/
  2. var DRIVERS = [
  3. localforage.INDEXEDDB,
  4. localforage.LOCALSTORAGE,
  5. localforage.WEBSQL
  6. ];
  7. DRIVERS.forEach(function(driverName) {
  8. if ((!Modernizr.indexeddb && driverName === localforage.INDEXEDDB) ||
  9. (!Modernizr.localstorage && driverName === localforage.LOCALSTORAGE) ||
  10. (!Modernizr.websqldatabase && driverName === localforage.WEBSQL)) {
  11. // Browser doesn't support this storage library, so we exit the API
  12. // tests.
  13. return;
  14. }
  15. describe('Service Worker support in ' + driverName, function() {
  16. 'use strict';
  17. // Use this until a test is added to Modernizr
  18. if (!('serviceworker' in Modernizr)) {
  19. Modernizr.serviceworker = 'serviceWorker' in navigator;
  20. }
  21. if (!Modernizr.serviceworker) {
  22. before.skip("doesn't have service worker support");
  23. beforeEach.skip("doesn't have service worker support");
  24. it.skip("doesn't have service worker support");
  25. after.skip("doesn't have service worker support");
  26. return;
  27. }
  28. if (!window.MessageChannel) {
  29. before.skip("doesn't have MessageChannel support");
  30. beforeEach.skip("doesn't have MessageChannel support");
  31. it.skip("doesn't have MessageChannel support");
  32. after.skip("doesn't have MessageChannel support");
  33. return;
  34. }
  35. before(function(done) {
  36. navigator.serviceWorker
  37. .register('/test/serviceworker-client.js')
  38. .then(function() {
  39. return localforage.setDriver(driverName);
  40. })
  41. .then(done);
  42. });
  43. after(function(done) {
  44. navigator.serviceWorker.ready
  45. .then(function(registration) {
  46. return registration.unregister();
  47. })
  48. .then(function(bool) {
  49. if (bool) {
  50. done();
  51. } else {
  52. done('service worker failed to unregister');
  53. }
  54. });
  55. });
  56. beforeEach(function(done) {
  57. localforage.clear(done);
  58. });
  59. if (driverName === localforage.LOCALSTORAGE ||
  60. driverName === localforage.WEBSQL) {
  61. it.skip(driverName + ' is not supported in service workers');
  62. return;
  63. }
  64. xit('should set a value on registration', function(done) {
  65. navigator.serviceWorker.ready
  66. .then(function() {
  67. return localforage.getItem('service worker registration');
  68. })
  69. .then(function(result) {
  70. expect(result)
  71. .to.equal('serviceworker present');
  72. done();
  73. })
  74. .catch(function(error) {
  75. done(error);
  76. });
  77. });
  78. it('saves data', function(done) {
  79. var messageChannel = new MessageChannel();
  80. messageChannel.port1.onmessage = function(event) {
  81. expect(event.data.body)
  82. .to.be('I have been set using ' + driverName);
  83. done();
  84. };
  85. navigator.serviceWorker.ready
  86. .then(function(registration) {
  87. registration.active.postMessage({
  88. driver: driverName,
  89. value: 'I have been set'
  90. }, [messageChannel.port2]);
  91. })
  92. .catch(function(error) {
  93. done(error);
  94. });
  95. });
  96. });
  97. });