aboutsummaryrefslogtreecommitdiff
path: root/src/stores/forums.test.js
blob: 694316baac611c3528c6dfc01ee5b6954fe4b303 (plain)
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
import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
import { resolve } from 'path';

import { resolveAfter } from '$/utils/resolve_after';

const { eachLike, like } = Matchers;

jest.mock('$/config/config.js');

import { getForum, getForums } from './forums';

const internals = {
  provider: null
};

describe('Forums store pact', () => {

  beforeAll(async () => {

    internals.provider = new Pact({
      port: 1234,
      dir: resolve(process.cwd(), 'pacts'),
      consumer: 'ForumsStore',
      provider: 'ForumAPIServer'
    });

    await internals.provider.setup();
  });

  afterEach(() => internals.provider.verify());
  afterAll(() => internals.provider.finalize());

  describe('GetForums', () => {

    beforeAll(async () => {

      const forumQuery = new GraphQLInteraction()
        .uponReceiving('a request to list the forums')
        .withRequest({
          path: '/graphql',
          method: 'POST'
        })
        .withOperation('GetForums')
        .withQuery(
          `query GetForums {
            forums {
              id
              glyph
              label
              position
              __typename
            }
          }`
        )
        .withVariables({})
        .willRespondWith({
          status: 200,
          headers: {
            'Content-Type': 'application/json; charset=utf-8'
          },
          body: {
            data: {
              forums: eachLike({
                id: like('butter'),
                glyph: like('⌘'),
                label: like('test_forums.butter'),
                position: like(1)
              })
            }
          }
        });
      return await internals.provider.addInteraction(forumQuery);
    });

    test('it returns the forums', async () => {

      const forums = getForums();
      const { counter, promise: resolveAfterTwo } = resolveAfter(2);
      let response = null;
      forums.subscribe((forumsValue) => {

        response = forumsValue;
        counter();
      });
      expect(response.data).toEqual(expect.arrayContaining([]));
      expect(response.loading).toBe(true);
      expect(response.error).toBe(undefined);
      await resolveAfterTwo;
      expect(response.data).toEqual(expect.arrayContaining([{
        id: 'butter',
        glyph: '⌘',
        label: 'test_forums.butter',
        position: 1
      }]));
      expect(response.loading).toBe(false);
      expect(response.error).toBe(undefined);
    });
  });

  describe('GetForum', () => {

    beforeAll(async () => {

      const forumQuery = new GraphQLInteraction()
        .uponReceiving('a request to get a single forum')
        .withRequest({
          path: '/graphql',
          method: 'POST'
        })
        .withOperation('GetForum')
        .withQuery(
          `query GetForum($id: ID!) {
            forum(id: $id) {
              id
              glyph
              label
              position
              topics {
                id
                title
                updated_at
                ttl
                __typename
              }
              __typename
            }
          }`
        )
        .withVariables({
          id: 'freezer'
        })
        .willRespondWith({
          status: 200,
          headers: {
            'Content-Type': 'application/json; charset=utf-8'
          },
          body: {
            data: {
              forum: like({
                id: 'freezer',
                glyph: like('✭'),
                label: like('test_forums.freezer'),
                position: like(3),
                topics: eachLike({
                  id: like('629de02c-151a-4db7-bb86-43b2add8a15a'),
                  title: like('Very pacty topic'),
                  updated_at: like(1619954611616),
                  ttl: like(3601)
                })
              })
            }
          }
        });
      return await internals.provider.addInteraction(forumQuery);
    });

    test('it returns the forum', async () => {

      const forum = getForum('freezer');
      const { counter, promise: resolveAfterTwo } = resolveAfter(2);
      let response = null;
      forum.subscribe((forumsValue) => {

        response = forumsValue;
        counter();
      });
      expect(response.data).toEqual(expect.arrayContaining([]));
      expect(response.loading).toBe(true);
      expect(response.error).toBe(undefined);
      await resolveAfterTwo;
      expect(response.data.id).toBe('freezer');
      expect(response.data.glyph).toBe('✭');
      expect(response.data.label).toBe('test_forums.freezer');
      expect(response.data.position).toBe(3);
      expect(response.data.topics).toEqual(expect.arrayContaining([{
        id: '629de02c-151a-4db7-bb86-43b2add8a15a',
        title: 'Very pacty topic',
        updated_at: 1619954611616,
        ttl: 3601
      }]));
      expect(response.loading).toBe(false);
      expect(response.error).toBe(undefined);
    });
  });
});