Skip to content

Commit

Permalink
return null instead of empty string (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndurell authored Sep 26, 2023
1 parent aea1bb4 commit 787efac
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
4 changes: 3 additions & 1 deletion sdk/core/src/main/java/com/klaviyo/core/model/DataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ interface DataStore {
* @param fallback
*/
fun DataStore.fetchOrCreate(key: String, fallback: () -> String): String =
fetch(key) ?: fallback().also { store(key, it) }
fetch(key) ?: fallback().also {
store(key, it)
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal object SharedPreferencesDataStore : DataStore {
* @return The value read from the shared preferences for the given key
*/
override fun fetch(key: String): String? {
return openSharedPreferences().getString(key, "")
return openSharedPreferences().getString(key, null)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ internal class SharedPreferencesDataStoreTest : BaseTest() {
fun `Fetch or create uses Klaviyo preferences`() {
withPreferenceMock()
withWriteStringMock(stubKey, stubValue)
every { preferenceMock.getString(stubKey, "") } returns null
every { preferenceMock.getString(stubKey, null) } returns null

SharedPreferencesDataStore.fetchOrCreate(stubKey) { stubValue }

verify { contextMock.getSharedPreferences(KLAVIYO_PREFS_NAME, Context.MODE_PRIVATE) }
verify { preferenceMock.getString(stubKey, "") }
verify { preferenceMock.getString(stubKey, null) }
verify { preferenceMock.edit() }
verify { editorMock.putString(stubKey, stubValue) }
verify { editorMock.apply() }
Expand Down Expand Up @@ -92,13 +92,13 @@ internal class SharedPreferencesDataStoreTest : BaseTest() {
val expectedString = "123" + Math.random().toString()

withPreferenceMock()
every { preferenceMock.getString(stubKey, "") } returns expectedString
every { preferenceMock.getString(stubKey, null) } returns expectedString

val actualString = SharedPreferencesDataStore.fetch(key = stubKey)

assertEquals(expectedString, actualString)
verify { contextMock.getSharedPreferences(KLAVIYO_PREFS_NAME, Context.MODE_PRIVATE) }
verify { preferenceMock.getString(stubKey, "") }
verify { preferenceMock.getString(stubKey, null) }
verify(inverse = true) { editorMock.apply() }
}
}

0 comments on commit 787efac

Please sign in to comment.