StorageSpec.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. (function (root, factory) {
  2. define([
  3. "converse"
  4. ], function (xmppchat) {
  5. return factory(xmppchat);
  6. }
  7. );
  8. } (this, function (xmppchat) {
  9. String.prototype.repeat = function(times) {
  10. return (new Array(times + 1)).join(this+' ').replace(/\s$/, '');
  11. };
  12. return describe("Local storage of messages and open chats", function() {
  13. beforeEach(function() {
  14. this.storage = new xmppchat.ClientStorage('dummy@localhost');
  15. });
  16. describe("open chat storage", function () {
  17. beforeEach(function() {
  18. // Removes all stored items
  19. this.storage.flush();
  20. });
  21. it("should by default not have any chats", function () {
  22. expect(this.storage.getOpenChats()).toEqual([]);
  23. });
  24. it("should be able to add chats", function () {
  25. this.storage.addOpenChat('chat1@localhost');
  26. expect(this.storage.getOpenChats()).toEqual(['chat1@localhost']);
  27. this.storage.addOpenChat('chat2@localhost');
  28. expect(this.storage.getOpenChats()).toEqual(['chat1@localhost', 'chat2@localhost']);
  29. this.storage.addOpenChat('chat3@localhost');
  30. expect(this.storage.getOpenChats()).toEqual(['chat1@localhost',
  31. 'chat2@localhost',
  32. 'chat3@localhost']);
  33. });
  34. it("should be able to remove chats", function () {
  35. this.storage.removeOpenChat('non-existing@localhost');
  36. expect(this.storage.getOpenChats()).toEqual([]);
  37. this.storage.addOpenChat('chat1@localhost');
  38. this.storage.addOpenChat('chat2@localhost');
  39. this.storage.addOpenChat('chat3@localhost');
  40. expect(this.storage.getOpenChats()).toEqual(['chat1@localhost',
  41. 'chat2@localhost',
  42. 'chat3@localhost']);
  43. this.storage.removeOpenChat('chat2@localhost');
  44. expect(this.storage.getOpenChats()).toEqual(['chat1@localhost',
  45. 'chat3@localhost']);
  46. this.storage.removeOpenChat('chat1@localhost');
  47. expect(this.storage.getOpenChats()).toEqual(['chat3@localhost']);
  48. this.storage.removeOpenChat('chat3@localhost');
  49. expect(this.storage.getOpenChats()).toEqual([]);
  50. this.storage.removeOpenChat('non-existing@localhost');
  51. expect(this.storage.getOpenChats()).toEqual([]);
  52. });
  53. });
  54. describe("message storage", function () {
  55. var iso_regex = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/;
  56. it("should by default not have any messages", function () {
  57. expect(this.storage.getMessages('chat@localhost')).toEqual([]);
  58. });
  59. it("should be able to add and retrieve messages", function () {
  60. var i, jid = 'chat@localhost';
  61. for (i=0; i<10; i++) {
  62. var msg = 'msg'.repeat(i+1);
  63. this.storage.addMessage(jid, msg, 'to');
  64. // Check that message is returned
  65. var msgs = this.storage.getMessages(jid);
  66. expect(msgs.length).toEqual(i+1);
  67. expect(msgs[i]).toEqual(jasmine.any(String));
  68. // First two space separated strings are ISO date and direction
  69. var msg_arr = msgs[i].split(' ', 2);
  70. // Check that first string is ISO format
  71. expect(msg_arr[0]).toMatch(iso_regex);
  72. expect(msg_arr[0]).toEqual(jasmine.any(String));
  73. expect(msg_arr[1]).toEqual('to');
  74. expect(msgs[i].replace(/(.*?\s.*?\s)/, '')).toEqual(msg);
  75. }
  76. });
  77. it("should be able to get last message", function () {
  78. var msg = this.storage.getLastMessage('chat@localhost'),
  79. msg_arr = msg.split(' ', 2);
  80. expect(msg_arr[0]).toMatch(iso_regex);
  81. expect(msg_arr[0]).toEqual(jasmine.any(String));
  82. expect(msg_arr[1]).toEqual('to');
  83. expect(msg.replace(/(.*?\s.*?\s)/, '')).toEqual('msg'.repeat(10));
  84. });
  85. it("should be able to clear all messages", function () {
  86. this.storage.clearMessages('chat@localhost');
  87. expect(this.storage.getMessages('chat@localhost')).toEqual([]);
  88. });
  89. it("should not store more than 30 messages", function () {
  90. var i, msgs;
  91. for (i=0; i<100; i++) {
  92. this.storage.addMessage('chat@localhost', 'msg', 'to');
  93. }
  94. msgs = this.storage.getMessages('chat@localhost');
  95. expect(msgs.length).toEqual(30);
  96. });
  97. });
  98. });
  99. }));