Selaa lähdekoodia

:gear: adjust test for queue return

tags/0.0.1
j 4 vuotta sitten
vanhempi
commit
9a71310e67
2 muutettua tiedostoa jossa 49 lisäystä ja 40 poistoa
  1. 15
    14
      backend/lib/routes/profile/patch-queue.js
  2. 34
    26
      backend/tests/matchqueue.spec.js

+ 15
- 14
backend/lib/routes/profile/patch-queue.js Näytä tiedosto

@@ -20,17 +20,16 @@ const responseSchemas = {
20 20
                 responses: Joi.array().items(),
21 21
                 user_type: Joi.any(),
22 22
             }),
23
-        )
23
+        ),
24 24
     ),
25 25
     error: Joi.object({
26 26
         error: Joi.string(),
27 27
     }),
28 28
 }
29 29
 
30
-
31 30
 const validators = {
32 31
     params: Joi.object({ profile_id: Joi.number(), target_id: Joi.number() }),
33
-    query: Joi.object({ include_profile:Joi.bool(), reinsert: Joi.boolean() }),
32
+    query: Joi.object({ include_profile: Joi.bool(), reinsert: Joi.boolean() }),
34 33
 }
35 34
 
36 35
 module.exports = {
@@ -44,30 +43,32 @@ module.exports = {
44 43
         handler: async function (request, h) {
45 44
             const { profile_id, target_id } = request.params
46 45
             const { include_profile, reinsert } = request.query
47
-            const { profileService, matchQueueService } = request.server.services()
46
+            const { profileService, matchQueueService } =
47
+                request.server.services()
48 48
             const updatedQueue = await matchQueueService.markAsDeleted(
49 49
                 profile_id,
50 50
                 target_id,
51 51
                 reinsert,
52 52
             )
53 53
             const queueIds = updatedQueue.map(entry => entry.target_id)
54
-            const res =  {
55
-                ok:true,
54
+            const res = {
55
+                ok: true,
56 56
                 handler: pluginConfig.handlerType,
57
-                data: queueIds
57
+                data: queueIds,
58 58
             }
59
-            if(include_profile) {
59
+            if (include_profile) {
60 60
                 res.data = await profileService.getProfilesFor(queueIds)
61 61
             }
62 62
             try {
63 63
                 return h.response(res).code(200)
64 64
             } catch (err) {
65
-                return h.response({
66
-                    ok:false,
67
-                    handler: pluginConfig.handlerType,
68
-                    data: { error: `${err}`}
69
-                }).code(409)
70
-
65
+                return h
66
+                    .response({
67
+                        ok: false,
68
+                        handler: pluginConfig.handlerType,
69
+                        data: { error: `${err}` },
70
+                    })
71
+                    .code(409)
71 72
             }
72 73
         },
73 74
         /** Validate based on validators object */

+ 34
- 26
backend/tests/matchqueue.spec.js Näytä tiedosto

@@ -1,13 +1,28 @@
1 1
 'use strict'
2 2
 
3 3
 const test = require('ava')
4
-const { stub } = require('sinon')
4
+const { stub, mock } = require('sinon')
5 5
 const Hapi = require('@hapi/hapi')
6 6
 
7 7
 const plugin = require('../lib/plugins/profile')
8 8
 
9 9
 const MatchQueue = require('../lib/models/matchqueue')
10
-
10
+/**
11
+ * Route parameters
12
+ */
13
+const params = {
14
+    profile_id: 1,
15
+    target_id: 2,
16
+    reinsert: true,
17
+    include_profile: false,
18
+}
19
+const mockReturn = {
20
+    queue: [
21
+        { mocked_profile_id: 1, target_id: 2, is_deleted: false },
22
+        { mocked_profile_id: 2, target_id: 3, is_deleted: false },
23
+        { mocked_profile_id: 3, target_id: 1, is_deleted: false },
24
+    ],
25
+}
11 26
 test('path / should return ok on GET', async t => {
12 27
     /**
13 28
      * Create a new server and register services,
@@ -18,17 +33,17 @@ test('path / should return ok on GET', async t => {
18 33
      * Schwifty at runtime.
19 34
      */
20 35
     const server = Hapi.server()
36
+    /**
37
+     * Overload so we don't register any models
38
+     * using the plugin call (see plugins/profile.js)
39
+     * and Manually load the model we need for the test
40
+     */
21 41
     server.registerModel = () => {}
22 42
     server.models = () => ({ MatchQueue })
23
-    await plugin.register(server)
24
-
25 43
     /**
26
-     * Route parameters
44
+     * Register Routes and Services as usual
27 45
      */
28
-    const profile_id = 1
29
-    const target_id = 2
30
-    const reinsert = true
31
-
46
+    await plugin.register(server)
32 47
     /**
33 48
      * Replace Objection model methods with our own custom functions
34 49
      * !: Kinda janky - might be better to temp knex sqlite instance
@@ -43,19 +58,13 @@ test('path / should return ok on GET', async t => {
43 58
             }),
44 59
         }),
45 60
         insert: queueRecord => {
46
-            t.is(queueRecord.profile_id, profile_id)
47
-            t.is(queueRecord.target_id, target_id)
48
-            t.is(queueRecord.is_deleted, !reinsert)
61
+            t.is(queueRecord.profile_id, params.profile_id)
62
+            t.is(queueRecord.target_id, params.target_id)
63
+            t.is(queueRecord.is_deleted, !params.reinsert)
49 64
         },
50 65
         // Mocked for getQueue()
51 66
         where: () => ({
52
-            andWhere: (cmd, val) => {
53
-                return [
54
-                    { mocked_profile_id: 1, is_deleted: val },
55
-                    { mocked_profile_id: 2, is_deleted: val },
56
-                    { mocked_profile_id: 3, is_deleted: val },
57
-                ]
58
-            },
67
+            andWhere: (cmd, val) => mockReturn.queue,
59 68
         }),
60 69
     })
61 70
 
@@ -64,13 +73,12 @@ test('path / should return ok on GET', async t => {
64 73
      */
65 74
     const { payload } = await server.inject({
66 75
         method: 'PATCH',
67
-        url: `/${profile_id}/queue/${target_id}/delete?reinsert=${reinsert}`,
76
+        url: `/${params.profile_id}/queue/${params.target_id}/delete?include_profile=${params.include_profile}&reinsert=${params.reinsert}`,
68 77
     })
69 78
     const res = JSON.parse(payload)
70
-
71
-    t.deepEqual(res, [
72
-        { mocked_profile_id: 1, is_deleted: false },
73
-        { mocked_profile_id: 2, is_deleted: false },
74
-        { mocked_profile_id: 3, is_deleted: false },
75
-    ])
79
+    t.deepEqual(res.ok, true)
80
+    t.deepEqual(
81
+        res.data,
82
+        mockReturn.queue.map(entry => entry.target_id),
83
+    )
76 84
 })

Loading…
Peruuta
Tallenna