Repository: realm/realm-swift
File: Realm/Tests/SwiftUITestHostUITests/SwiftUITestHostUITests.swift (Swift)

Code before:
            XCTAssert(cell1.staticTexts["changed"].exists)
            XCTAssert(cell2.staticTexts["changed"].exists)
        } else {
            XCTAssert(app.tables.otherElements.staticTexts["A"].exists)
            XCTAssert(app.tables.otherElements.staticTexts["c"].exists)
            let cell0 = app.tables.firstMatch.cells.element(boundBy: 0)
            XCTAssert(cell0.staticTexts["Another List"].exists)
            let cell2 = app.tables.cells.element(boundBy: 1)
            let cell3 = app.tables.cells.element(boundBy: 2)
            XCTAssert(cell2.staticTexts["changed"].exists)
            XCTAssert(cell3.staticTexts["changed"].exists)
            XCTAssertEqual(app.tables.firstMatch.cells.count, 3)
        }
        XCTAssertEqual(realm.objects(ReminderList.self).count, 3)

        let collectionViewsQuery = XCUIApplication().collectionViews
        collectionViewsQuery.children(matching: .other).element(boundBy: 1).tap()
        collectionViewsQuery.children(matching: .cell).element(boundBy: 1).children(matching: .other).element(boundBy: 1).children(matching: .other).element.swipeLeft()
        collectionViewsQuery.buttons["Delete"].tap()

        XCTAssertEqual(realm.objects(ReminderList.self).count, 2)

        collectionViewsQuery.children(matching: .other).element(boundBy: 2).tap()
        collectionViewsQuery.children(matching: .cell).element(boundBy: 2).children(matching: .other).element(boundBy: 1).children(matching: .other).element.swipeLeft()
        collectionViewsQuery.buttons["Delete"].tap()

        collectionViewsQuery.children(matching: .other).element(boundBy: 1).tap()
        collectionViewsQuery.children(matching: .cell).element(boundBy: 1).children(matching: .other).element(boundBy: 1).children(matching: .other).element.swipeLeft()
        collectionViewsQuery.buttons["Delete"].tap()
        XCTAssertEqual(realm.objects(ReminderList.self).count, 0)
    }

    @MainActor
    func testKeyPathObservedSectionedResults2() {
        // Tests ObservedSectionedResults ctor that uses the `sectionBlock` param.
        app.launchEnvironment["test_type"] = "observed_sectioned_results_sort_descriptors"
        app.launch()

        let addButton = app.buttons["addList"]
        addButton.tap()
        addButton.tap()

        // Populate reminders to reminder list.
        try! realm.write {
            for obj in realm.objects(ReminderList.self) {
                obj.reminders.append(Reminder())
            }
        }
        // Change the name of two ReminderList objects.
        // This is a separate write block because it's testing a change outside
        // the keypath input.

Review comment (suggestion):
```suggestion
XCTAssertEqual(realm.objects(ReminderList.self).count, 1)
```

Code after:
            XCTAssert(cell1.staticTexts["changed"].exists)
            XCTAssert(cell2.staticTexts["changed"].exists)
        } else {
            XCTAssert(app.tables.otherElements.staticTexts["A"].exists)
            XCTAssert(app.tables.otherElements.staticTexts["c"].exists)
            let cell0 = app.tables.firstMatch.cells.element(boundBy: 0)
            XCTAssert(cell0.staticTexts["Another List"].exists)
            let cell2 = app.tables.cells.element(boundBy: 1)
            let cell3 = app.tables.cells.element(boundBy: 2)
            XCTAssert(cell2.staticTexts["changed"].exists)
            XCTAssert(cell3.staticTexts["changed"].exists)
            XCTAssertEqual(app.tables.firstMatch.cells.count, 3)
        }
        XCTAssertEqual(realm.objects(ReminderList.self).count, 3)

        let collectionViewsQuery = XCUIApplication().collectionViews
        collectionViewsQuery.children(matching: .other).element(boundBy: 1).tap()
        collectionViewsQuery.children(matching: .cell).element(boundBy: 1).children(matching: .other).element(boundBy: 1).children(matching: .other).element.swipeLeft()
        collectionViewsQuery.buttons["Delete"].tap()
        XCTAssertEqual(realm.objects(ReminderList.self).count, 2)

        collectionViewsQuery.children(matching: .other).element(boundBy: 2).tap()
        collectionViewsQuery.children(matching: .cell).element(boundBy: 2).children(matching: .other).element(boundBy: 1).children(matching: .other).element.swipeLeft()
        collectionViewsQuery.buttons["Delete"].tap()
        XCTAssertEqual(realm.objects(ReminderList.self).count, 1)

        collectionViewsQuery.children(matching: .other).element(boundBy: 1).tap()
        collectionViewsQuery.children(matching: .cell).element(boundBy: 1).children(matching: .other).element(boundBy: 1).children(matching: .other).element.swipeLeft()
        collectionViewsQuery.buttons["Delete"].tap()
        XCTAssertEqual(realm.objects(ReminderList.self).count, 0)
    }

    @MainActor
    func testKeyPathObservedSectionedResults2() {
        // Tests ObservedSectionedResults ctor that uses the `sectionBlock` param.
        app.launchEnvironment["test_type"] = "observed_sectioned_results_sort_descriptors"
        app.launch()

        let addButton = app.buttons["addList"]
        addButton.tap()
        addButton.tap()

        // Populate reminders to reminder list.
        try! realm.write {
            for obj in realm.objects(ReminderList.self) {
                obj.reminders.append(Reminder())
            }
        }
        // Change the name of two ReminderList objects.
        // This is a separate write block because it's testing a change outside
        // the keypath input.