@@ -4,7 +4,7 @@ import { NgbActiveModal } from "@ng-bootstrap/ng-bootstrap";
44import { Label , LabelAnnotation } from "@/types" ;
55import { ProblemService } from "@/services/problem.service" ;
66import { AuthService } from "@/services/auth.service" ;
7- import { BehaviorSubject } from "rxjs" ;
7+ import { BehaviorSubject , Observable } from "rxjs" ;
88
99describe ( "ManageLabelsModalComponent" , ( ) => {
1010 let component : ManageLabelsModalComponent ;
@@ -90,13 +90,9 @@ describe("ManageLabelsModalComponent", () => {
9090 } ) ;
9191
9292 it ( "should populate availableLabels$ with all labels initially" , fakeAsync ( ( ) => {
93- let availableLabels : Label [ ] = [ ] ;
94- component . availableLabels$ . subscribe ( labels => {
95- availableLabels = labels ;
96- } ) ;
97- tick ( ) ;
93+ const availableLabels = getSubscriptionValue ( component . availableLabels$ ) ;
9894
99- expect ( availableLabels . length ) . toBe ( 3 ) ;
95+ expect ( availableLabels ? .length ) . toBe ( 3 ) ;
10096 expect ( availableLabels ) . toContain ( testLabel1 ) ;
10197 expect ( availableLabels ) . toContain ( testLabel2 ) ;
10298 expect ( availableLabels ) . toContain ( testLabel3 ) ;
@@ -106,13 +102,9 @@ describe("ManageLabelsModalComponent", () => {
106102 component . form . controls . selectedLabelIds . setValue ( [ 1 , 2 ] ) ;
107103 tick ( ) ;
108104
109- let availableLabels : Label [ ] = [ ] ;
110- component . availableLabels$ . subscribe ( labels => {
111- availableLabels = labels ;
112- } ) ;
113- tick ( ) ;
105+ const availableLabels = getSubscriptionValue ( component . availableLabels$ ) ;
114106
115- expect ( availableLabels . length ) . toBe ( 1 ) ;
107+ expect ( availableLabels ? .length ) . toBe ( 1 ) ;
116108 expect ( availableLabels ) . toContain ( testLabel3 ) ;
117109 expect ( availableLabels ) . not . toContain ( testLabel1 ) ;
118110 expect ( availableLabels ) . not . toContain ( testLabel2 ) ;
@@ -145,34 +137,26 @@ describe("ManageLabelsModalComponent", () => {
145137 component . form . controls . selectedLabelIds . setValue ( [ testLabel1 . id ] ) ;
146138 tick ( ) ;
147139
148- let shownLabels : LabelAnnotation [ ] = [ ] ;
149- component . shownLabels$ . subscribe ( labels => {
150- shownLabels = labels ;
151- } ) ;
152- tick ( ) ;
140+ const shownLabels = getSubscriptionValue ( component . shownLabels$ ) ;
153141
154- expect ( shownLabels . length ) . toBe ( 1 ) ;
155- expect ( shownLabels [ 0 ] . label ) . toEqual ( testLabel1 ) ;
156- expect ( shownLabels [ 0 ] . id ) . toBeNull ( ) ;
157- expect ( shownLabels [ 0 ] . attachedByCurrentUser ) . toBe ( true ) ;
158- expect ( shownLabels [ 0 ] . createdBy ) . toBe ( "Current User" ) ;
142+ expect ( shownLabels ? .length ) . toBe ( 1 ) ;
143+ expect ( shownLabels ?. [ 0 ] . label ) . toEqual ( testLabel1 ) ;
144+ expect ( shownLabels ?. [ 0 ] . id ) . toBeNull ( ) ;
145+ expect ( shownLabels ?. [ 0 ] . attachedByCurrentUser ) . toBe ( true ) ;
146+ expect ( shownLabels ?. [ 0 ] . createdBy ) . toBe ( "Current User" ) ;
159147 } ) ) ;
160148
161149 it ( "should preserve existing LabelAnnotation objects in shownLabels$" , fakeAsync ( ( ) => {
162150 component . currentAnnotations = [ testAttachedLabel ] ;
163151 component . form . controls . selectedLabelIds . setValue ( [ testLabel1 . id ] ) ;
164152 tick ( ) ;
165153
166- let shownLabels : LabelAnnotation [ ] = [ ] ;
167- component . shownLabels$ . subscribe ( labels => {
168- shownLabels = labels ;
169- } ) ;
170- tick ( ) ;
154+ const shownLabels = getSubscriptionValue ( component . shownLabels$ ) ;
171155
172- expect ( shownLabels . length ) . toBe ( 1 ) ;
173- expect ( shownLabels [ 0 ] ) . toBe ( testAttachedLabel ) ;
174- expect ( shownLabels [ 0 ] . id ) . toBe ( 999 ) ;
175- expect ( shownLabels [ 0 ] . createdBy ) . toBe ( "Other User" ) ;
156+ expect ( shownLabels ? .length ) . toBe ( 1 ) ;
157+ expect ( shownLabels ?. [ 0 ] ) . toBe ( testAttachedLabel ) ;
158+ expect ( shownLabels ?. [ 0 ] . id ) . toBe ( 999 ) ;
159+ expect ( shownLabels ?. [ 0 ] . createdBy ) . toBe ( "Other User" ) ;
176160 } ) ) ;
177161
178162 it ( "should close modal with transformed data" , ( ) => {
@@ -211,21 +195,13 @@ describe("ManageLabelsModalComponent", () => {
211195 component . form . controls . selectedLabelIds . setValue ( [ testLabel1 . id ] ) ;
212196 tick ( ) ;
213197
214- let shownLabels : LabelAnnotation [ ] = [ ] ;
215- component . shownLabels$ . subscribe ( labels => {
216- shownLabels = labels ;
217- } ) ;
218- tick ( ) ;
198+ const shownLabels = getSubscriptionValue ( component . shownLabels$ ) ;
219199
220- expect ( shownLabels [ 0 ] . createdBy ) . toBe ( "Unknown user" ) ;
200+ expect ( shownLabels ?. [ 0 ] . createdBy ) . toBe ( "Unknown user" ) ;
221201 } ) ) ;
222202
223203 it ( "should set loadingLabels$ to false after labels are available" , fakeAsync ( ( ) => {
224- let loading = true ;
225- component . loadingLabels$ . subscribe ( isLoading => {
226- loading = isLoading ;
227- } ) ;
228- tick ( ) ;
204+ const loading = getSubscriptionValue ( component . loadingLabels$ ) ;
229205
230206 expect ( loading ) . toBe ( false ) ;
231207 } ) ) ;
@@ -248,12 +224,21 @@ describe("ManageLabelsModalComponent", () => {
248224 component . form . controls . selectedLabelIds . setValue ( [ 999 ] ) ; // Non-existent label ID
249225 tick ( ) ;
250226
251- let shownLabels : LabelAnnotation [ ] = [ ] ;
252- component . shownLabels$ . subscribe ( labels => {
253- shownLabels = labels ;
254- } ) ;
255- tick ( ) ;
227+ const shownLabels = getSubscriptionValue ( component . shownLabels$ ) ;
256228
257- expect ( shownLabels . length ) . toBe ( 0 ) ;
229+ expect ( shownLabels ? .length ) . toBe ( 0 ) ;
258230 } ) ) ;
259231} ) ;
232+
233+ /**
234+ * Subscribe to an observable and update the provided value variable with
235+ * emitted values.
236+ */
237+ function getSubscriptionValue < T > ( observ : Observable < T > ) : T | undefined {
238+ let value : T | undefined ;
239+ observ . subscribe ( val => {
240+ value = val ;
241+ } ) ;
242+ tick ( ) ;
243+ return value ;
244+ }
0 commit comments