package Ġcom . ta ilo red apps . count rie sex ample Ċ Ċ import Ġcom . ta ilo red apps . android ap pt emplate . base . ui . Async Ċ import Ġcom . ta ilo red apps . count rie sex ample . core . Countries Provider Ċ import Ġcom . ta ilo red apps . count rie sex ample . core . model . Country Ċ import Ġcom . ta ilo red apps . count rie sex ample . overview . Overview ViewModel Ċ import Ġio . mock k . Runs Ċ import Ġio . mock k . co Every Ċ import Ġio . mock k . co Verify Ċ import Ġio . mock k . every Ċ import Ġio . mock k . just Ċ import Ġio . mock k . mock k Ċ import Ġio . mock k . verify Ċ import Ġkotlinx . coroutines . channels . Con fl ated Broadcast Channel Ċ import Ġkotlinx . coroutines . flow . Flow Ċ import Ġkotlinx . coroutines . flow . as Flow Ċ import Ġkotlinx . coroutines . flow . flow Of Ċ import Ġkotlinx . coroutines . flow . toList Ċ import Ġkotlinx . coroutines . launch Ċ import Ġkotlinx . coroutines . test . Test Coroutine Scope Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġkotlin . test . assertEquals Ċ Ċ internal Ġclass ĠOverview ViewModel Test Ġ{ ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġtest Scope Rule Ġ= ĠTest Coroutine Scope Rule ( override Main Dispatcher Ġ= Ġtrue ) ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġsut : ĠOverview ViewModel ĊĊĠĠĠ Ġprivate Ġval Ġprovider : ĠCount ries Provider Ġ= Ġmock k () ĊĊĠĠĠ Ġ@ Before ĊĠĠĠ Ġfun Ġbefore () Ġ{ ĊĠĠĠĠĠĠĠ Ġsut Ġ= ĠOverview ViewModel ( provider ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Load All Countries On Start () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġprovider . getCount ries () Ġ} Ġreturns Ġflow Of ( listOf ( mock Country )) ĊĊĠĠĠĠĠĠĠ Ġsut . state ĊĊĠĠĠĠĠĠĠ Ġverify ( exact ly Ġ= Ġ 1 ) Ġ{ Ġprovider . getCount ries () Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Reload Action Triggers Refresh Countries Call () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġprovider . getCount ries () Ġ} Ġreturns Ġflow Of ( listOf ( mock Country )) ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ Ġprovider . refresh Countries () Ġ} Ġjust ĠRuns ĊĊĠĠĠĠĠĠĠ Ġsut . dispatch ( Overview ViewModel . Action . Reload ) ĊĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ Ġprovider . refresh Countries () Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Reload Action Sets Loading And Success State () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġchannel Ġ= ĠCon fl ated Broadcast Channel ( listOf ( mock Country )) ĊĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġprovider . getCount ries () Ġ} Ġreturns Ġchannel . as Flow () ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ Ġprovider . refresh Countries () Ġ} Ġjust ĠRuns ĊĊĠĠĠĠĠĠĠ Ġval Ġstates Ġ= Ġsut . state . test In ( test Scope Rule ) ĊĊĠĠĠĠĠĠĠ Ġsut . dispatch ( Overview ViewModel . Action . Reload ) ĊĊĠĠĠĠĠĠĠ Ġchannel . offer ( listOf ( mock Country , Ġmock Country )) ĊĊĠĠĠĠĠĠĠ Ġval Ġstate Load s Ġ= Ġstates . map Ġ{ Ġit . countries Load Ġ} ĊĠĠĠĠĠĠĠ ĠassertEquals ( Async . Success ( Unit ), Ġstate Load s [ 0 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Async . Loading , Ġstate Load s [ 1 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Async . Success ( Unit ), Ġstate Load s [ 2 ]) ĊĊĠĠĠĠĠĠĠ Ġval Ġstate Countries Ġ= Ġstates . map Ġ{ Ġit . countries Ġ} ĊĠĠĠĠĠĠĠ ĠassertEquals ( listOf ( mock Country ), Ġstate Countries [ 0 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( listOf ( mock Country ), Ġstate Countries [ 1 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( listOf ( mock Country , Ġmock Country ), Ġstate Countries [ 2 ]) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( 3 , Ġstates . count ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Reload Action Sets Error State On Api Failure () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġerror To Throw Ġ= ĠThrowable () ĊĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġprovider . getCount ries () Ġ} Ġreturns Ġflow Of ( listOf ( mock Country )) ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ Ġprovider . refresh Countries () Ġ} Ġthrows Ġerror To Throw ĊĊĠĠĠĠĠĠĠ Ġval Ġstates Ġ= Ġsut . state . test In ( test Scope Rule ) ĊĊĠĠĠĠĠĠĠ Ġsut . dispatch ( Overview ViewModel . Action . Reload ) ĊĊĠĠĠĠĠĠĠ Ġval Ġstate Load s Ġ= Ġstates . map Ġ{ Ġit . countries Load Ġ} ĊĠĠĠĠĠĠĠ ĠassertEquals ( Async . Success ( Unit ), Ġstate Load s [ 0 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Async . Loading , Ġstate Load s [ 1 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( Async . Error ( error To Throw ), Ġstate Load s [ 2 ]) ĊĊĠĠĠĠĠĠĠ Ġval Ġstate Countries Ġ= Ġstates . map Ġ{ Ġit . countries Ġ} ĊĠĠĠĠĠĠĠ ĠassertEquals ( listOf ( mock Country ), Ġstate Countries [ 0 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( listOf ( mock Country ), Ġstate Countries [ 1 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( listOf ( mock Country ), Ġstate Countries [ 2 ]) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( 3 , Ġstates . count ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Toggle Favorite Action Triggers Toggle Favorite In Repo () Ġ{ ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġprovider . getCount ries () Ġ} Ġreturns Ġflow Of ( listOf ( mock Country )) ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ Ġprovider . toggle Favorite ( any ()) Ġ} Ġjust ĠRuns ĊĊĠĠĠĠĠĠĠ Ġsut . dispatch ( Overview ViewModel . Action . Toggle Favorite ( mock Country )) ĊĊĠĠĠĠĠĠĠ Ġco Verify Ġ{ Ġprovider . toggle Favorite ( mock Country ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Toggle Favorite Action Sets Success State With Toggle d Favorite () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġinitial Mock Country Ġ= Ġmock Country ĊĠĠĠĠĠĠĠ Ġval Ġfavorite Toggle d Mock Country Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġinitial Mock Country . copy ( favorite Ġ= Ġ! initial Mock Country . favorite ) ĊĊĠĠĠĠĠĠĠ Ġval Ġchannel Ġ= ĠCon fl ated Broadcast Channel ( listOf ( initial Mock Country )) ĊĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġprovider . getCount ries () Ġ} Ġreturns Ġchannel . as Flow () ĊĠĠĠĠĠĠĠ Ġco Every Ġ{ Ġprovider . toggle Favorite ( any ()) Ġ} Ġjust ĠRuns ĊĊĠĠĠĠĠĠĠ Ġval Ġstates Ġ= Ġsut . state . test In ( test Scope Rule ) ĊĊĠĠĠĠĠĠĠ Ġsut . dispatch ( Overview ViewModel . Action . Toggle Favorite ( initial Mock Country )) ĊĊĠĠĠĠĠĠĠ Ġchannel . offer ( listOf ( favorite Toggle d Mock Country )) ĊĊĠĠĠĠĠĠĠ Ġval Ġstate Countries Ġ= Ġstates . map Ġ{ Ġit . countries Ġ} ĊĠĠĠĠĠĠĠ ĠassertEquals ( listOf ( initial Mock Country ), Ġstate Countries [ 0 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( listOf ( favorite Toggle d Mock Country ), Ġstate Countries [ 1 ]) ĊĠĠĠĠĠĠĠ ĠassertEquals ( 2 , Ġstate Countries . count ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmock Country Ġ= ĠCountry ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ"", Ġ"", Ġ"", Ġnull , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfalse , Ġ 0 , Ġ 0 . 0 , Ġ"", ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ"", Ġ"", Ġ"", Ġempty List () ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ} Ċ } Ċ Ċ private Ġfun Ġ< T > ĠFlow < T >. test In ( scope : ĠTest Coroutine Scope ): ĠList < T > Ġ{ ĊĠĠĠ Ġval Ġstates Ġ= Ġmutable ListOf < T >() ĊĠĠĠ Ġscope . launch Ġ{ Ġto List ( states ) Ġ} ĊĠĠĠ Ġreturn Ġstates Ċ }