@@ -80,52 +80,57 @@ describe('Lesson3Test', () => {
80
80
maybeIt (
81
81
'checks multiple choice answers are configured correctly' ,
82
82
async ( ) => {
83
- for ( const [ providerName , questions ] of quizQuestionsByProvider ) {
84
- for ( const question of questions ) {
85
- if ( ! ( question instanceof MultipleChoiceQuizQuestion ) ) {
86
- continue ;
87
- }
88
-
89
- // Assert that multiple choice questions have at least one correct answer.
90
- const choices = question . getAnswerChoices ( ) ;
91
- const areAnswersValid = await Promise . all (
92
- [ ...choices ] . map ( async ( choice ) => {
93
- return quizConfig . checkAnswer (
94
- providerName ,
95
- question . getQuestionNumber ( ) ,
96
- choice ,
97
- ) ;
98
- } ) ,
99
- ) ;
100
-
101
- expect ( areAnswersValid . some ( ( isCorrect ) => isCorrect ) ) . toBe ( true ) ;
83
+ const { providerName, questions } = getQuestionsFromCurrentProvider ( ) ;
84
+ for ( const question of questions ) {
85
+ if ( ! ( question instanceof MultipleChoiceQuizQuestion ) ) {
86
+ continue ;
102
87
}
88
+
89
+ // Assert that multiple choice questions have at least one correct answer.
90
+ const choices = question . getAnswerChoices ( ) ;
91
+ const areAnswersValid = await Promise . all (
92
+ [ ...choices ] . map ( async ( choice ) => {
93
+ return quizConfig . checkAnswer (
94
+ providerName ,
95
+ question . getQuestionNumber ( ) ,
96
+ choice ,
97
+ ) ;
98
+ } ) ,
99
+ ) ;
100
+
101
+ expect ( areAnswersValid . some ( ( isCorrect ) => isCorrect ) ) . toBe ( true ) ;
103
102
}
104
103
} ,
105
104
) ;
106
105
107
106
maybeIt ( 'checks for correct answers' , async ( ) => {
107
+ const { providerName, questions } = getQuestionsFromCurrentProvider ( ) ;
108
+ for ( const question of questions ) {
109
+ const actualAnswer = question . getAnswer ( ) ;
110
+ softExpect ( actualAnswer ) . not . toBe ( AnswerChoice . UNANSWERED ) ;
111
+ softExpect (
112
+ await quizConfig . checkAnswer (
113
+ providerName ,
114
+ question . getQuestionNumber ( ) ,
115
+ actualAnswer ,
116
+ ) ,
117
+ ) . toBe ( true ) ;
118
+ }
119
+ } ) ;
120
+
121
+ function getQuestionsFromCurrentProvider ( ) : {
122
+ providerName : string ;
123
+ questions : QuizQuestion [ ] ;
124
+ } {
108
125
const targetProviderName = process . env . PROVIDER_NAME ?. trim ( ) || '' ;
109
126
110
127
if ( ! quizQuestionsByProvider . has ( targetProviderName ) ) {
111
128
throw new Error ( `Unknown provider name: ${ targetProviderName } ` ) ;
112
129
}
113
130
114
- for ( const [ providerName , questions ] of quizQuestionsByProvider ) {
115
- if ( providerName !== process . env . PROVIDER_NAME ?. trim ( ) ) {
116
- continue ;
117
- }
118
- for ( const question of questions ) {
119
- const actualAnswer = question . getAnswer ( ) ;
120
- softExpect ( actualAnswer ) . not . toBe ( AnswerChoice . UNANSWERED ) ;
121
- softExpect (
122
- await quizConfig . checkAnswer (
123
- providerName ,
124
- question . getQuestionNumber ( ) ,
125
- actualAnswer ,
126
- ) ,
127
- ) . toBe ( true ) ;
128
- }
129
- }
130
- } ) ;
131
+ return {
132
+ providerName : targetProviderName ,
133
+ questions : quizQuestionsByProvider . get ( targetProviderName ) || [ ] ,
134
+ } ;
135
+ }
131
136
} ) ;
0 commit comments