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