@@ -102,37 +102,113 @@ describe('SurveyResultMongoRepository', () => {
102102 } ) ;
103103
104104 describe ( 'loadBySurveyId()' , ( ) => {
105- test ( 'Should load survey result' , async ( ) => {
105+ test ( 'Should load survey result with two users ' , async ( ) => {
106106 const sut = makeSut ( ) ;
107107 const survey = await mockSurvey ( ) ;
108- const account = await mockAccount ( ) ;
108+ const firstAccount = await mockAccount ( ) ;
109+ const secondAccount = await mockAccount ( ) ;
109110 await surveyResultCollection . insertMany ( [
110111 {
111112 surveyId : MongoHelper . objectId ( survey . id ) ,
112- accountId : MongoHelper . objectId ( account . id ) ,
113+ accountId : MongoHelper . objectId ( firstAccount . id ) ,
113114 answerId : survey . answers [ 0 ] . answerId ,
114115 date : new Date ( ) ,
115116 } ,
116117 {
117118 surveyId : MongoHelper . objectId ( survey . id ) ,
118- accountId : MongoHelper . objectId ( account . id ) ,
119+ accountId : MongoHelper . objectId ( secondAccount . id ) ,
119120 answerId : survey . answers [ 0 ] . answerId ,
120121 date : new Date ( ) ,
121122 } ,
122123 ] ) ;
123- const surveyResult = await sut . loadBySurveyId ( survey . id ) ;
124+ const surveyResult = await sut . loadBySurveyId ( survey . id , firstAccount . id ) ;
124125 expect ( surveyResult ) . toBeTruthy ( ) ;
125126 expect ( surveyResult . surveyId ) . toEqual ( survey . id ) ;
126127 expect ( surveyResult . answers [ 0 ] . count ) . toBe ( 2 ) ;
127128 expect ( surveyResult . answers [ 0 ] . percent ) . toBe ( 100 ) ;
129+ expect ( surveyResult . answers [ 0 ] . isCurrentAccountAnswer ) . toBe ( true ) ;
128130 expect ( surveyResult . answers [ 1 ] . count ) . toBe ( 0 ) ;
129131 expect ( surveyResult . answers [ 1 ] . percent ) . toBe ( 0 ) ;
132+ expect ( surveyResult . answers [ 1 ] . isCurrentAccountAnswer ) . toBe ( false ) ;
133+ } ) ;
134+
135+ test ( 'Should load survey result with three users' , async ( ) => {
136+ const sut = makeSut ( ) ;
137+ const survey = await mockSurvey ( ) ;
138+ const firstAccount = await mockAccount ( ) ;
139+ const secondAccount = await mockAccount ( ) ;
140+ const thirdAccount = await mockAccount ( ) ;
141+ await surveyResultCollection . insertMany ( [
142+ {
143+ surveyId : MongoHelper . objectId ( survey . id ) ,
144+ accountId : MongoHelper . objectId ( firstAccount . id ) ,
145+ answerId : survey . answers [ 0 ] . answerId ,
146+ date : new Date ( ) ,
147+ } ,
148+ {
149+ surveyId : MongoHelper . objectId ( survey . id ) ,
150+ accountId : MongoHelper . objectId ( secondAccount . id ) ,
151+ answerId : survey . answers [ 1 ] . answerId ,
152+ date : new Date ( ) ,
153+ } ,
154+ {
155+ surveyId : MongoHelper . objectId ( survey . id ) ,
156+ accountId : MongoHelper . objectId ( thirdAccount . id ) ,
157+ answerId : survey . answers [ 1 ] . answerId ,
158+ date : new Date ( ) ,
159+ } ,
160+ ] ) ;
161+ const surveyResult = await sut . loadBySurveyId (
162+ survey . id ,
163+ secondAccount . id
164+ ) ;
165+ console . log ( surveyResult ) ;
166+ expect ( surveyResult ) . toBeTruthy ( ) ;
167+ expect ( surveyResult . surveyId ) . toEqual ( survey . id ) ;
168+ expect ( surveyResult . answers [ 0 ] . count ) . toBe ( 2 ) ;
169+ expect ( surveyResult . answers [ 0 ] . percent ) . toBe ( 67 ) ;
170+ expect ( surveyResult . answers [ 0 ] . isCurrentAccountAnswer ) . toBe ( true ) ;
171+ expect ( surveyResult . answers [ 1 ] . count ) . toBe ( 1 ) ;
172+ expect ( surveyResult . answers [ 1 ] . percent ) . toBe ( 33 ) ;
173+ expect ( surveyResult . answers [ 1 ] . isCurrentAccountAnswer ) . toBe ( false ) ;
174+ } ) ;
175+
176+ test ( 'Should load survey result with third user no answers' , async ( ) => {
177+ const sut = makeSut ( ) ;
178+ const survey = await mockSurvey ( ) ;
179+ const firstAccount = await mockAccount ( ) ;
180+ const secondAccount = await mockAccount ( ) ;
181+ const thirdAccount = await mockAccount ( ) ;
182+ await surveyResultCollection . insertMany ( [
183+ {
184+ surveyId : MongoHelper . objectId ( survey . id ) ,
185+ accountId : MongoHelper . objectId ( firstAccount . id ) ,
186+ answerId : survey . answers [ 0 ] . answerId ,
187+ date : new Date ( ) ,
188+ } ,
189+ {
190+ surveyId : MongoHelper . objectId ( survey . id ) ,
191+ accountId : MongoHelper . objectId ( secondAccount . id ) ,
192+ answerId : survey . answers [ 1 ] . answerId ,
193+ date : new Date ( ) ,
194+ } ,
195+ ] ) ;
196+ const surveyResult = await sut . loadBySurveyId ( survey . id , thirdAccount . id ) ;
197+ expect ( surveyResult ) . toBeTruthy ( ) ;
198+ expect ( surveyResult . surveyId ) . toEqual ( survey . id ) ;
199+ expect ( surveyResult . answers [ 0 ] . count ) . toBe ( 1 ) ;
200+ expect ( surveyResult . answers [ 0 ] . percent ) . toBe ( 50 ) ;
201+ expect ( surveyResult . answers [ 0 ] . isCurrentAccountAnswer ) . toBe ( false ) ;
202+ expect ( surveyResult . answers [ 1 ] . count ) . toBe ( 1 ) ;
203+ expect ( surveyResult . answers [ 1 ] . percent ) . toBe ( 50 ) ;
204+ expect ( surveyResult . answers [ 1 ] . isCurrentAccountAnswer ) . toBe ( false ) ;
130205 } ) ;
131206
132207 test ( "Should return null if survey result don't have answers" , async ( ) => {
133208 const sut = makeSut ( ) ;
134209 const survey = await mockSurvey ( ) ;
135- const surveyResult = await sut . loadBySurveyId ( survey . id ) ;
210+ const account = await mockAccount ( ) ;
211+ const surveyResult = await sut . loadBySurveyId ( survey . id , account . id ) ;
136212 expect ( surveyResult ) . toBeNull ( ) ;
137213 } ) ;
138214 } ) ;
0 commit comments