File size: 6,163 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// @flow
import { request } from '../../utils';
import data from 'shared/testing/data';
import { toPlainText, toState } from 'shared/draft-utils';

const messageToPlainText = message =>
  toPlainText(toState(JSON.parse(message.content.body)));

// Get all messages in our test thread and sort them by time
const messages = data.messages
  .filter(({ threadId }) => threadId === 'thread-1')
  .sort((a, b) => a.timestamp - b.timestamp);

describe('messageConnection', () => {
  it('should fetch a threads messages', async () => {
    const query = /* GraphQL */ `
      {
        thread(id: "thread-1") {
          messageConnection(first: 999999) {
            edges {
              node {
                id
              }
            }
          }
        }
      }
    `;

    expect.hasAssertions();
    const result = await request(query);
    expect(result.data.thread.messageConnection.edges).toHaveLength(
      messages.length
    );
    expect(result).toMatchSnapshot();
  });

  it('should fetch the first message first', async () => {
    const query = /* GraphQL */ `
      {
        thread(id: "thread-1") {
          messageConnection(first: 1) {
            pageInfo {
              hasNextPage
              hasPreviousPage
            }
            edges {
              node {
                id
                content {
                  body
                }
              }
            }
          }
        }
      }
    `;

    expect.hasAssertions();
    const result = await request(query);

    expect(result.data.thread.messageConnection.edges).toHaveLength(1);

    expect(result.data.thread.messageConnection.pageInfo).toEqual({
      // This is true if there is more messages than this one
      hasNextPage: messages.length > 1,
      // This has to be false since it's the first message
      hasPreviousPage: false,
    });

    expect(
      messageToPlainText(result.data.thread.messageConnection.edges[0].node)
    ).toEqual(messageToPlainText(messages[0]));
  });

  it('should fetch the second message next', async () => {
    // Get the cursor of the first message
    const query = /* GraphQL */ `
      {
        thread(id: "thread-1") {
          messageConnection(first: 1) {
            edges {
              cursor
            }
          }
        }
      }
    `;

    expect.hasAssertions();
    const result = await request(query);
    const cursor = result.data.thread.messageConnection.edges[0].cursor;

    // Get one message after the cursor of the first message
    // i.e. the second message
    const nextQuery = /* GraphQL */ `
      {
        thread(id: "thread-1") {
          messageConnection(first: 1, after: "${cursor}") {
            pageInfo {
              hasNextPage
              hasPreviousPage
            }
            edges {
              node {
                content {
                  body
                }
              }
            }
          }
        }
      }
    `;

    const nextResult = await request(nextQuery);

    expect(nextResult.data.thread.messageConnection.edges).toHaveLength(1);

    expect(nextResult.data.thread.messageConnection.pageInfo).toEqual({
      hasNextPage: messages.length > 2,
      // We know this has a previous page
      hasPreviousPage: true,
    });

    expect(
      messageToPlainText(nextResult.data.thread.messageConnection.edges[0].node)
    ).toEqual(messageToPlainText(messages[1]));
  });

  it('should correctly set pageInfo when more messages are requested than are available', async () => {
    // Request more messages than there are
    const query = /* GraphQL */ `
      {
        thread(id: "thread-1") {
          messageConnection(first: ${messages.length + 1}) {
            pageInfo {
              hasNextPage
              hasPreviousPage
            }
            edges {
              cursor
            }
          }
        }
      }
    `;

    expect.hasAssertions();
    const result = await request(query);
    const { edges, pageInfo } = result.data.thread.messageConnection;

    expect(edges).toHaveLength(messages.length);

    expect(pageInfo).toEqual({
      hasNextPage: false,
      hasPreviousPage: false,
    });
  });

  it('should correctly set pageInfo when all messages are requested', async () => {
    // Request more messages than there are
    const query = /* GraphQL */ `
      {
        thread(id: "thread-1") {
          messageConnection(first: ${messages.length}) {
            pageInfo {
              hasNextPage
              hasPreviousPage
            }
            edges {
              cursor
            }
          }
        }
      }
    `;

    expect.hasAssertions();
    const result = await request(query);
    const { edges, pageInfo } = result.data.thread.messageConnection;

    expect(edges).toHaveLength(messages.length);

    expect(pageInfo).toEqual({
      hasNextPage: false,
      hasPreviousPage: false,
    });
  });

  it('should correctly set pageInfo when more messages are requested than are available after a cursor', async () => {
    // Get the cursor of the first message
    const query = /* GraphQL */ `
      {
        thread(id: "thread-1") {
          messageConnection(first: 1) {
            edges {
              cursor
            }
          }
        }
      }
    `;

    expect.hasAssertions();
    const result = await request(query);
    const cursor = result.data.thread.messageConnection.edges[0].cursor;

    // Get all messages after the cursor of the first message
    // but get more than are available
    const nextQuery = /* GraphQL */ `
      {
        thread(id: "thread-1") {
          messageConnection(first: ${messages.length}, after: "${cursor}") {
            pageInfo {
              hasNextPage
              hasPreviousPage
            }
            edges {
              cursor
            }
          }
        }
      }
    `;

    const nextResult = await request(nextQuery);
    const { edges, pageInfo } = nextResult.data.thread.messageConnection;

    expect(edges).toHaveLength(messages.length - 1);
    expect(pageInfo).toEqual({
      hasNextPage: false,
      hasPreviousPage: true,
    });
  });
});