Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Null Pointer Exception in PublishSettingsViewModel #20745

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ constructor(

open fun start(postRepository: EditPostRepository?) {
editPostRepository = postRepository
val startCalendar = postRepository?.let { getCurrentPublishDateAsCalendar(it) }
?: localeManagerWrapper.getCurrentCalendar()
val startCalendar = postRepository?.takeIf { it.hasPost() }?.let {
getCurrentPublishDateAsCalendar(it)
} ?: localeManagerWrapper.getCurrentCalendar()
updateDateAndTimeFromCalendar(startCalendar)
onPostStatusChanged(postRepository?.getPost())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class EditPostPublishSettingsViewModelTest : BaseUnitTest() {
null
}
whenever(editPostRepository.getPost()).thenReturn(post)
whenever(editPostRepository.hasPost()).thenReturn(true)
}

@Test
Expand Down Expand Up @@ -450,4 +451,25 @@ class EditPostPublishSettingsViewModelTest : BaseUnitTest() {

assertThat(schedulingReminderPeriod).isEqualTo(ONE_HOUR)
}

@Test
fun `on start sets current date when post not present in the repository`() {
var uiModel: PublishUiModel? = null
viewModel.onUiModel.observeForever {
uiModel = it
}

whenever(editPostRepository.hasPost()).thenReturn(false)
whenever(editPostRepository.getPost()).thenReturn(null)

viewModel.start(editPostRepository)

assertThat(viewModel.year).isEqualTo(2019)
assertThat(viewModel.month).isEqualTo(6)
assertThat(viewModel.day).isEqualTo(6)
assertThat(viewModel.hour).isEqualTo(10)
assertThat(viewModel.minute).isEqualTo(20)

assertThat(uiModel!!.publishDateLabel).isEqualTo("Immediately")
}
}
Loading