|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const assert = require('assert') |
| 5 | +const _compact = require('lodash/compact') |
| 6 | +const _includes = require('lodash/includes') |
| 7 | +const { PulseMessage } = require('../../../lib') |
| 8 | +const testModel = require('../../helpers/test_model') |
| 9 | +const testModelValidation = require('../../helpers/test_model_validation') |
| 10 | + |
| 11 | +describe('Pulse Message model', () => { |
| 12 | + testModel({ |
| 13 | + model: PulseMessage, |
| 14 | + orderedFields: [ |
| 15 | + 'id', |
| 16 | + 'mts', |
| 17 | + null, |
| 18 | + 'userID', |
| 19 | + null, |
| 20 | + 'title', |
| 21 | + 'content', |
| 22 | + null, |
| 23 | + null, |
| 24 | + 'isPin', |
| 25 | + 'isPublic', |
| 26 | + null, |
| 27 | + 'tags', |
| 28 | + 'attachments', |
| 29 | + null, |
| 30 | + 'likes', |
| 31 | + 'userLiked', |
| 32 | + null |
| 33 | + ] |
| 34 | + }) |
| 35 | + |
| 36 | + testModelValidation({ |
| 37 | + model: PulseMessage, |
| 38 | + validData: { |
| 39 | + id: 'foo', |
| 40 | + mts: new Array(...(new Array(5))).map(() => Math.random()), |
| 41 | + userID: ['foo', 'bar', 'baz', 'qux'], |
| 42 | + title: ['foo', 'bar', 'baz', 'qux'], |
| 43 | + content: ['foo', 'bar', 'baz', 'qux'], |
| 44 | + isPin: new Array(...(new Array(5))).map(() => Math.random()), |
| 45 | + isPublic: new Array(...(new Array(5))).map(() => Math.random()), |
| 46 | + tags: ['hash', 'tag'], |
| 47 | + attachments: ['foo', 'bar', 'baz', 'qux'], |
| 48 | + likes: new Array(...(new Array(5))).map(() => Math.random()), |
| 49 | + userLiked: new Array(...(new Array(5))).map(() => Math.random()) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + it('initializes correctly', () => { |
| 54 | + const pm = new PulseMessage([ |
| 55 | + 'foo', |
| 56 | + 12345, |
| 57 | + null, |
| 58 | + 'bar', |
| 59 | + null, |
| 60 | + 'title', |
| 61 | + 'content', |
| 62 | + null, |
| 63 | + null, |
| 64 | + 1, |
| 65 | + 0, |
| 66 | + null, |
| 67 | + ['#hash', '#tag'], |
| 68 | + ['foo', 'bar'], |
| 69 | + null, |
| 70 | + 1, |
| 71 | + 2, |
| 72 | + null |
| 73 | + ]) |
| 74 | + |
| 75 | + assert.strictEqual(pm.id, 'foo') |
| 76 | + assert.strictEqual(pm.mts, 12345) |
| 77 | + assert.strictEqual(pm.userID, 'bar') |
| 78 | + assert.strictEqual(pm.title, 'title') |
| 79 | + assert.strictEqual(pm.content, 'content') |
| 80 | + assert.strictEqual(pm.isPin, 1) |
| 81 | + assert.strictEqual(pm.isPublic, 0) |
| 82 | + assert.deepEqual(pm.tags, ['#hash', '#tag']) |
| 83 | + assert.deepEqual(pm.attachments, ['foo', 'bar']) |
| 84 | + assert.strictEqual(pm.likes, 1) |
| 85 | + assert.strictEqual(pm.userLiked, 2) |
| 86 | + }) |
| 87 | + |
| 88 | + it('serializes correctly', () => { |
| 89 | + const pm = new PulseMessage([ |
| 90 | + 'foo', |
| 91 | + 12345, |
| 92 | + null, |
| 93 | + 'bar', |
| 94 | + null, |
| 95 | + 'title', |
| 96 | + 'content', |
| 97 | + null, |
| 98 | + null, |
| 99 | + 1, |
| 100 | + 1, |
| 101 | + null, |
| 102 | + ['#hash', '#tag'], |
| 103 | + ['foo', 'bar'], |
| 104 | + null, |
| 105 | + 1, |
| 106 | + 2, |
| 107 | + null |
| 108 | + ]) |
| 109 | + |
| 110 | + const arr = _compact(pm.serialize()) |
| 111 | + assert.deepStrictEqual(arr, [ |
| 112 | + 'foo', |
| 113 | + 12345, |
| 114 | + 'bar', |
| 115 | + 'title', |
| 116 | + 'content', |
| 117 | + 1, |
| 118 | + 1, |
| 119 | + ['#hash', '#tag'], |
| 120 | + ['foo', 'bar'], |
| 121 | + 1, |
| 122 | + 2 |
| 123 | + ]) |
| 124 | + }) |
| 125 | + |
| 126 | + it('unserializes correctly', () => { |
| 127 | + const obj = PulseMessage.unserialize([ |
| 128 | + 'foo', |
| 129 | + 12345, |
| 130 | + null, |
| 131 | + 'bar', |
| 132 | + null, |
| 133 | + 'title', |
| 134 | + 'content', |
| 135 | + null, |
| 136 | + null, |
| 137 | + 1, |
| 138 | + 1, |
| 139 | + null, |
| 140 | + ['#hash', '#tag'], |
| 141 | + ['foo', 'bar'], |
| 142 | + null, |
| 143 | + 1, |
| 144 | + 2, |
| 145 | + null |
| 146 | + ]) |
| 147 | + |
| 148 | + assert.strictEqual(obj.id, 'foo') |
| 149 | + assert.strictEqual(obj.mts, 12345) |
| 150 | + assert.strictEqual(obj.userID, 'bar') |
| 151 | + assert.strictEqual(obj.title, 'title') |
| 152 | + assert.strictEqual(obj.content, 'content') |
| 153 | + assert.strictEqual(obj.isPin, 1) |
| 154 | + assert.strictEqual(obj.isPublic, 1) |
| 155 | + assert.deepEqual(obj.tags, ['#hash', '#tag']) |
| 156 | + assert.deepEqual(obj.attachments, ['foo', 'bar']) |
| 157 | + assert.strictEqual(obj.likes, 1) |
| 158 | + assert.strictEqual(obj.userLiked, 2) |
| 159 | + }) |
| 160 | + |
| 161 | + describe('toString', () => { |
| 162 | + it('includes pertinent information', () => { |
| 163 | + const pm = new PulseMessage({ |
| 164 | + id: 'foo777', |
| 165 | + mts: 12345, |
| 166 | + userID: 'bar888', |
| 167 | + title: 'title', |
| 168 | + content: 'content', |
| 169 | + isPin: 1, |
| 170 | + isPublic: 0, |
| 171 | + tags: ['#hash', '#tag'], |
| 172 | + attachments: ['foo', 'bar'], |
| 173 | + likes: 1, |
| 174 | + userLiked: 1 |
| 175 | + }) |
| 176 | + |
| 177 | + const str = pm.toString() |
| 178 | + assert.ok(_includes(str, '(foo777)'), 'id missing') |
| 179 | + assert.ok(_includes(str, 'bar888'), 'userID missing') |
| 180 | + assert.ok(_includes(str, 'title'), 'title missing') |
| 181 | + assert.ok(_includes(str, 'pinned'), 'pinned missing') |
| 182 | + assert.ok(_includes(str, 'not public'), 'not public missing') |
| 183 | + assert.ok(_includes(str, 'content'), 'content missing') |
| 184 | + assert.ok(_includes(str, '#hash #tag'), 'hash tags missing') |
| 185 | + assert.ok(_includes(str, 'foo bar'), 'attachments missing') |
| 186 | + assert.ok(_includes(str, 'likes(1)'), 'likes missing') |
| 187 | + }) |
| 188 | + }) |
| 189 | +}) |
0 commit comments