Bläddra i källkod

:gear: adding frontend tests

tags/0.0.1^2
j 3 år sedan
förälder
incheckning
b4e23b0a1f

+ 2
- 1
frontend/src/entities/chat-message/chat-message.schema.js Visa fil

@@ -9,7 +9,8 @@ import Joi from 'joi'
9 9
 const chatMessageSchema = {
10 10
     type: 'object',
11 11
     properties: Joi.object().keys({
12
-        description: Joi.string(),
12
+        description: Joi.string().required(),
13
+        type: Joi.string().required(),
13 14
     }),
14 15
     /** fields required before saving */
15 16
     required: ['description'],

+ 14
- 0
frontend/src/entities/chat-message/chat-message.spec.js Visa fil

@@ -0,0 +1,14 @@
1
+import test from 'ava'
2
+import { ChatMessage } from './index.js'
3
+
4
+test('instantiate a ChatMessage', async t => {
5
+    const m = new ChatMessage('foobar')
6
+    t.is(m.description, 'foobar')
7
+    t.is(m.type, 'chatmessage')
8
+    t.is(m.isValid(), true)
9
+})
10
+
11
+test('instantiate a bad ChatMessage', async t => {
12
+    const m = new ChatMessage()
13
+    t.is(m.isValid(), false)
14
+})

+ 37
- 0
frontend/src/entities/grouping/grouping.spec.js Visa fil

@@ -0,0 +1,37 @@
1
+import test from 'ava'
2
+import { Profile } from '../profile/index.js'
3
+import { Grouping } from './index.js'
4
+
5
+test('instantiate a Grouping', async t => {
6
+    const membershipInfoToTest = {
7
+        grouping_id: 8675309,
8
+        is_paired: false,
9
+        grouping_type: 'tester',
10
+    }
11
+    membershipInfoToTest.profile = new Profile({ email: 'grouping@test.com' })
12
+    const g = new Grouping(membershipInfoToTest)
13
+    t.is(g.grouping_id, membershipInfoToTest.grouping_id)
14
+    t.is(g.is_paired, membershipInfoToTest.is_paired)
15
+    t.is(g.isValid(), true)
16
+
17
+    t.is(g.profile.email, membershipInfoToTest.profile.email)
18
+    t.is(g.profile.type, 'profile')
19
+    t.is(g.profile.isValid(), true)
20
+})
21
+
22
+test('instantiate a bad Grouping', async t => {
23
+    const membershipInfoToTest = {
24
+        grouping_id: null,
25
+        is_paired: false,
26
+        grouping_type: 'tester',
27
+    }
28
+    membershipInfoToTest.profile = new Profile({ email: 'grouping@test.com' })
29
+    const g = new Grouping(membershipInfoToTest)
30
+    t.is(g.grouping_id, membershipInfoToTest.grouping_id)
31
+    t.is(g.is_paired, membershipInfoToTest.is_paired)
32
+    t.is(g.isValid(), false)
33
+
34
+    t.is(g.profile.email, membershipInfoToTest.profile.email)
35
+    t.is(g.profile.type, 'profile')
36
+    t.is(g.profile.isValid(), true)
37
+})

+ 1
- 0
frontend/src/entities/profile/profile.spec.js Visa fil

@@ -5,4 +5,5 @@ test('instantiate a Profile', async t => {
5 5
     const profToTest = { email: 'fake@email.com', user_name: 'bob' }
6 6
     const p = new Profile(profToTest)
7 7
     t.is(p.user_name, profToTest.user_name)
8
+    t.is(p.isValid(), true)
8 9
 })

+ 5
- 0
frontend/src/entities/survey/survey.spec.js Visa fil

@@ -0,0 +1,5 @@
1
+import test from 'ava'
2
+
3
+test('check survey entity', async t => {
4
+    t.pass()
5
+})

+ 17
- 7
frontend/src/services/chat.service.js Visa fil

@@ -1,6 +1,6 @@
1 1
 import PubNub from 'pubnub'
2 2
 
3
-import { ChatMessage } from '../entities'
3
+import { ChatMessage } from '../entities/index.js'
4 4
 
5 5
 const MAIN_CHANNEL = 'Channel-Siimee'
6 6
 
@@ -21,10 +21,7 @@ const _providerMethods = {
21 21
 /**
22 22
  * Breaking out as much pubnub specific flavor
23 23
  */
24
-const _setupPubnub = async uuid => {
25
-    const publishKey = import.meta.env.VITE_PUBNUB_PUBLISH_KEY
26
-    const subscribeKey = import.meta.env.VITE_PUBNUB_SUBSCRIBE_KEY
27
-
24
+const _setupPubnub = async (uuid, publishKey, subscribeKey) => {
28 25
     if (!uuid) return console.error('no pubnub uuid set')
29 26
 
30 27
     const pubnubClient = await new PubNub({
@@ -80,9 +77,22 @@ class Chatter {
80 77
         this.listeners.message = cb
81 78
     }
82 79
 
83
-    async setup(uuid, groupings) {
80
+    async setup(uuid, groupings, testing = false) {
84 81
         this.uuid = `${uuid}`
85
-        this.provider = await _setupPubnub(this.uuid)
82
+
83
+        // Pass api keys in ugle fasion for testing purposes
84
+        // TODO: refactor this with a mock
85
+        this.provider = testing
86
+            ? await _setupPubnub(
87
+                  this.uuid,
88
+                  testing.publishKey,
89
+                  testing.subscribeKey,
90
+              )
91
+            : await _setupPubnub(
92
+                  this.uuid,
93
+                  import.meta.env.VITE_PUBNUB_PUBLISH_KEY,
94
+                  import.meta.env.VITE_PUBNUB_SUBSCRIBE_KEY,
95
+              )
86 96
 
87 97
         //  step_1: build the this.groupings object from the backend
88 98
         //  await for the groupings to be fetched before subscribing to channels

+ 0
- 1
frontend/src/views/ChatView.vue Visa fil

@@ -83,7 +83,6 @@ export default {
83 83
                 if (!grouping) {
84 84
                     throw `no match found for ${currentProfile.id.value}`
85 85
                 }
86
-                console.log('grouping :', grouping)
87 86
                 this.target = grouping.profile
88 87
             } catch (err) {
89 88
                 console.error(err)

+ 40
- 0
frontend/tests/chat.service.spec.js Visa fil

@@ -0,0 +1,40 @@
1
+import test from 'ava'
2
+import { Chatter, MAIN_CHANNEL } from '../src/services/chat.service.js'
3
+
4
+test('Make sure we can instantiate Chatter', async t => {
5
+    const chatter = new Chatter()
6
+    t.is(chatter._subscriptions.length, 1)
7
+    t.is(chatter._subscriptions[0], MAIN_CHANNEL)
8
+
9
+    const testCb = () => {
10
+        const a = 'foo'
11
+        return a
12
+    }
13
+    chatter.setOnMessage(testCb)
14
+    t.is(chatter.listeners.message, testCb)
15
+
16
+    chatter._setupAllChannels([
17
+        { grouping_name: 'test-channel-01' },
18
+        { grouping_name: 'test-channel-02' },
19
+    ])
20
+    t.is(chatter.subscriptions.length, 2)
21
+    t.is(chatter.subscriptions[0], 'test-channel-01')
22
+    t.is(chatter.subscriptions[1], 'test-channel-02')
23
+
24
+    chatter.stop()
25
+    t.is(chatter._subscriptions[0], MAIN_CHANNEL)
26
+})
27
+
28
+test('Use PubNub via Chatter', async t => {
29
+    const chatter = new Chatter()
30
+    t.is(chatter._subscriptions.length, 1)
31
+    t.is(chatter._subscriptions[0], MAIN_CHANNEL)
32
+
33
+    const testUUID = 'test_123_uuid'
34
+    const subs = await chatter.setup(testUUID, [], {
35
+        publishKey: 'foo',
36
+        subscribeKey: 'bar',
37
+    })
38
+    t.is(chatter.uuid, testUUID)
39
+    t.deepEqual(subs, [])
40
+})

Laddar…
Avbryt
Spara