]> git.r.bdr.sh - rbdr/forum/blame - src/lib/stores/tags.test.ts
Update / use typescript
[rbdr/forum] / src / lib / stores / tags.test.ts
CommitLineData
5fc4715f
RBR
1import { GraphQLInteraction, Pact, Matchers } from '@pact-foundation/pact';
2import { resolve } from 'path';
3
a7cf03c1 4import { resolveAfter } from '$lib/utils/resolve_after';
5fc4715f
RBR
5
6const { eachLike, like } = Matchers;
7
a7cf03c1 8jest.mock('$lib/config/config.ts');
5fc4715f
RBR
9
10import { getTag } from './tags';
11
12const internals = {
13 provider: null
14};
15
16describe('Tags store pact', () => {
17
18 beforeAll(async () => {
19
20 internals.provider = new Pact({
21 port: 1234,
22 dir: resolve(process.cwd(), 'pacts'),
23 consumer: 'ForumClient',
24 provider: 'ForumServer',
25 pactfileWriteMode: 'update'
26 });
27
28 await internals.provider.setup();
29 });
30
31 afterEach(() => internals.provider.verify());
32 afterAll(() => internals.provider.finalize());
33
34 describe('When there\'s data', () => {
35
36 describe('GetTag', () => {
37
38 beforeAll(async () => {
39
40 const tagQuery = new GraphQLInteraction()
41 .given('there\'s data')
42 .uponReceiving('a request to get a single tag')
43 .withRequest({
44 path: '/graphql',
45 method: 'POST'
46 })
47 .withOperation('GetTag')
48 .withQuery(
49 `query GetTag($id: ID!) {
50 tag(id: $id) {
51 id
52 topics {
53 id
54 title
55 updated_at
56 ttl
57 __typename
58 }
59 __typename
60 }
61 }`
62 )
63 .withVariables({
64 id: 'pineapple'
65 })
66 .willRespondWith({
67 status: 200,
68 headers: {
69 'Content-Type': 'application/json; charset=utf-8'
70 },
71 body: {
72 data: {
73 tag: {
74 id: like('pineapple'),
75 topics: eachLike({
76 id: like('cd038ae7-e8b4-4e38-9543-3d697e69ac34'),
77 title: like('This topic is about pineapples'),
78 updated_at: like(1619978944077),
79 ttl: like(3555)
80 })
81 }
82 }
83 }
84 });
85 return await internals.provider.addInteraction(tagQuery);
86 });
87
88 test('it returns the tag', async () => {
89
90 const tag = getTag('pineapple');
91 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
92 let response = null;
93 tag.subscribe((tagValue) => {
94
95 response = tagValue;
96 counter();
97 });
98 expect(response.data).toBe(null);
99 expect(response.loading).toBe(true);
100 expect(response.error).toBe(undefined);
101 await resolveAfterTwo;
102 expect(response.data).toEqual({
103 id: 'pineapple',
104 topics: [{
105 id: 'cd038ae7-e8b4-4e38-9543-3d697e69ac34',
106 title: 'This topic is about pineapples',
107 updated_at: 1619978944077,
108 ttl: 3555
109 }]
110 });
111 expect(response.loading).toBe(false);
112 expect(response.error).toBe(undefined);
113 });
114 });
115 });
116
117 describe('When there\'s no data', () => {
118
119 describe('GetTag', () => {
120
121 beforeAll(async () => {
122
123 const tagQuery = new GraphQLInteraction()
124 .given('there\'s no data')
125 .uponReceiving('a request to get a single tag')
126 .withRequest({
127 path: '/graphql',
128 method: 'POST'
129 })
130 .withOperation('GetTag')
131 .withQuery(
132 `query GetTag($id: ID!) {
133 tag(id: $id) {
134 id
135 topics {
136 id
137 title
138 updated_at
139 ttl
140 __typename
141 }
142 __typename
143 }
144 }`
145 )
146 .withVariables({
147 id: 'pineapple'
148 })
149 .willRespondWith({
150 status: 200,
151 headers: {
152 'Content-Type': 'application/json; charset=utf-8'
153 },
154 body: {
155 data: {
156 tag: null
157 }
158 }
159 });
160 return await internals.provider.addInteraction(tagQuery);
161 });
162
163 test('it returns the tag', async () => {
164
165 const tag = getTag('pineapple');
166 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
167 let response = null;
168 tag.subscribe((tagValue) => {
169
170 response = tagValue;
171 counter();
172 });
173 expect(response.data).toBe(null);
174 expect(response.loading).toBe(true);
175 expect(response.error).toBe(undefined);
176 await resolveAfterTwo;
177 expect(response.data).toBe(null);
178 expect(response.loading).toBe(false);
179 expect(response.error).toBe(undefined);
180 });
181 });
182 });
183
26dfa00e 184 describe('When there\'s a server error', () => {
5fc4715f
RBR
185
186 describe('GetTag', () => {
187
188 beforeAll(async () => {
189
190 const tagQuery = new GraphQLInteraction()
26dfa00e 191 .given('there\'s a server error')
5fc4715f
RBR
192 .uponReceiving('a request to get a single tag')
193 .withRequest({
194 path: '/graphql',
195 method: 'POST'
196 })
197 .withOperation('GetTag')
198 .withQuery(
199 `query GetTag($id: ID!) {
200 tag(id: $id) {
201 id
202 topics {
203 id
204 title
205 updated_at
206 ttl
207 __typename
208 }
209 __typename
210 }
211 }`
212 )
213 .withVariables({
214 id: 'pineapple'
215 })
216 .willRespondWith({
217 status: 500
218 });
219 return await internals.provider.addInteraction(tagQuery);
220 });
221
222 test('it returns the error', async () => {
223
224 const tag = getTag('pineapple');
225 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
226 let response = null;
227 tag.subscribe((tagValue) => {
228
229 response = tagValue;
230 counter();
231 });
232 expect(response.data).toBe(null);
233 expect(response.loading).toBe(true);
234 expect(response.error).toBe(undefined);
235 await resolveAfterTwo;
236 expect(response.data).toBe(null);
237 expect(response.loading).toBe(false);
238 expect(response.error).toBeInstanceOf(Error);
239 });
240 });
241 });
26dfa00e
RBR
242
243 describe('When there\'s an error in the response', () => {
244
245 describe('GetTag', () => {
246
247 beforeAll(async () => {
248
249 const tagQuery = new GraphQLInteraction()
250 .given('there\'s an error in the response')
251 .uponReceiving('a request to get a single tag')
252 .withRequest({
253 path: '/graphql',
254 method: 'POST'
255 })
256 .withOperation('GetTag')
257 .withQuery(
258 `query GetTag($id: ID!) {
259 tag(id: $id) {
260 id
261 topics {
262 id
263 title
264 updated_at
265 ttl
266 __typename
267 }
268 __typename
269 }
270 }`
271 )
272 .withVariables({
273 id: 'pineapple'
274 })
275 .willRespondWith({
276 status: 200,
277 headers: {
278 'Content-Type': 'application/json; charset=utf-8'
279 },
280 body: {
281 errors: eachLike({
282 message: like('An error occurred when fetching the tag')
283 })
284 }
285 });
286 return await internals.provider.addInteraction(tagQuery);
287 });
288
289 test('it returns the error', async () => {
290
291 const tag = getTag('pineapple');
292 const { counter, promise: resolveAfterTwo } = resolveAfter(2);
293 let response = null;
294 tag.subscribe((tagValue) => {
295
296 response = tagValue;
297 counter();
298 });
299 expect(response.data).toBe(null);
300 expect(response.loading).toBe(true);
301 expect(response.error).toBe(undefined);
302 await resolveAfterTwo;
303 expect(response.data).toBe(null);
304 expect(response.loading).toBe(false);
305 expect(response.error.graphQLErrors).toEqual(expect.arrayContaining([{
306 message: 'An error occurred when fetching the tag'
307 }]));
308 });
309 });
310 });
5fc4715f 311});