|
| 1 | +import Testing |
1 | 2 | import Foundation |
2 | | -import XCTest |
3 | 3 | import WordPressKit |
4 | | -import WordPressUI |
5 | 4 |
|
6 | 5 | @testable import WordPress |
7 | | -@testable import WordPressData |
8 | 6 |
|
9 | | -class TimeZoneSelectorViewModelTests: CoreDataTestCase { |
| 7 | +@MainActor |
| 8 | +struct TimeZoneSelectorViewModelTests { |
| 9 | + @Test func initAndCheckInitialState() async { |
| 10 | + let service = MockTimeZoneService() |
| 11 | + let viewModel = TimeZoneSelectorViewModel(service: service) |
10 | 12 |
|
11 | | - private var viewModel: TimeZoneSelectorViewModel! |
12 | | - |
13 | | - private var timeZoneGroups: [TimeZoneGroup]! |
| 13 | + #expect(viewModel.sections.isEmpty) |
| 14 | + #expect(!viewModel.isLoading) |
| 15 | + #expect(viewModel.error == nil) |
| 16 | + } |
14 | 17 |
|
15 | | - override func setUp() { |
16 | | - super.setUp() |
| 18 | + @Test func filteredSectionsWithEmptySearchText() async { |
| 19 | + let mockGroups = createMockTimeZoneGroups() |
| 20 | + let service = MockTimeZoneService(timeZoneGroups: mockGroups) |
| 21 | + let viewModel = TimeZoneSelectorViewModel(service: service) |
17 | 22 |
|
18 | | - // Given TimeZoneGroups |
19 | | - // When new ViewModel created with TimeZoneStore with state=loaded |
20 | | - loadTimeZoneGroupsIntoViewModel() |
21 | | - } |
| 23 | + await viewModel.loadTimezones() |
22 | 24 |
|
23 | | - override func tearDown() { |
24 | | - viewModel = nil |
25 | | - timeZoneGroups = nil |
| 25 | + let filtered = viewModel.filteredSections(searchText: "") |
26 | 26 |
|
27 | | - super.tearDown() |
| 27 | + #expect(filtered.count == viewModel.sections.count) |
| 28 | + #expect(filtered == viewModel.sections) |
28 | 29 | } |
29 | 30 |
|
30 | | - func loadTimeZoneGroupsIntoViewModel(selectedValue: String = "", filter: String? = nil) { |
31 | | - timeZoneGroups = [timeZoneGroup()] |
| 31 | + @Test func filteredSectionsWithMatchingSearchText() async { |
| 32 | + let mockGroups = createMockTimeZoneGroups() |
| 33 | + let service = MockTimeZoneService(timeZoneGroups: mockGroups) |
| 34 | + let viewModel = TimeZoneSelectorViewModel(service: service) |
32 | 35 |
|
33 | | - let loaded = TimeZoneStoreState.loaded(timeZoneGroups) |
34 | | - viewModel = TimeZoneSelectorViewModel( |
35 | | - state: TimeZoneSelectorViewModel.State.with(storeState: loaded), |
36 | | - selectedValue: selectedValue, |
37 | | - filter: filter) |
38 | | - } |
| 36 | + await viewModel.loadTimezones() |
39 | 37 |
|
40 | | - func testReady() throws { |
41 | | - switch viewModel.state { |
42 | | - case .loading: |
43 | | - XCTFail() |
44 | | - case .ready(let groups): |
45 | | - // Then viewModel should be ready |
46 | | - XCTAssertEqual(groups.count, timeZoneGroups.count) |
47 | | - case .error: |
48 | | - XCTFail() |
49 | | - } |
50 | | - } |
| 38 | + let filtered = viewModel.filteredSections(searchText: "Addis") |
51 | 39 |
|
52 | | - func testGroups() { |
53 | | - // Then viewModel allTimeZonesGroup() count is equal to mock data count |
54 | | - XCTAssertEqual(viewModel.groups.count, timeZoneGroups.count) |
| 40 | + #expect(filtered.count == 1) |
| 41 | + #expect(filtered[0].name == "Africa") |
| 42 | + #expect(filtered[0].timezones.count == 1) |
| 43 | + #expect(filtered[0].timezones[0].timezone.label == "Addis Ababa") |
55 | 44 | } |
56 | 45 |
|
57 | | - func testFilteredGroupsExists() { |
58 | | - // When user types "Addis" which exists |
59 | | - loadTimeZoneGroupsIntoViewModel(filter: "Addis") |
| 46 | + @Test func filteredSectionsWithNonMatchingSearchText() async { |
| 47 | + let mockGroups = createMockTimeZoneGroups() |
| 48 | + let service = MockTimeZoneService(timeZoneGroups: mockGroups) |
| 49 | + let viewModel = TimeZoneSelectorViewModel(service: service) |
60 | 50 |
|
61 | | - // Then viewModel filteredGroups should be Addis_Ababa |
62 | | - let filteredGroups = viewModel.filteredGroups |
63 | | - XCTAssertEqual(filteredGroups.count, 1) |
| 51 | + await viewModel.loadTimezones() |
64 | 52 |
|
65 | | - let timeZoneGroup: TimeZoneGroup = filteredGroups[0] |
66 | | - XCTAssertEqual(timeZoneGroup.timezones.count, 1) |
67 | | - XCTAssertEqual(timeZoneGroup.name, "Africa") |
| 53 | + let filtered = viewModel.filteredSections(searchText: "NoTimeZoneForThisFilter") |
68 | 54 |
|
69 | | - let timeZone: WPTimeZone = timeZoneGroup.timezones[0] |
70 | | - XCTAssertEqual(timeZone.label, Constants.timeZoneTestTuple3.label) |
71 | | - XCTAssertEqual(timeZone.value, Constants.timeZoneTestTuple3.value) |
| 55 | + #expect(filtered.isEmpty) |
72 | 56 | } |
73 | 57 |
|
74 | | - func testFilteredGroupsDoesNotExist() { |
75 | | - // When user types an invalid filter |
76 | | - loadTimeZoneGroupsIntoViewModel(filter: "NoTimeZoneForThisFilter") |
| 58 | + @Test func loadTimezonesSuccess() async { |
| 59 | + // Create mock data |
| 60 | + let mockGroups = [ |
| 61 | + TimeZoneGroup(name: "Africa", timezones: [ |
| 62 | + NamedTimeZone(label: "Abidjan", value: "Africa/Abidjan"), |
| 63 | + NamedTimeZone(label: "Accra", value: "Africa/Accra") |
| 64 | + ]), |
| 65 | + TimeZoneGroup(name: "America", timezones: [ |
| 66 | + NamedTimeZone(label: "New York", value: "America/New_York") |
| 67 | + ]) |
| 68 | + ] |
77 | 69 |
|
78 | | - // Then viewModel filteredGroups will be empty |
79 | | - let filteredGroups = viewModel.filteredGroups |
80 | | - XCTAssertEqual(filteredGroups.count, 0) |
81 | | - } |
| 70 | + let service = MockTimeZoneService(timeZoneGroups: mockGroups) |
| 71 | + let viewModel = TimeZoneSelectorViewModel(service: service) |
82 | 72 |
|
83 | | - func testGetTimeZoneForIdentifier() { |
84 | | - // When TimeZoneIdentifier = "Africa/Addis_Ababa" |
85 | | - // Then "Africa/Addis_Ababa" WPTimeZone returned |
86 | | - guard let timeZone = viewModel.getTimeZoneForIdentifier(Constants.timeZoneTestTuple3.value) else { |
87 | | - XCTFail() |
88 | | - return |
89 | | - } |
| 73 | + #expect(viewModel.sections.isEmpty) |
| 74 | + #expect(!viewModel.isLoading) |
90 | 75 |
|
91 | | - XCTAssertNotNil(timeZone) |
92 | | - XCTAssertEqual(timeZone.label, Constants.timeZoneTestTuple3.label) |
93 | | - XCTAssertEqual(timeZone.value, Constants.timeZoneTestTuple3.value) |
94 | | - } |
| 76 | + await viewModel.loadTimezones() |
95 | 77 |
|
96 | | - func testTableViewModel() { |
97 | | - // When viewModel has no selected value |
98 | | - let immuTable: ImmuTable = viewModel.tableViewModel(selectionHandler: { (selectedTimezone) in |
99 | | - }) |
100 | | - |
101 | | - // Then section count = 1 |
102 | | - let sections = immuTable.sections |
103 | | - XCTAssertNotNil(sections) |
104 | | - XCTAssertEqual(sections.count, 1) |
105 | | - |
106 | | - // Then rows count = 3 |
107 | | - let section: ImmuTableSection = sections[0] |
108 | | - let rows = section.rows |
109 | | - XCTAssertNotNil(rows) |
110 | | - XCTAssertEqual(rows.count, 3) |
| 78 | + #expect(!viewModel.isLoading) |
| 79 | + #expect(viewModel.error == nil) |
| 80 | + #expect(viewModel.sections.count == 2) |
| 81 | + #expect(viewModel.sections[0].name == "Africa") |
| 82 | + #expect(viewModel.sections[0].timezones.count == 2) |
| 83 | + #expect(viewModel.sections[1].name == "America") |
| 84 | + #expect(viewModel.sections[1].timezones.count == 1) |
111 | 85 | } |
112 | 86 |
|
113 | | - func testTableViewModelSelectedValue() { |
114 | | - // When selectedValue = "Africa/Addis_Ababa" |
115 | | - loadTimeZoneGroupsIntoViewModel(selectedValue: Constants.timeZoneTestTuple3.value) |
| 87 | + @Test func loadTimezonesError() async { |
| 88 | + let service = MockTimeZoneService(shouldThrowError: true) |
| 89 | + let viewModel = TimeZoneSelectorViewModel(service: service) |
116 | 90 |
|
117 | | - // Then selectedValue should be Addis_Ababa |
118 | | - XCTAssertEqual(viewModel.selectedValue, Constants.timeZoneTestTuple3.value) |
119 | | - } |
| 91 | + #expect(viewModel.error == nil) |
| 92 | + #expect(!viewModel.isLoading) |
120 | 93 |
|
121 | | - func testNoResultsViewModelLoading() { |
122 | | - // Given viewModel |
123 | | - // When loading |
124 | | - viewModel = TimeZoneSelectorViewModel( |
125 | | - state: TimeZoneSelectorViewModel.State.with(storeState: TimeZoneStoreState.loading), |
126 | | - selectedValue: "", |
127 | | - filter: nil) |
128 | | - |
129 | | - // Then noResultsViewModel exists |
130 | | - guard let noResultsVCModel: NoResultsViewController.Model = viewModel.noResultsViewModel else { |
131 | | - XCTFail() |
132 | | - return |
133 | | - } |
134 | | - |
135 | | - // Then accessoryView exists |
136 | | - XCTAssertNotNil(noResultsVCModel.accessoryView) |
| 94 | + await viewModel.loadTimezones() |
137 | 95 |
|
138 | | - // Then noResultsViewModel title is loading |
139 | | - XCTAssertEqual(noResultsVCModel.titleText, TimeZoneSelectorViewModel.LocalizedText.loadingTitle) |
| 96 | + #expect(!viewModel.isLoading) |
| 97 | + #expect(viewModel.error != nil) |
| 98 | + #expect(viewModel.sections.isEmpty) |
140 | 99 | } |
141 | 100 |
|
142 | | - func testNoResultsViewModelReady() { |
143 | | - // Given ViewModel |
144 | | - // When ViewModel state is ready |
| 101 | + @Test func loadTimezonesUpdatesSuggestion() async { |
| 102 | + // Mock timezone groups containing device's current timezone |
| 103 | + let deviceTimezone = TimeZone.current.identifier |
| 104 | + let mockGroups = [ |
| 105 | + TimeZoneGroup(name: "Test", timezones: [ |
| 106 | + NamedTimeZone(label: "Test Zone", value: deviceTimezone) |
| 107 | + ]) |
| 108 | + ] |
145 | 109 |
|
146 | | - // Then noResultsViewModel is nil |
147 | | - XCTAssertNil(viewModel.noResultsViewModel) |
148 | | - } |
| 110 | + let service = MockTimeZoneService(timeZoneGroups: mockGroups) |
| 111 | + let viewModel = TimeZoneSelectorViewModel(service: service) |
149 | 112 |
|
150 | | - func testNoResultsViewModelError() { |
151 | | - // Given ViewModel |
152 | | - // When ViewModel state is error |
153 | | - viewModel = TimeZoneSelectorViewModel( |
154 | | - state: TimeZoneSelectorViewModel.State.with(storeState: TimeZoneStoreState.error(testError())), |
155 | | - selectedValue: "", |
156 | | - filter: nil) |
157 | | - |
158 | | - // Then noResultsViewModel exists |
159 | | - guard let noResultsVCModel: NoResultsViewController.Model = viewModel.noResultsViewModel else { |
160 | | - XCTFail() |
161 | | - return |
162 | | - } |
| 113 | + #expect(viewModel.suggestedTimezoneRowViewModel == nil) |
163 | 114 |
|
164 | | - // Then noResultsViewModel title is No Connection |
165 | | - XCTAssertEqual(noResultsVCModel.titleText, TimeZoneSelectorViewModel.LocalizedText.noConnectionTitle) |
166 | | - } |
| 115 | + await viewModel.loadTimezones() |
167 | 116 |
|
168 | | - func timeZoneGroup() -> TimeZoneGroup { |
169 | | - var zones = [WPTimeZone]() |
170 | | - zones.append(NamedTimeZone(label: Constants.timeZoneTestTuple1.label, value: Constants.timeZoneTestTuple1.value)) |
171 | | - zones.append(NamedTimeZone(label: Constants.timeZoneTestTuple2.label, value: Constants.timeZoneTestTuple2.value)) |
172 | | - zones.append(NamedTimeZone(label: Constants.timeZoneTestTuple3.label, value: Constants.timeZoneTestTuple3.value)) |
173 | | - return TimeZoneGroup(name: "Africa", timezones: zones) |
| 117 | + #expect(viewModel.suggestedTimezoneRowViewModel != nil) |
| 118 | + #expect(viewModel.suggestedTimezoneRowViewModel?.timezone.value.caseInsensitiveCompare(deviceTimezone) == .orderedSame) |
174 | 119 | } |
175 | 120 |
|
| 121 | + // MARK: - Helpers |
| 122 | + |
| 123 | + private func createMockTimeZoneGroups() -> [TimeZoneGroup] { |
| 124 | + [ |
| 125 | + TimeZoneGroup(name: "Africa", timezones: [ |
| 126 | + NamedTimeZone(label: "Abidjan", value: "Africa/Abidjan"), |
| 127 | + NamedTimeZone(label: "Accra", value: "Africa/Accra"), |
| 128 | + NamedTimeZone(label: "Addis Ababa", value: "Africa/Addis_Ababa") |
| 129 | + ]), |
| 130 | + TimeZoneGroup(name: "America", timezones: [ |
| 131 | + NamedTimeZone(label: "New York", value: "America/New_York"), |
| 132 | + NamedTimeZone(label: "Los Angeles", value: "America/Los_Angeles") |
| 133 | + ]) |
| 134 | + ] |
| 135 | + } |
176 | 136 | } |
177 | 137 |
|
178 | | -private extension TimeZoneSelectorViewModelTests { |
179 | | - enum DecodingError: Error { |
180 | | - case decodingFailed |
| 138 | +private struct MockTimeZoneService: TimeZoneServiceProtocol { |
| 139 | + var shouldThrowError = false |
| 140 | + var timeZoneGroups: [TimeZoneGroup] = [] |
| 141 | + |
| 142 | + func timezones() async throws -> [TimeZoneGroup] { |
| 143 | + if shouldThrowError { |
| 144 | + throw MockError.testError |
| 145 | + } |
| 146 | + return timeZoneGroups |
181 | 147 | } |
182 | 148 |
|
183 | | - enum Constants { |
184 | | - typealias timeZoneTestTuple = (label: String, value: String) |
185 | | - static let timeZoneTestTuple1: timeZoneTestTuple = (label: "Abidjan", value: "Africa/Abidjan") |
186 | | - static let timeZoneTestTuple2: timeZoneTestTuple = (label: "Accra", value: "Africa/Accra") |
187 | | - static let timeZoneTestTuple3: timeZoneTestTuple = (label: "Addis Ababa", value: "Africa/Addis_Ababa") |
| 149 | + enum MockError: Error { |
| 150 | + case testError |
188 | 151 | } |
189 | 152 | } |
0 commit comments