Parcourir la source

:sparkles: using sinon to mock objection methods in tests | building mock for Schwifty model registration | working sample test for matchqueue

tags/0.0.1
j il y a 4 ans
Parent
révision
8618bf8619

+ 0
- 1
backend/lib/services/matchqueue.js Voir le fichier

@@ -87,7 +87,6 @@ module.exports = class MatchQueueService extends Schmervice.Service {
87 87
                 is_deleted: false,
88 88
             })
89 89
         }
90
-
91 90
         return await this.getQueue(profileId)
92 91
     }
93 92
 }

+ 8388
- 10
backend/package-lock.json
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 2
- 1
backend/package.json Voir le fichier

@@ -36,6 +36,7 @@
36 36
     "ava": "^3.15.0",
37 37
     "eslint": "^7.28.0",
38 38
     "nodemon": "^2.0.7",
39
-    "nyc": "^15.1.0"
39
+    "nyc": "^15.1.0",
40
+    "sinon": "^11.1.2"
40 41
   }
41 42
 }

+ 0
- 44
backend/tests/brian.spec.js Voir le fichier

@@ -1,44 +0,0 @@
1
-'use strict'
2
-
3
-const test = require('ava')
4
-const Hapi = require('@hapi/hapi')
5
-const Schwifty = require('@hapipal/schwifty')
6
-const Schmervice = require('@hapipal/schmervice')
7
-
8
-const MatchQueService = require('../lib/services/matchqueue')
9
-const MatchQueModel = require('../lib/models/matchqueue')
10
-
11
-test('path / should return ok on GET', async t => {
12
-    const server = Hapi.server()
13
-
14
-    await server.register({
15
-        plugin: Schwifty,
16
-        options: {
17
-            knex: {
18
-                client: 'mysql',
19
-                useNullAsDefault: true,
20
-                connection: {
21
-                    host: process.env.DB_HOST,
22
-                    user: process.env.DB_USER,
23
-                    password: process.env.DB_ROOT_PASSWORD,
24
-                    database: process.env.DB_NAME,
25
-                },
26
-            },
27
-        },
28
-    })
29
-
30
-    await server.register(Schmervice)
31
-    await server.registerModel(MatchQueModel)
32
-    await server.registerService(MatchQueService)
33
-
34
-    // I can't quite wrap my head around why this wouldn't at least reference this.server.models()
35
-    // returns TypeError {
36
-    // message: 'Cannot read properties of undefined (reading \ 'models\')'
37
-    // }
38
-
39
-    const matchQueService = new MatchQueService()
40
-    const potentials = await matchQueService.getQueue()
41
-    console.log(potentials)
42
-    t.is(payload, '{"test":"hello, world"}')
43
-    t.is(statusCode, 200)
44
-})

+ 0
- 19
backend/tests/example.test.js Voir le fichier

@@ -1,19 +0,0 @@
1
-'use strict'
2
-
3
-const test = require('ava')
4
-const Hapi = require('@hapi/hapi')
5
-
6
-const routeBase = '../lib/routes'
7
-
8
-const route = require(routeBase + '/example/base')
9
-
10
-test('path / should return ok on GET', async t => {
11
-    const server = Hapi.server()
12
-
13
-    server.route(route)
14
-
15
-    const { statusCode, payload } = await server.inject(route.path)
16
-
17
-    t.is(payload, '{"test":"hello, world"}')
18
-    t.is(statusCode, 200)
19
-})

+ 76
- 0
backend/tests/matchqueue.spec.js Voir le fichier

@@ -0,0 +1,76 @@
1
+'use strict'
2
+
3
+const test = require('ava')
4
+const { stub } = require('sinon')
5
+const Hapi = require('@hapi/hapi')
6
+
7
+const plugin = require('../lib/plugins/profile')
8
+
9
+const MatchQueue = require('../lib/models/matchqueue')
10
+
11
+test('path / should return ok on GET', async t => {
12
+    /**
13
+     * Create a new server and register services,
14
+     * models and routes for testing
15
+     * -
16
+     * NOTE: We use a mocked registerModel() and register
17
+     * models manually. Normally this is handled by
18
+     * Schwifty at runtime.
19
+     */
20
+    const server = Hapi.server()
21
+    server.registerModel = () => {}
22
+    server.models = () => ({ MatchQueue })
23
+    await plugin.register(server)
24
+
25
+    /**
26
+     * Route parameters
27
+     */
28
+    const profile_id = 1
29
+    const target_id = 2
30
+    const reinsert = true
31
+
32
+    /**
33
+     * Replace Objection model methods with our own custom functions
34
+     * !: Kinda janky - might be better to temp knex sqlite instance
35
+     */
36
+    stub(server.models()['MatchQueue'], 'query').returns({
37
+        // Mocked for markAsDeleted()
38
+        patch: () => ({
39
+            where: () => ({
40
+                andWhere: () => ({
41
+                    first: () => ({ patch: 'bop' }),
42
+                }),
43
+            }),
44
+        }),
45
+        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)
49
+        },
50
+        // Mocked for getQueue()
51
+        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
+            },
59
+        }),
60
+    })
61
+
62
+    /**
63
+     * Test the server with registered models and services
64
+     */
65
+    const { payload } = await server.inject({
66
+        method: 'PATCH',
67
+        url: `/${profile_id}/queue/${target_id}/delete?reinsert=${reinsert}`,
68
+    })
69
+    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
+    ])
76
+})

Chargement…
Annuler
Enregistrer