Files
iOS/Tests/App/EntityPicker/EntityPickerViewModel.test.swift
Bruno Pantaleão Gonçalves 3613bb8721 CarPlay iOS 26 UI Improvements (#4465)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->
<img width="800" height="480" alt="Simulator Screenshot - Daily tester 2
- 2026-03-31 at 16 45 28"
src="https://github.com/user-attachments/assets/927764c8-9de7-49e9-98c4-8cc8fd0bcf6b"
/>

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2026-04-01 09:39:44 +02:00

76 lines
2.2 KiB
Swift

import Foundation
@testable import HomeAssistant
@testable import Shared
import Testing
// MARK: - Fakes & Helpers
private struct FakeEntity: Equatable {
let entityId: String
let name: String
let domain: String
let serverId: String
}
private extension HAAppEntity {
static func make(_ id: String, name: String, domain: String, serverId: String) -> HAAppEntity {
HAAppEntity(
id: id,
entityId: id,
serverId: serverId,
domain: domain,
name: name,
icon: nil,
rawDeviceClass: ""
)
}
}
private extension AppArea {
static func make(id: String, name: String, entities: [String]) -> AppArea {
AppArea(
id: id,
serverId: "A",
areaId: id,
name: name,
aliases: [],
picture: nil,
icon: nil,
sortOrder: nil,
entities: Set(entities)
)
}
}
@Suite("EntityPickerViewModel")
struct EntityPickerViewModelTests {
private func makeVM(
domainFilter: Domain? = nil,
selectedServerId: String? = nil,
entities: [HAAppEntity],
areas: [AppArea] = []
) -> EntityPickerViewModel {
let vm = EntityPickerViewModel(domainFilter: domainFilter, selectedServerId: selectedServerId)
vm.entities = entities
vm.areaData = areas
// Assume fetchEntities updates caches; if it reaches out externally, comment out and call cache builders
vm.fetchEntities()
return vm
}
@Test("Groups by domain from all entities when no server selected")
func groupsByDomain() async throws {
let entities: [HAAppEntity] = [
.make("light.kitchen", name: "Kitchen Light", domain: "light", serverId: "A"),
.make("switch.pump", name: "Pump", domain: "switch", serverId: "A"),
.make("light.bedroom", name: "Bedroom Light", domain: "light", serverId: "B"),
]
let vm = EntityPickerViewModel(domainFilter: nil, selectedServerId: nil)
vm.entities = entities
vm._test_groupByDomain()
#expect(vm.entitiesByDomain["light"]?.count == 2)
#expect(vm.entitiesByDomain["switch"]?.count == 1)
}
}