3 const burstCalculator = require('./burst-calculator');
5 describe('burst-calculator', () => {
6 const testUserA = { id: 'abc', username: 'john_smith' };
8 it('should handle empty array', () => {
9 const result = burstCalculator.parse([]);
10 expect(result).toEqual([]);
13 it('should mark one chatItem as start of the burst', () => {
18 const result = burstCalculator.parse([chatItem]);
19 expect(result[0]).toEqual({ burstStart: true, ...chatItem });
22 it('should group two eligible messages into a burst', () => {
25 sent: '2020-02-04T00:00:00.000Z'
29 sent: '2020-02-04T00:01:00.000Z'
31 const result = burstCalculator.parse([chatItem1, chatItem2]);
32 expect(result[0]).toEqual({ burstStart: true, ...chatItem1 });
33 expect(result[1]).toEqual({ burstStart: false, ...chatItem2 });
36 it('should reset burst with a status message', () => {
39 sent: '2020-02-04T00:00:00.000Z'
44 sent: '2020-02-04T00:01:00.000Z'
46 const result = burstCalculator.parse([chatItem1, chatItem2]);
47 expect(result[0]).toEqual({ burstStart: true, ...chatItem1 });
48 expect(result[1]).toEqual({ burstStart: true, ...chatItem2 });
51 it('should reset burst with a different user', () => {
52 const testUserB = { id: 'def', username: 'peter_griffin' };
55 sent: '2020-02-04T00:00:00.000Z'
59 sent: '2020-02-04T00:01:00.000Z'
61 const result = burstCalculator.parse([chatItem1, chatItem2]);
62 expect(result[0]).toEqual({ burstStart: true, ...chatItem1 });
63 expect(result[1]).toEqual({ burstStart: true, ...chatItem2 });
66 it('should reset burst when more than 5 minutes elapses from the burst start', () => {
69 sent: '2020-02-04T00:00:00.000Z'
73 sent: '2020-02-04T00:02:00.000Z'
75 // 4 minutes since the last message, this would discover an issue if we only
76 // measured 5 minutes since the previous message
79 sent: '2020-02-04T00:06:00.000Z'
81 const result = burstCalculator.parse([chatItem1, chatItem2, chatItem3]);
82 expect(result[0]).toEqual({ burstStart: true, ...chatItem1 });
83 expect(result[1]).toEqual({ burstStart: false, ...chatItem2 });
84 expect(result[2]).toEqual({ burstStart: true, ...chatItem3 });
87 describe('thread messages', () => {
88 it('should ignore thread messages and compute burst only for MMF', () => {
91 sent: '2020-02-04T00:00:00.000Z'
93 // this message will get ignored and no burst attribute is added
97 sent: '2020-02-04T00:01:00.000Z'
99 // this message continues the burst in main message feed
102 sent: '2020-02-04T00:02:00.000Z'
104 const result = burstCalculator.parse([chatItem1, chatItem2, chatItem3]);
105 expect(result[0]).toEqual({ burstStart: true, ...chatItem1 });
106 expect(result[1]).toEqual({ ...chatItem2 });
107 expect(result[2]).toEqual({ burstStart: false, ...chatItem3 });
110 it('should ignore thread messages and only reset burst if MMF chat message users differ', () => {
111 const testUserB = { id: 'def', username: 'peter_griffin' };
114 sent: '2020-02-04T00:00:00.000Z'
116 // this message will get ignored and no burst attribute is added
120 sent: '2020-02-04T00:01:00.000Z'
122 // this message restarts the burst in main message feed, we validate that we don't continue burst from the previous thread message
125 sent: '2020-02-04T00:02:00.000Z'
127 const result = burstCalculator.parse([chatItem1, chatItem2, chatItem3]);
128 expect(result[0]).toEqual({ burstStart: true, ...chatItem1 });
129 expect(result[1]).toEqual({ ...chatItem2 });
130 expect(result[2]).toEqual({ burstStart: true, ...chatItem3 });
134 describe('complex sanity check scenarios', () => {
135 const testUserB = { id: 'def', username: 'peter_griffin' };
137 const baseTimeAddMinutes = minutes => {
138 const base = new Date('2020-02-04T00:00:00.000Z');
139 base.setMinutes(minutes);
140 return base.toISOString();
143 const removeBurstDataForTesting = items =>
145 const newItem = { ...item };
146 delete newItem.burstStart;
150 it('should calculate bursts for multiple messages', () => {
154 sent: baseTimeAddMinutes(0),
157 // reset burst, different user
160 sent: baseTimeAddMinutes(1),
166 sent: baseTimeAddMinutes(2),
172 sent: baseTimeAddMinutes(3),
176 // reset burst, previous was status
179 sent: baseTimeAddMinutes(4),
185 sent: baseTimeAddMinutes(5),
189 const result = burstCalculator.parse(removeBurstDataForTesting(expected));
190 expect(result).toEqual(expected);