File size: 2,269 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// @flow
import { sortAndGroupMessages } from '../group-messages';

// TODO: FIXME
type Message = Object;

// 01.01.2017
const FIRST_JAN = 1483225200000;

const createMessage = ({
  timestamp,
  body,
  authorId,
  id,
}: {
  timestamp?: Date,
  body?: string,
  authorId?: string,
  id?: string | number,
}): Message => ({
  id: id || 'whatever',
  timestamp: timestamp || new Date(FIRST_JAN),
  content: {
    body: body || 'Hey',
  },
  author: {
    user: {
      id: authorId || 'asdf123',
    },
  },
  messageType: 'text',
});

const filterRobo = messageGroups =>
  messageGroups.filter(group => group[0].author.user.id !== 'robo');

it('should sort messages by timestamp', () => {
  const one = createMessage({
    timestamp: new Date(FIRST_JAN - 10000),
    body: 'First',
  });
  const two = createMessage({
    timestamp: new Date(FIRST_JAN),
    body: 'Second',
  });
  const messages = [two, one];

  expect(filterRobo(sortAndGroupMessages(messages))).toEqual([[one, two]]);
});

it('should group messages by author', () => {
  const one = createMessage({
    authorId: 'first',
  });
  const two = createMessage({
    authorId: 'first',
  });
  const three = createMessage({
    authorId: 'second',
  });
  const four = createMessage({
    authorId: 'first',
  });

  const messages = [one, two, three, four];

  expect(filterRobo(sortAndGroupMessages(messages))).toEqual([
    [one, two],
    [three],
    [four],
  ]);
});

it('should add a timestamp above the first message', () => {
  const timestamp = new Date(FIRST_JAN);
  const messages = [createMessage({ timestamp })];

  expect(sortAndGroupMessages(messages)).toMatchSnapshot();
});

const SEVEN_HOURS = 25200000;
it("should add a timestamp between two messages if there's more than six hours between them", () => {
  const first = createMessage({
    timestamp: new Date(FIRST_JAN),
  });
  const second = createMessage({
    timestamp: new Date(FIRST_JAN + SEVEN_HOURS),
  });

  const messages = [first, second];

  const result = sortAndGroupMessages(messages);

  // Should have three message groups, two robo texts + two groups
  expect(result).toHaveLength(4);
  // Expect a robo text timestamp to be between the message groups
  expect(result[2][0].author.user.id).toEqual('robo');
});