Skip to content

Commit

Permalink
PIA-1001: Add search unit tests on the regions list
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-laura-sempere committed Jan 25, 2024
1 parent 41e8f1e commit e2c6358
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 22 deletions.
1 change: 0 additions & 1 deletion PIA VPN-tvOS/Navigation/Destinations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ enum RegionSelectionDestinations: Destinations {

enum RegionsDestinations: Destinations {
case serversList
// TODO: Consider removing this Destination (it seems it is not needed)
case selectServer(_: ServerType)

static func == (lhs: RegionsDestinations, rhs: RegionsDestinations) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ class RegionsContainerViewModel: ObservableObject {

@Published private(set) var selectedSideMenuItem: RegionSelectionSideMenuItems = .all

@Published var search = ""
@Published var searchIsActive = false

private let onSearchSelectedAction: AppRouter.Actions

init(onSearchSelectedAction: AppRouter.Actions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class RegionsListViewModel: ObservableObject {
return
}
self.servers = self.useCase.getCurrentServers().filter({ server in
return server.name.lowercased().contains(searchTerm.lowercased())
return server.name.lowercased().contains(searchTerm.lowercased()) ||
searchTerm.lowercased().contains(server.country.lowercased())
})
}.store(in: &cancellables)
}
Expand Down
20 changes: 4 additions & 16 deletions PIA VPN-tvOS/RegionsSelection/UI/RegionsContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,27 @@ struct RegionsContainerView: View {
}
}
}

var sideMenuNavLinks: some View {
VStack {
ForEach(viewModel.sideMenuItems) { item in
NavigationLink(value: item) {
Text(item.text)
}
}
}
}


var body: some View {

VStack(alignment: .leading) {
// TODO: Render either all or favorites
switch viewModel.selectedSideMenuItem {
case .all:
HStack(alignment: .top) {
sideMenuButtons
VStack {
RegionsSelectionFactory.makeRegionsListView()
}.navigationTitle("All")
}
}
case .search:
EmptyView()
case .favourites:
HStack(alignment: .top) {
sideMenuButtons
VStack {
RegionsSelectionFactory.makeRegionsListView()
}.navigationTitle("FAV")
}
}
case .search:
EmptyView()
}

}.navigationDestination(for: RegionSelectionDestinations.self) { route in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class InstallVpnConfigurationProvider: InstallVPNConfigurationUseCaseType {
func callAsFunction() async throws {
return try await withCheckedThrowingContinuation { continuation in
if isSimulator {
NSLog(">>> >>> Simulator")
continuation.resume()
vpnConfigurationAvailability.set(value: true)
} else {
Expand Down
23 changes: 23 additions & 0 deletions PIA VPN-tvOSTests/RegionsList/RegionsListViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ class RegionsListViewModelTests: XCTestCase {
class Fixture {
let regionsListUseCaseMock = RegionsListUseCaseMock()
let appRouterSpy = AppRouterSpy()

var allServers: [ServerMock] = [
ServerMock(name: "Toronto", identifier: "ca-server", regionIdentifier: "ca-region", country: "CA", geo: false),
ServerMock(name: "Montreal", identifier: "ca-server2", regionIdentifier: "ca-region2", country: "CA", geo: false),
ServerMock(name: "Barcelona-1", identifier: "es-server", regionIdentifier: "es-region", country: "ES", geo: false),
ServerMock(name: "Madrid", identifier: "es-server2", regionIdentifier: "es-region2", country: "ES", geo: false)

]
}

var fixture: Fixture!
Expand Down Expand Up @@ -61,4 +69,19 @@ class RegionsListViewModelTests: XCTestCase {

}

func test_regionsDidSearch() {
// GIVEN THAT we have 4 servers available (2 in CA and 2 in ES)
fixture.regionsListUseCaseMock.getCurrentServersResult = fixture.allServers
instantiateSut(with: .pop(router: fixture.appRouterSpy))
XCTAssertEqual(sut.servers.count, 4)

// WHEN we search for 'Canada'
sut.search = "Canada"

// THEN the displayed servers are only 2 (The ones in 'CA')
XCTAssertEqual(sut.servers.count, 2)
XCTAssertEqual(sut.servers.first!.country, "CA")
XCTAssertEqual(sut.servers.last!.country, "CA")
}

}

0 comments on commit e2c6358

Please sign in to comment.