Переглянути джерело

:gear: tweaking imports for ava | adding rudimentary login service tests

tags/0.0.3^2
j 3 роки тому
джерело
коміт
8941de1be4

+ 2
- 2
frontend/src/services/grouping.service.js Переглянути файл

@@ -1,5 +1,5 @@
1
-import { db } from '../utils/db'
2
-import { Grouping, Profile } from '../entities'
1
+import { db } from '../utils/db.js'
2
+import { Grouping, Profile } from '../entities/index.js'
3 3
 
4 4
 /**
5 5
  * Get Memberships associated with a single Profile from the database and

+ 8
- 9
frontend/src/services/index.js Переглянути файл

@@ -1,9 +1,8 @@
1
-export * from './user.service'
2
-export * from './profile.service'
3
-export * from './grouping.service'
4
-export * from './survey.service'
5
-export * from './queue.service'
6
-export * from './chat.service'
7
-export * from './notification.service'
8
-export * from './login.service'
9
-
1
+export * from './user.service.js'
2
+export * from './profile.service.js'
3
+export * from './grouping.service.js'
4
+export * from './survey.service.js'
5
+export * from './queue.service.js'
6
+export * from './chat.service.js'
7
+export * from './notification.service.js'
8
+export * from './login.service.js'

+ 3
- 3
frontend/src/services/login.service.js Переглянути файл

@@ -4,8 +4,8 @@ import {
4 4
     fetchResponsesByProfileId,
5 5
     Chatter,
6 6
     StonkAlert,
7
-} from '../services'
8
-import { surveyFactory } from '../utils'
7
+} from '../services/index.js'
8
+import { surveyFactory } from '../utils/index.js'
9 9
 
10 10
 /**
11 11
  * Logged in profile state manager
@@ -146,4 +146,4 @@ class Login {
146 146
 
147 147
 const currentProfile = new Login()
148 148
 
149
-export { currentProfile }
149
+export { currentProfile, Login }

+ 1
- 1
frontend/src/services/notification.service.js Переглянути файл

@@ -1,4 +1,4 @@
1
-import { remote } from '../utils/db'
1
+import { remote } from '../utils/db.js'
2 2
 
3 3
 /**
4 4
  * Base notifier class

+ 7
- 3
frontend/src/services/profile.service.js Переглянути файл

@@ -1,5 +1,5 @@
1
-import { db } from '../utils/db'
2
-import { Profile } from '../entities/profile'
1
+import { db } from '../utils/db.js'
2
+import { Profile } from '../entities/profile/profile.js'
3 3
 
4 4
 /**
5 5
  * Get Profiles associated with a single user from the database and
@@ -30,4 +30,8 @@ const fetchProfileByProfileId = async profileId => {
30 30
     return profile
31 31
 }
32 32
 
33
-export { fetchProfilesByUserId, fetchProfileByProfileId, createProfileForUserId }
33
+export {
34
+    fetchProfilesByUserId,
35
+    fetchProfileByProfileId,
36
+    createProfileForUserId,
37
+}

+ 2
- 2
frontend/src/services/queue.service.js Переглянути файл

@@ -1,5 +1,5 @@
1
-import { db } from '../utils/db'
2
-import { Profile } from '../entities'
1
+import { db } from '../utils/db.js'
2
+import { Profile } from '../entities/index.js'
3 3
 
4 4
 /**
5 5
  * Get a match queue of profiles

+ 1
- 1
frontend/src/services/survey.service.js Переглянути файл

@@ -1,4 +1,4 @@
1
-import { db } from '../utils/db'
1
+import { db } from '../utils/db.js'
2 2
 
3 3
 /**
4 4
  * Get Survey for first time profile creation from the database and

+ 2
- 2
frontend/src/services/user.service.js Переглянути файл

@@ -1,4 +1,4 @@
1
-import { db } from '../utils/db'
1
+import { db } from '../utils/db.js'
2 2
 
3 3
 /**
4 4
  * Signup a new user
@@ -8,7 +8,7 @@ const signupUser = async user => {
8 8
     const payload = {
9 9
         user_name: user.name,
10 10
         user_email: user.email,
11
-        is_poster: user.seeking == 'position' ? 0 : 1
11
+        is_poster: user.seeking == 'position' ? 0 : 1,
12 12
     }
13 13
     return await db.post(`/user/signup`, payload)
14 14
 }

+ 39
- 30
frontend/src/utils/index.js Переглянути файл

@@ -1,18 +1,18 @@
1 1
 import Joi from 'joi'
2
+import { Connector } from './db.js'
3
+import { SurveyFactory } from './survey.js'
4
+import { possible } from './lang.js'
5
+import { pidMixin, cardMixin } from './mixins.js'
2 6
 
3
-import { Connector } from './db'
4
-import { SurveyFactory } from './survey'
5
-import { possible } from './lang'
6
-import { pidMixin, cardMixin } from './mixins'
7
-
8
-import { possibleZipcodes } from '../../../backend/db/data-generator/config.json'
7
+import config from '../../../backend/db/data-generator/config.json' assert { type: 'json' }
8
+const possibleZipcodes = config.possibleZipcodes
9 9
 
10 10
 const api = new Connector('kittens')
11 11
 
12 12
 const validatorMapping = {
13 13
     'input-string': Joi.string(),
14 14
     'tag-cloud': Joi.string(),
15
-    'checklist': Joi.string(),
15
+    checklist: Joi.string(),
16 16
     'input-slide': Joi.string(),
17 17
 }
18 18
 
@@ -22,10 +22,8 @@ const makeKebob = input => {
22 22
 
23 23
 const surveyFactory = new SurveyFactory(possible['usa'])
24 24
 
25
-
26 25
 const mixins = { pidMixin, cardMixin }
27 26
 
28
-
29 27
 const randomNumber = max => {
30 28
     return Math.floor(Math.random() * max) < 1
31 29
         ? 1
@@ -75,26 +73,37 @@ const randomMedia = () => {
75 73
 
76 74
 const randomSurveyResponses = count => {
77 75
     const surveyResponses = [
78
-        { id: null, "idOrPrompt": "email", "val": `${randomEmail()}` },
79
-        { id: null, "idOrPrompt": "name", "val": `john test-${count}` },
80
-        { id: 99, "idOrPrompt": 15, "val": randomValFrom(possible.usa.pronouns) },
81
-        { id: null, "idOrPrompt": "seeking", "val": Math.random() > 0.2 ? possible.usa.seeking[0] : possible.usa.seeking[1] },
82
-        { id: 99, "idOrPrompt": 13, "val": randomValFrom(possible.usa.urgency) },
83
-        { id: null, "idOrPrompt": "experience", "val": randomValFrom(possible.usa.experience) },
84
-        { id: 99, "idOrPrompt": 14, "val": "swe" },
85
-        { id: 99, "idOrPrompt": 10, "val": randomValFrom(possible.usa.duration) },
86
-        { id: 99, "idOrPrompt": 9, "val": randomValFrom(possible.usa.language) },
87
-        { id: 99, "idOrPrompt": 11, "val": randomValFrom(possible.usa.presence) },
88
-        { id: 99, "idOrPrompt": 7, "val": `${randomValFrom(possibleZipcodes)}` },
89
-        { id: 99, "idOrPrompt": 16, "val": `${randomNumber(55)}` },
90
-        { id: 99, "idOrPrompt": 12, "val": "this is a test of the survey signup" },
91
-        { id: 99, "idOrPrompt": 8, "val": randomMedia() },
92
-        { id: 99, "idOrPrompt": 1, "val": `${randomNumber(3) - randomNumber(3)}` },
93
-        { id: 99, "idOrPrompt": 2, "val": `${randomNumber(3) - randomNumber(3)}` },
94
-        { id: 99, "idOrPrompt": 3, "val": `${randomNumber(3) - randomNumber(3)}` },
95
-        { id: 99, "idOrPrompt": 4, "val": `${randomNumber(3) - randomNumber(3)}` },
96
-        { id: 99, "idOrPrompt": 5, "val": `${randomNumber(3) - randomNumber(3)}` },
97
-        { id: 99, "idOrPrompt": 6, "val": `${randomNumber(3) - randomNumber(3)}` }
76
+        { id: null, idOrPrompt: 'email', val: `${randomEmail()}` },
77
+        { id: null, idOrPrompt: 'name', val: `john test-${count}` },
78
+        { id: 99, idOrPrompt: 15, val: randomValFrom(possible.usa.pronouns) },
79
+        {
80
+            id: null,
81
+            idOrPrompt: 'seeking',
82
+            val:
83
+                Math.random() > 0.2
84
+                    ? possible.usa.seeking[0]
85
+                    : possible.usa.seeking[1],
86
+        },
87
+        { id: 99, idOrPrompt: 13, val: randomValFrom(possible.usa.urgency) },
88
+        {
89
+            id: null,
90
+            idOrPrompt: 'experience',
91
+            val: randomValFrom(possible.usa.experience),
92
+        },
93
+        { id: 99, idOrPrompt: 14, val: 'swe' },
94
+        { id: 99, idOrPrompt: 10, val: randomValFrom(possible.usa.duration) },
95
+        { id: 99, idOrPrompt: 9, val: randomValFrom(possible.usa.language) },
96
+        { id: 99, idOrPrompt: 11, val: randomValFrom(possible.usa.presence) },
97
+        { id: 99, idOrPrompt: 7, val: `${randomValFrom(possibleZipcodes)}` },
98
+        { id: 99, idOrPrompt: 16, val: `${randomNumber(55)}` },
99
+        { id: 99, idOrPrompt: 12, val: 'this is a test of the survey signup' },
100
+        { id: 99, idOrPrompt: 8, val: randomMedia() },
101
+        { id: 99, idOrPrompt: 1, val: `${randomNumber(3) - randomNumber(3)}` },
102
+        { id: 99, idOrPrompt: 2, val: `${randomNumber(3) - randomNumber(3)}` },
103
+        { id: 99, idOrPrompt: 3, val: `${randomNumber(3) - randomNumber(3)}` },
104
+        { id: 99, idOrPrompt: 4, val: `${randomNumber(3) - randomNumber(3)}` },
105
+        { id: 99, idOrPrompt: 5, val: `${randomNumber(3) - randomNumber(3)}` },
106
+        { id: 99, idOrPrompt: 6, val: `${randomNumber(3) - randomNumber(3)}` },
98 107
     ]
99 108
     return surveyResponses
100 109
 }
@@ -110,5 +119,5 @@ export {
110 119
     randomValFrom,
111 120
     randomMedia,
112 121
     randomName,
113
-    randomEmail
122
+    randomEmail,
114 123
 }

+ 29
- 15
frontend/src/utils/survey.js Переглянути файл

@@ -1,5 +1,5 @@
1
-import { Survey } from '../entities'
2
-import { fetchQuestions } from '../services'
1
+import { Survey } from '../entities/index.js'
2
+import { fetchQuestions } from '../services/index.js'
3 3
 
4 4
 class SurveyFactory {
5 5
     constructor(responses) {
@@ -7,39 +7,53 @@ class SurveyFactory {
7 7
         this.questionsFromDb = []
8 8
     }
9 9
     _setSteps(langFile) {
10
-        const stepsToProcess = [...Object.values(langFile) ]
10
+        const stepsToProcess = [...Object.values(langFile)]
11 11
         const seenIds = []
12 12
         const stepsInCommon = stepsToProcess.map(step => {
13 13
             // Match question to step
14
-            const match = this.questionsFromDb.filter(q => q.response_key_prompt == step)[0]
15
-            if(match) { seenIds.push(match.response_key_id) }
14
+            const match = this.questionsFromDb.filter(
15
+                q => q.response_key_prompt == step,
16
+            )[0]
17
+            if (match) {
18
+                seenIds.push(match.response_key_id)
19
+            }
16 20
             return {
17
-                response_key_id: match ? match.response_key_id: null,
18
-                response_key_category: match ? match.response_key_category: 'profile',
19
-                response_key_prompt: match ? match.response_key_prompt: step,
20
-                response_key_description: match ? match.response_key_description: null,
21
-                responses: this.responsesByCategory[step] ? this.responsesByCategory[step] : [] 
21
+                response_key_id: match ? match.response_key_id : null,
22
+                response_key_category: match
23
+                    ? match.response_key_category
24
+                    : 'profile',
25
+                response_key_prompt: match ? match.response_key_prompt : step,
26
+                response_key_description: match
27
+                    ? match.response_key_description
28
+                    : null,
29
+                responses: this.responsesByCategory[step]
30
+                    ? this.responsesByCategory[step]
31
+                    : [],
22 32
             }
23 33
         })
24
-        const unseen = this.questionsFromDb.filter(q => !seenIds.includes(q.response_key_id))
34
+        const unseen = this.questionsFromDb.filter(
35
+            q => !seenIds.includes(q.response_key_id),
36
+        )
25 37
         return [...stepsInCommon, ...unseen]
26 38
     }
27 39
     async getQuestions() {
28 40
         try {
29 41
             this.questionsFromDb = await fetchQuestions()
30 42
             return this.questionsFromDb
31
-        } catch(err) {
43
+        } catch (err) {
32 44
             console.error(err)
33 45
         }
34 46
     }
35 47
     async createSurvey(langFile, roleTree) {
36
-        if(!this.questionsFromDb.length) {
48
+        if (!this.questionsFromDb.length) {
37 49
             const res = await this.getQuestions()
38
-            console.warn(`Attempted to create a survey before getting questions: retrieved ${res.length} questions`)
50
+            console.warn(
51
+                `Attempted to create a survey before getting questions: retrieved ${res.length} questions`,
52
+            )
39 53
         }
40 54
         const steps = this._setSteps(langFile)
41 55
         return new Survey(steps, roleTree)
42 56
     }
43 57
 }
44 58
 
45
-export { SurveyFactory }
59
+export { SurveyFactory }

+ 58
- 0
frontend/tests/login.service.spec.js Переглянути файл

@@ -0,0 +1,58 @@
1
+import test from 'ava'
2
+import { Login } from '../src/services/login.service.js'
3
+
4
+test('Make sure we can instantiate login service', async t => {
5
+    const currentProfile = new Login()
6
+
7
+    t.is(currentProfile.isLoggedIn, false)
8
+    t.is(currentProfile.isComplete, false)
9
+    t.is(currentProfile.hasResponses, 0)
10
+
11
+    currentProfile.id.value = 99
12
+    currentProfile._loading.value = false
13
+    t.is(currentProfile.isLoggedIn, true)
14
+})
15
+
16
+test('Make sure login.tags can be set', async t => {
17
+    const currentProfile = new Login()
18
+    const testTags = ['x', 'y', 'z']
19
+    currentProfile.setTags(testTags)
20
+    t.is(currentProfile.tags, testTags)
21
+})
22
+
23
+test('Make sure login.responses can be set', async t => {
24
+    const currentProfile = new Login()
25
+    const testResponses = ['100', '200', '300']
26
+    currentProfile.setResponses(testResponses)
27
+    t.is(currentProfile.responses, testResponses)
28
+})
29
+
30
+test('Make sure login.groupings work correctly', async t => {
31
+    const currentProfile = new Login()
32
+    const testGroupings = [
33
+        { name: 'a', is_paired: false },
34
+        { name: 'b', is_paired: false },
35
+        { name: 'c', is_paired: true },
36
+    ]
37
+    currentProfile.groupings = testGroupings
38
+    t.deepEqual(
39
+        currentProfile.pendingGroupings.map(g => g.name),
40
+        ['a', 'b'],
41
+    )
42
+    t.deepEqual(
43
+        currentProfile.pairedGroupings.map(g => g.name),
44
+        ['c'],
45
+    )
46
+})
47
+
48
+test('Make sure login.logout() works', async t => {
49
+    const currentProfile = new Login()
50
+    currentProfile.id.value = 99
51
+
52
+    currentProfile.toaster = { name: 'dummy_toaster', stop: () => {} }
53
+    currentProfile.chatter = { name: 'dummy_chatter', stop: () => {} }
54
+    t.is(currentProfile.chatter.name, 'dummy_chatter')
55
+
56
+    currentProfile.logout()
57
+    t.is(currentProfile.id.value, null)
58
+})

Завантаження…
Відмінити
Зберегти