@@ -17,20 +17,29 @@ package com.ichi2.anki.previewer
1717
1818import androidx.test.ext.junit.runners.AndroidJUnit4
1919import com.ichi2.anki.NotetypeFile
20- import com.ichi2.anki.libanki.testutils.AnkiTest
2120import com.ichi2.testutils.JvmTest
21+ import io.mockk.coEvery
22+ import io.mockk.spyk
2223import org.hamcrest.CoreMatchers.equalTo
2324import org.hamcrest.MatcherAssert.assertThat
2425import org.junit.Rule
2526import org.junit.Test
2627import org.junit.rules.TemporaryFolder
2728import org.junit.runner.RunWith
29+ import kotlin.test.assertNotEquals
2830
2931@RunWith(AndroidJUnit4 ::class )
3032class TemplatePreviewerViewModelTest : JvmTest () {
3133 @get:Rule
3234 val tempDirectory = TemporaryFolder ()
3335
36+ private fun createViewModel (arguments : TemplatePreviewerArguments ): TemplatePreviewerViewModel =
37+ spyk(TemplatePreviewerViewModel (arguments)).apply {
38+ // the default implementation requires the Collection media directory,
39+ // which needs a Robolectric setup with CollectionStorageMode.IN_MEMORY_WITH_MEDIA or ON_DISK
40+ coEvery { prepareCardTextForDisplay(any()) } answers { firstArg() }
41+ }
42+
3443 @Test
3544 fun `getCurrentTabIndex returns the correct tab if the first cloze isn't 1 and ord isn't 0` () =
3645 runClozeTest(ord = 6 , fields = mutableListOf (" {{c7::foo}} {{c4::bar}} {{c9::ha}}" )) {
@@ -57,9 +66,20 @@ class TemplatePreviewerViewModelTest : JvmTest() {
5766 ) {
5867 onPageFinished(false )
5968 assertThat(this .cardsWithEmptyFronts!! .await()[0 ], equalTo(false ))
60- assertThat(this .cardsWithEmptyFronts!! .await()[1 ], equalTo(true ))
69+ assertThat(this .cardsWithEmptyFronts.await()[1 ], equalTo(true ))
6170 }
6271
72+ @Test
73+ fun `card ords are changed` () {
74+ runClozeTest(tempDirectory = tempDirectory, fields = mutableListOf (" {{c1::one}} {{c2::bar}}" )) {
75+ onPageFinished(false )
76+ val ord1 = currentCard.await().ord
77+ onTabSelected(1 )
78+ val ord2 = currentCard.await().ord
79+ assertNotEquals(ord1, ord2)
80+ }
81+ }
82+
6383 private fun runClozeTest (
6484 ord : Int = 0,
6585 fields : MutableList <String >? = null,
@@ -79,25 +99,25 @@ class TemplatePreviewerViewModelTest : JvmTest() {
7999 tags = mutableListOf (),
80100 ord = ord,
81101 )
82- val viewModel = TemplatePreviewerViewModel (arguments)
102+ val viewModel = createViewModel (arguments)
83103 block(viewModel)
84104 }
85- }
86105
87- fun AnkiTest.runClozeTest (
88- ord : Int = 0,
89- tempDirectory : TemporaryFolder ,
90- fields : MutableList <String >? = null,
91- block : suspend TemplatePreviewerViewModel .() -> Unit ,
92- ) = runTest {
93- val notetype = col.notetypes.byName(" Cloze" )!!
94- val arguments =
95- TemplatePreviewerArguments (
96- notetypeFile = NotetypeFile (tempDirectory.root, notetype),
97- fields = fields ? : mutableListOf (" {{c1::foo}} {{c2::bar}}" , " anki" ),
98- tags = mutableListOf (),
99- ord = ord,
100- )
101- val viewModel = TemplatePreviewerViewModel (arguments)
102- block(viewModel)
106+ private fun runClozeTest (
107+ ord : Int = 0,
108+ tempDirectory : TemporaryFolder ,
109+ fields : MutableList <String >? = null,
110+ block : suspend TemplatePreviewerViewModel .() -> Unit ,
111+ ) = runTest {
112+ val notetype = col.notetypes.byName(" Cloze" )!!
113+ val arguments =
114+ TemplatePreviewerArguments (
115+ notetypeFile = NotetypeFile (tempDirectory.root, notetype),
116+ fields = fields ? : mutableListOf (" {{c1::foo}} {{c2::bar}}" , " anki" ),
117+ tags = mutableListOf (),
118+ ord = ord,
119+ )
120+ val viewModel = createViewModel(arguments)
121+ block(viewModel)
122+ }
103123}
0 commit comments