From bdc9b4a1f2b1e426e159d66860085f062c47a568 Mon Sep 17 00:00:00 2001 From: Abhash Kumar Singh Date: Thu, 19 Oct 2023 07:47:36 -0700 Subject: [PATCH] feat(datastore): Add `isLoaded` public property in List+Model (#3296) --- .../DataStore/Model/Lazy/List+Model.swift | 10 ++++++++++ .../AWSDataStorePluginTests/Core/ListTests.swift | 16 +++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift b/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift index 09c3b8a3cd..aee10e9e53 100644 --- a/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift +++ b/Amplify/Categories/DataStore/Model/Lazy/List+Model.swift @@ -26,6 +26,15 @@ public class List: Collection, Codable, ExpressibleByArrayLite /// The current state of lazily loaded list var loadedState: LoadedState + + /// Boolean property to check if list is loaded + public var isLoaded: Bool { + if case .loaded = loadedState { + return true + } + + return false + } /// The provider for fulfilling list behaviors let listProvider: AnyModelListProvider @@ -61,6 +70,7 @@ public class List: Collection, Codable, ExpressibleByArrayLite } } + // MARK: - Initializers public init(listProvider: AnyModelListProvider) { diff --git a/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift b/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift index 31bdb24e5c..b2dcb3b347 100644 --- a/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift +++ b/AmplifyPlugins/DataStore/Tests/AWSDataStorePluginTests/Core/ListTests.swift @@ -8,9 +8,9 @@ import Combine import XCTest -@testable import Amplify -@testable import AmplifyTestCommon -@testable import AWSDataStorePlugin +import Amplify +import AmplifyTestCommon +import AWSDataStorePlugin class ListTests: BaseDataStoreTests { @@ -25,15 +25,9 @@ class ListTests: BaseDataStoreTests { let postId = preparePostDataForTest() func checkComments(_ comments: List) async throws { - guard case .notLoaded = comments.loadedState else { - XCTFail("Should not be loaded") - return - } + XCTAssertFalse(comments.isLoaded) try await comments.fetch() - guard case .loaded = comments.loadedState else { - XCTFail("Should be loaded") - return - } + XCTAssertTrue(comments.isLoaded) XCTAssertEqual(comments.count, 2) expect.fulfill() }