Skip to content

Commit f9dfca7

Browse files
Add unit tests
1 parent 8c4e2a4 commit f9dfca7

File tree

1 file changed

+56
-19
lines changed
  • composeApp/src/desktopTest/kotlin/com/sebastianneubauer/jsontreeviewer

1 file changed

+56
-19
lines changed

composeApp/src/desktopTest/kotlin/com/sebastianneubauer/jsontreeviewer/ViewModelTest.kt

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ import org.junit.Test
99
import java.io.File
1010
import java.nio.file.Files
1111
import kotlin.test.assertEquals
12-
import kotlin.time.TestTimeSource
1312

1413
@OptIn(ExperimentalCoroutinesApi::class)
1514
class ViewModelTest {
1615

1716
private val dispatcher = UnconfinedTestDispatcher()
18-
private val timeSource = TestTimeSource()
1917
private val json = "{\"Hello\": \"World\"}"
2018

2119
private val underTest = ViewModel(
2220
coroutineScope = TestScope(dispatcher),
2321
ioDispatcher = dispatcher,
24-
timeSource = timeSource
22+
)
23+
24+
private val contentState = State.Content(
25+
json = json,
26+
searchDirection = null,
27+
displayMode = Contract.DisplayMode.Render
2528
)
2629

2730
private fun getFile(): File {
@@ -42,17 +45,7 @@ class ViewModelTest {
4245
underTest.updateDragAndDropState(DragAndDropState.Success(file))
4346

4447
assertEquals(
45-
expected = State.Content(
46-
json = json,
47-
searchDirection = null,
48-
stats = Contract.Stats(
49-
filePath = file.path,
50-
fileName = file.name,
51-
fileSize = "0.02KB",
52-
fileReadTime = "0.000ms",
53-
fileLines = "1"
54-
)
55-
),
48+
expected = contentState,
5649
actual = underTest.state.value
5750
)
5851
}
@@ -101,15 +94,59 @@ class ViewModelTest {
10194
}
10295

10396
@Test
104-
fun `jsonTree parsing error returns Error state`() {
105-
val throwable = Throwable(message = "")
106-
underTest.showJsonParsingError(throwable)
97+
fun `updateDisplayMode updates the Content state with the new DisplayMode`() {
98+
// Arrange: Start in Content state with Render mode
99+
val file = getFile()
100+
underTest.updateDragAndDropState(DragAndDropState.Success(file))
101+
assertEquals(
102+
expected = contentState,
103+
actual = underTest.state.value
104+
)
105+
106+
underTest.updateDisplayMode(Contract.DisplayMode.Edit)
107107

108108
assertEquals(
109-
expected = State.Error(
110-
error = Contract.ErrorType.JsonParserError(message = throwable.localizedMessage)
109+
expected = contentState.copy(
110+
displayMode = Contract.DisplayMode.Edit
111111
),
112112
actual = underTest.state.value
113113
)
114114
}
115+
116+
@Test
117+
fun `updateJson updates the Content state with the new json`() {
118+
// Arrange: Start in Content state with Render mode
119+
val file = getFile()
120+
underTest.updateDragAndDropState(DragAndDropState.Success(file))
121+
assertEquals(
122+
expected = contentState,
123+
actual = underTest.state.value
124+
)
125+
126+
underTest.updateJson("hello")
127+
128+
assertEquals(
129+
expected = contentState.copy(json = "hello"),
130+
actual = underTest.state.value
131+
)
132+
}
133+
134+
@Test
135+
fun `reset updates the state to the initial state`() {
136+
// Arrange: Start in Content state with Render mode
137+
val file = getFile()
138+
underTest.updateDragAndDropState(DragAndDropState.Success(file))
139+
assertEquals(
140+
expected = contentState,
141+
actual = underTest.state.value
142+
)
143+
144+
underTest.reset()
145+
146+
assertEquals(
147+
expected = State.Initial,
148+
actual = underTest.state.value
149+
)
150+
}
151+
115152
}

0 commit comments

Comments
 (0)