|
| 1 | +import { EventBuffer } from '../../../../src/features/utils/event-buffer' |
| 2 | + |
| 3 | +let eventBuffer |
| 4 | +describe('EventBuffer', () => { |
| 5 | + beforeEach(() => { |
| 6 | + eventBuffer = new EventBuffer() |
| 7 | + }) |
| 8 | + it('has default values', () => { |
| 9 | + expect(eventBuffer).toMatchObject({ |
| 10 | + bytes: 0, |
| 11 | + buffer: [] |
| 12 | + }) |
| 13 | + }) |
| 14 | + |
| 15 | + describe('add', () => { |
| 16 | + it('should add data to the buffer while maintaining size', () => { |
| 17 | + expect(eventBuffer).toMatchObject({ |
| 18 | + bytes: 0, |
| 19 | + buffer: [] |
| 20 | + }) |
| 21 | + |
| 22 | + const mockEvent = { test: 1 } |
| 23 | + |
| 24 | + eventBuffer.add(mockEvent) |
| 25 | + expect(eventBuffer).toMatchObject({ |
| 26 | + bytes: JSON.stringify(mockEvent).length, |
| 27 | + buffer: [mockEvent] |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should not add if one event is too large', () => { |
| 32 | + eventBuffer.add({ test: 'x'.repeat(1000000) }) |
| 33 | + expect(eventBuffer.buffer).toEqual([]) |
| 34 | + }) |
| 35 | + |
| 36 | + it('should not add if existing buffer would become too large', () => { |
| 37 | + eventBuffer.add({ test: 'x'.repeat(999988) }) |
| 38 | + expect(eventBuffer.bytes).toEqual(999999) |
| 39 | + expect(eventBuffer.buffer.length).toEqual(1) |
| 40 | + eventBuffer.add({ test2: 'testing' }) |
| 41 | + expect(eventBuffer.bytes).toEqual(999999) |
| 42 | + expect(eventBuffer.buffer.length).toEqual(1) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should be chainable', () => { |
| 46 | + const mockEvent1 = { test: 1 } |
| 47 | + const mockEvent2 = { test: 2 } |
| 48 | + eventBuffer.add(mockEvent1).add(mockEvent2) |
| 49 | + expect(eventBuffer).toMatchObject({ |
| 50 | + bytes: JSON.stringify(mockEvent1).length + JSON.stringify(mockEvent2).length, |
| 51 | + buffer: [mockEvent1, mockEvent2] |
| 52 | + }) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + describe('merge', () => { |
| 57 | + it('should merge two EventBuffers - append', () => { |
| 58 | + const mockEvent1 = { test: 1 } |
| 59 | + const mockEvent2 = { test: 2 } |
| 60 | + eventBuffer.add(mockEvent1) |
| 61 | + |
| 62 | + const secondBuffer = new EventBuffer() |
| 63 | + secondBuffer.add(mockEvent2) |
| 64 | + eventBuffer.merge(secondBuffer) |
| 65 | + expect(eventBuffer).toMatchObject({ |
| 66 | + bytes: JSON.stringify({ test: 1 }).length + JSON.stringify({ test: 2 }).length, |
| 67 | + buffer: [mockEvent1, mockEvent2] |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should merge two EventBuffers - prepend', () => { |
| 72 | + const mockEvent1 = { test: 1 } |
| 73 | + const mockEvent2 = { test: 2 } |
| 74 | + eventBuffer.add(mockEvent1) |
| 75 | + |
| 76 | + const secondBuffer = new EventBuffer() |
| 77 | + secondBuffer.add(mockEvent2) |
| 78 | + eventBuffer.merge(secondBuffer, true) |
| 79 | + expect(eventBuffer).toMatchObject({ |
| 80 | + bytes: JSON.stringify({ test: 1 }).length + JSON.stringify({ test: 2 }).length, |
| 81 | + buffer: [mockEvent2, mockEvent1] |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + it('should not merge if not an EventBuffer', () => { |
| 86 | + eventBuffer.add({ test: 1 }) |
| 87 | + // not EventBuffer |
| 88 | + eventBuffer.merge({ regular: 'object' }) |
| 89 | + expect(eventBuffer.buffer).toEqual([{ test: 1 }]) |
| 90 | + // not EventBuffer |
| 91 | + eventBuffer.merge('string') |
| 92 | + expect(eventBuffer.buffer).toEqual([{ test: 1 }]) |
| 93 | + // not EventBuffer |
| 94 | + eventBuffer.merge(123) |
| 95 | + expect(eventBuffer.buffer).toEqual([{ test: 1 }]) |
| 96 | + // not EventBuffer |
| 97 | + eventBuffer.merge(true) |
| 98 | + expect(eventBuffer.buffer).toEqual([{ test: 1 }]) |
| 99 | + // not EventBuffer |
| 100 | + eventBuffer.merge(Symbol('test')) |
| 101 | + expect(eventBuffer.buffer).toEqual([{ test: 1 }]) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should not merge if too big', () => { |
| 105 | + const mockEvent1 = { test: 'x'.repeat(999988) } |
| 106 | + const mockEvent2 = { test2: 'testing' } |
| 107 | + eventBuffer.add(mockEvent1) |
| 108 | + |
| 109 | + const secondBuffer = new EventBuffer() |
| 110 | + secondBuffer.add(mockEvent2) |
| 111 | + |
| 112 | + eventBuffer.merge(secondBuffer) |
| 113 | + expect(eventBuffer.buffer.length).toEqual(1) |
| 114 | + expect(eventBuffer.bytes).toEqual(999999) |
| 115 | + }) |
| 116 | + |
| 117 | + it('should be chainable', () => { |
| 118 | + const mockEvent1 = { test1: 1 } |
| 119 | + const mockEvent2 = { test2: 2 } |
| 120 | + const mockEvent3 = { test3: 3 } |
| 121 | + |
| 122 | + const secondBuffer = new EventBuffer() |
| 123 | + const thirdBuffer = new EventBuffer() |
| 124 | + |
| 125 | + eventBuffer.add(mockEvent1) |
| 126 | + secondBuffer.add(mockEvent2) |
| 127 | + thirdBuffer.add(mockEvent3) |
| 128 | + |
| 129 | + eventBuffer.merge(secondBuffer).merge(thirdBuffer) |
| 130 | + expect(eventBuffer).toMatchObject({ |
| 131 | + bytes: JSON.stringify(mockEvent1).length + JSON.stringify(mockEvent2).length + JSON.stringify(mockEvent3).length, |
| 132 | + buffer: [mockEvent1, mockEvent2, mockEvent3] |
| 133 | + }) |
| 134 | + }) |
| 135 | + }) |
| 136 | + |
| 137 | + describe('hasData', () => { |
| 138 | + it('should return false if no events', () => { |
| 139 | + jest.spyOn(eventBuffer, 'bytes', 'get').mockReturnValue(100) |
| 140 | + expect(eventBuffer.hasData).toEqual(false) |
| 141 | + }) |
| 142 | + it('should return false if no bytes', () => { |
| 143 | + jest.spyOn(eventBuffer, 'buffer', 'get').mockReturnValue({ test: 1 }) |
| 144 | + expect(eventBuffer.hasData).toEqual(false) |
| 145 | + }) |
| 146 | + it('should return true if has a valid event and size', () => { |
| 147 | + eventBuffer.add({ test: 1 }) |
| 148 | + expect(eventBuffer.hasData).toEqual(true) |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + describe('canMerge', () => { |
| 153 | + it('should return false if would be too big', () => { |
| 154 | + jest.spyOn(eventBuffer, 'bytes', 'get').mockReturnValue(999999) |
| 155 | + expect(eventBuffer.canMerge(1)).toEqual(false) |
| 156 | + }) |
| 157 | + it('should return false if no size provided', () => { |
| 158 | + eventBuffer.add({ test: 1 }) |
| 159 | + expect(eventBuffer.canMerge()).toEqual(false) |
| 160 | + }) |
| 161 | + it('should return false if size is not a number', () => { |
| 162 | + eventBuffer.add({ test: 1 }) |
| 163 | + expect(eventBuffer.canMerge('test')).toEqual(false) |
| 164 | + expect(eventBuffer.canMerge(false)).toEqual(false) |
| 165 | + expect(eventBuffer.canMerge(['test'])).toEqual(false) |
| 166 | + expect(eventBuffer.canMerge({ test: 1 })).toEqual(false) |
| 167 | + }) |
| 168 | + it('should return true if has a valid event and size', () => { |
| 169 | + eventBuffer.add({ test: 1 }) |
| 170 | + expect(eventBuffer.canMerge(20)).toEqual(true) |
| 171 | + }) |
| 172 | + }) |
| 173 | +}) |
0 commit comments