From ac2dc8f1c4eaf0d6811c30d90f6f2a6113fe75c9 Mon Sep 17 00:00:00 2001 From: BoD Date: Fri, 2 May 2025 18:12:21 +0200 Subject: [PATCH] Add a test for watchers being notified after a clear (issue #78) --- .../src/commonTest/kotlin/WatcherTest.kt | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/normalized-cache/src/commonTest/kotlin/WatcherTest.kt b/tests/normalized-cache/src/commonTest/kotlin/WatcherTest.kt index bed32232..1b8cef13 100644 --- a/tests/normalized-cache/src/commonTest/kotlin/WatcherTest.kt +++ b/tests/normalized-cache/src/commonTest/kotlin/WatcherTest.kt @@ -248,6 +248,40 @@ class WatcherTest { job.cancel() } + @Test + fun differentQueryTriggersWatcherAfterCleared() = runTest(before = { setUp() }) { + val channel = Channel() + + // The first query should get a "R2-D2" name + val episodeHeroNameWithIdQuery = EpisodeHeroNameWithIdQuery(Episode.EMPIRE) + apolloClient.enqueueTestResponse(episodeHeroNameWithIdQuery, episodeHeroNameWithIdData) + val job = launch { + apolloClient.query(episodeHeroNameWithIdQuery).watch().collect { + channel.send(it.data) + } + } + + // Cache miss is emitted first (null data) + assertNull(channel.awaitElement()) + assertEquals(channel.awaitElement()?.hero?.name, "R2-D2") + + // Clear the cache + cacheManager.clearAll().also { cacheManager.publish(CacheManager.ALL_KEYS) } + // Cache miss due to the cache being cleared + assertNull(channel.awaitElement()) + + // Another newer call gets updated information with "Artoo" + val heroAndFriendsNamesWithIDsQuery = HeroAndFriendsNamesWithIDsQuery(Episode.NEWHOPE) + apolloClient.enqueueTestResponse(heroAndFriendsNamesWithIDsQuery, heroAndFriendsNamesWithIDsNameChangedData) + apolloClient.query(heroAndFriendsNamesWithIDsQuery) + .fetchPolicy(FetchPolicy.NetworkOnly) + .execute() + // Cache miss due to the cache being cleared + assertNull(channel.awaitElement()) + + job.cancel() + } + /** * Same as noChangeSameQuery with different queries */