Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ChatRoom.spec.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { parser } from './ChatRoom';
  2. import { $pres } from 'strophe.js';
  3. // This rule makes creating the xml elements take up way more
  4. // space than necessary.
  5. /* eslint-disable newline-per-chained-call */
  6. describe('ChatRoom', () => {
  7. describe('packet2JSON', () => {
  8. let nodes = [];
  9. beforeEach(() => {
  10. nodes = [];
  11. });
  12. it('translates attributes correctly', () => {
  13. const p = $pres({
  14. to: 'tojid',
  15. from: 'fromjid'
  16. })
  17. .c('fake-with-attr', {
  18. fakeAttr1: 'attrValue1',
  19. fakeAttr2: 'attrValue2'
  20. }).up();
  21. parser.packet2JSON(p.tree(), nodes);
  22. expect(nodes.length).toBe(1);
  23. const fakeWithAttr = nodes
  24. .find(n => n.tagName === 'fake-with-attr');
  25. expect(fakeWithAttr).toBeTruthy();
  26. expect(Object.keys(fakeWithAttr.attributes).length).toEqual(2);
  27. expect(fakeWithAttr.attributes.fakeAttr1).toBeTruthy();
  28. expect(fakeWithAttr.attributes.fakeAttr1).toEqual('attrValue1');
  29. expect(fakeWithAttr.attributes.fakeAttr2).toBeTruthy();
  30. expect(fakeWithAttr.attributes.fakeAttr2).toEqual('attrValue2');
  31. expect(fakeWithAttr.children.length).toEqual(0);
  32. expect(fakeWithAttr.value).toBeFalsy();
  33. });
  34. it('translates element text correctly', () => {
  35. const p = $pres({
  36. to: 'tojid',
  37. from: 'fromjid'
  38. })
  39. .c('user-agent').t('user-agent-text').up();
  40. parser.packet2JSON(p.tree(), nodes);
  41. expect(nodes.length).toBe(1);
  42. const userAgent = nodes.find(n => n.tagName === 'user-agent');
  43. expect(userAgent).toBeTruthy();
  44. expect(Object.keys(userAgent.attributes).length).toEqual(0);
  45. expect(userAgent.children.length).toEqual(0);
  46. expect(userAgent.value).toEqual('user-agent-text');
  47. });
  48. it('translates elements with children correctly', () => {
  49. const p = $pres({
  50. to: 'tojid',
  51. from: 'fromjid'
  52. })
  53. .c('identity')
  54. .c('user')
  55. .c('id').t('id-text').up()
  56. .c('name').t('name-text').up()
  57. .c('avatar').t('avatar-text').up()
  58. .up()
  59. .c('group').t('group-text').up()
  60. .up();
  61. parser.packet2JSON(p.tree(), nodes);
  62. const identity = nodes.find(n => n.tagName === 'identity');
  63. expect(identity).toBeTruthy();
  64. expect(Object.keys(identity.attributes).length).toEqual(0);
  65. expect(identity.children.length).toEqual(2);
  66. {
  67. const user = identity.children
  68. .find(n => n.tagName === 'user');
  69. expect(user).toBeTruthy();
  70. expect(Object.keys(user.attributes).length).toEqual(0);
  71. expect(user.children.length).toEqual(3);
  72. {
  73. const id = user.children
  74. .find(n => n.tagName === 'id');
  75. expect(id).toBeTruthy();
  76. expect(Object.keys(id.attributes).length).toEqual(0);
  77. expect(id.children.length).toEqual(0);
  78. expect(id.value).toEqual('id-text');
  79. }
  80. {
  81. const name = user.children
  82. .find(n => n.tagName === 'name');
  83. expect(name).toBeTruthy();
  84. expect(Object.keys(name.attributes).length).toEqual(0);
  85. expect(name.children.length).toEqual(0);
  86. expect(name.value).toEqual('name-text');
  87. }
  88. {
  89. const avatar = user.children
  90. .find(n => n.tagName === 'avatar');
  91. expect(avatar).toBeTruthy();
  92. expect(Object.keys(avatar.attributes).length).toEqual(0);
  93. expect(avatar.children.length).toEqual(0);
  94. expect(avatar.value).toEqual('avatar-text');
  95. }
  96. expect(user.value).toBeFalsy();
  97. }
  98. {
  99. const group = identity.children
  100. .find(n => n.tagName === 'group');
  101. expect(group).toBeTruthy();
  102. expect(Object.keys(group.attributes).length).toEqual(0);
  103. expect(group.children.length).toEqual(0);
  104. expect(group.value).toEqual('group-text');
  105. }
  106. expect(identity.value).toBeFalsy();
  107. });
  108. });
  109. });