I'm using dataStore preferences to store boolean value locally and uable to store it. I would like to write the unit test cases for dataStore implementation functions.
Below are the methods that I have created for saving and fetching the value. can someone help here how to write unit test cases for TestDataSourceImpl class.
interface TestDataSource {
suspend fun setTestFlag(flag: Boolean)
suspend fun getTestFlag(): Flow<Boolean?>
}
class TestDataSourceImpl : TestDataSource {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "test")
val context: Context by kInject()
companion object {
val testFlag= booleanPreferencesKey("testKey")
}
override suspend fun setTestFlag(flag: Boolean) {
context.dataStore.edit {
it[testFlag] = flag
}
}
override suspend fun getTestFlag(): Flow<Boolean?> {
return flow {
emit(context.dataStore.data.map { value ->
value[testFlag]
}.first())
}
}
}
interface TestDataSource {
suspend fun setTestFlag(flag: Boolean)
suspend fun getTestFlag(): Flow<Boolean?>
}
class TestDataSourceImpl : TestDataSource {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "test")
val context: Context by kInject()
companion object {
val testFlag= booleanPreferencesKey("testKey")
}
override suspend fun setTestFlag(flag: Boolean) {
context.dataStore.edit {
it[testFlag] = flag
}
}
override suspend fun getTestFlag(): Flow<Boolean?> {
return flow {
emit(context.dataStore.data.map { value ->
value[testFlag]
}.first())
}
}
}