|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | +import UIKit |
| 4 | +import WordPressKit |
| 5 | +@testable import WordPress |
| 6 | + |
| 7 | +/// Covers the Months and Years and Avg. Views Per Day detail tables, where month rows |
| 8 | +/// from different years can show the same month name and the same count. |
| 9 | +/// |
| 10 | +/// The suite is serialized because expanded years live in the static |
| 11 | +/// `StatsDataHelper.expandedRowLabelsDetails`. |
| 12 | +@MainActor |
| 13 | +@Suite(.serialized) |
| 14 | +struct SiteStatsDetailsPostStatsTests { |
| 15 | + |
| 16 | + @Test(arguments: [StatSection.postStatsMonthsYears, .postStatsAverageViews]) |
| 17 | + func equalMonthsInTwoExpandedYearsAreSeparateRows(section: StatSection) throws { |
| 18 | + let table = PostStatsTable(section: section, april2025: 12) |
| 19 | + |
| 20 | + table.expand(["2026", "2025"]) |
| 21 | + |
| 22 | + #expect(monthCounts(in: try table.snapshot()) == ["12", "16", "12", "15"]) |
| 23 | + } |
| 24 | + |
| 25 | + @Test func collapsingAndReopeningAYearKeepsBothMonths() throws { |
| 26 | + let table = PostStatsTable(section: .postStatsMonthsYears, april2025: 12) |
| 27 | + |
| 28 | + table.expand(["2026", "2025"]) |
| 29 | + #expect(monthCounts(in: try table.snapshot()) == ["12", "16", "12", "15"]) |
| 30 | + |
| 31 | + table.expand(["2026"]) |
| 32 | + #expect(monthCounts(in: try table.snapshot()) == ["12", "16"]) |
| 33 | + |
| 34 | + table.expand(["2026", "2025"]) |
| 35 | + #expect(monthCounts(in: try table.snapshot()) == ["12", "16", "12", "15"]) |
| 36 | + } |
| 37 | + |
| 38 | + @Test func changedCountReplacesOnlyThatMonth() throws { |
| 39 | + let table = PostStatsTable(section: .postStatsMonthsYears, april2025: 12) |
| 40 | + table.expand(["2026", "2025"]) |
| 41 | + let beforeMonths = monthItems(in: try table.snapshot()) |
| 42 | + |
| 43 | + table.setPostDetails(april2025: 13) |
| 44 | + let after = try table.snapshot() |
| 45 | + |
| 46 | + #expect(monthCounts(in: after) == ["12", "16", "13", "15"]) |
| 47 | + // The diffable data source replaces an item only when it no longer finds an equal one. |
| 48 | + #expect(beforeMonths.map { after.indexOfItem($0) != nil } == [true, true, false, true]) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// MARK: - Helpers |
| 53 | + |
| 54 | +private extension SiteStatsDetailsPostStatsTests { |
| 55 | + func monthItems(in snapshot: ImmuTableDiffableDataSourceSnapshot) -> [AnyHashableImmuTableRow] { |
| 56 | + snapshot.itemIdentifiers.filter { $0.immuTableRow is DetailExpandableChildRow } |
| 57 | + } |
| 58 | + |
| 59 | + func monthCounts(in snapshot: ImmuTableDiffableDataSourceSnapshot) -> [String] { |
| 60 | + monthItems(in: snapshot).compactMap { ($0.immuTableRow as? DetailExpandableChildRow)?.rowData.data } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// A post stats detail table backed by a real `StatsPeriodStore`. |
| 65 | +/// |
| 66 | +/// Each test creates its own instance and releases it on the main actor: the store's |
| 67 | +/// `Dispatcher` asserts when its receipts are released on another thread. |
| 68 | +@MainActor |
| 69 | +private final class PostStatsTable { |
| 70 | + private let postID = 1046 |
| 71 | + private let section: StatSection |
| 72 | + private let periodStore = StatsPeriodStore() |
| 73 | + private let viewModel: SiteStatsDetailsViewModel |
| 74 | + // Strong references: the view model keeps its delegates weak. |
| 75 | + private let detailsHandler = MockDetailsDelegate() |
| 76 | + private let referrerHandler = MockReferrerDelegate() |
| 77 | + private let savedExpandedRows = StatsDataHelper.expandedRowLabelsDetails |
| 78 | + |
| 79 | + init(section: StatSection, april2025: Int) { |
| 80 | + self.section = section |
| 81 | + viewModel = SiteStatsDetailsViewModel( |
| 82 | + detailsDelegate: detailsHandler, |
| 83 | + referrerDelegate: referrerHandler, |
| 84 | + insightsStore: StatsInsightsStore(), |
| 85 | + periodStore: periodStore |
| 86 | + ) |
| 87 | + viewModel.fetchDataFor(statSection: section, postID: postID) |
| 88 | + // Set the data after `fetchDataFor`, which can mark the post stats as loading. |
| 89 | + setPostDetails(april2025: april2025) |
| 90 | + } |
| 91 | + |
| 92 | + deinit { |
| 93 | + StatsDataHelper.expandedRowLabelsDetails = savedExpandedRows |
| 94 | + } |
| 95 | + |
| 96 | + func expand(_ years: [String]) { |
| 97 | + StatsDataHelper.expandedRowLabelsDetails[section] = years |
| 98 | + } |
| 99 | + |
| 100 | + /// Seven years with positive totals. 2026 and 2025 both have April, with April 2025 |
| 101 | + /// set by the caller. Averages use the same numbers. |
| 102 | + func setPostDetails(april2025: Int) { |
| 103 | + var years: [String: [String: Int]] = [ |
| 104 | + "2026": ["3": 16, "4": 12], |
| 105 | + "2025": ["3": 15, "4": april2025] |
| 106 | + ] |
| 107 | + for (offset, year) in (2020...2024).enumerated() { |
| 108 | + years[String(year)] = ["3": 10 + offset, "4": 6 + offset] |
| 109 | + } |
| 110 | + let months = years.mapValues { ["months": $0, "total": $0.values.reduce(0, +)] as [String: Any] } |
| 111 | + let json: [String: AnyObject] = [ |
| 112 | + "date": "2026-11-18" as AnyObject, |
| 113 | + "views": 0 as AnyObject, |
| 114 | + "years": months as AnyObject, |
| 115 | + "averages": months as AnyObject, |
| 116 | + "weeks": [Any]() as AnyObject, |
| 117 | + "data": [Any]() as AnyObject |
| 118 | + ] |
| 119 | + periodStore.state.postStats[postID] = StatsPostDetails(jsonDictionary: json) |
| 120 | + periodStore.state.postStatsFetchingStatuses[postID] = .success |
| 121 | + } |
| 122 | + |
| 123 | + /// Returns the view model's real snapshot after checking that the expanded month rows differ. |
| 124 | + /// |
| 125 | + /// UIKit raises `NSInternalInconsistencyException` for duplicate snapshot items, which ends |
| 126 | + /// the test process. The view model's row builders are private, so the check builds the |
| 127 | + /// month rows with the same `StatsDataHelper` methods and fails the test instead. |
| 128 | + func snapshot() throws -> ImmuTableDiffableDataSourceSnapshot { |
| 129 | + let details = try #require(periodStore.getPostStats(for: postID)) |
| 130 | + let yearsData = section == .postStatsAverageViews ? details.dailyAveragesPerMonth : details.monthlyBreakdown |
| 131 | + let rows = (StatsDataHelper.expandedRowLabelsDetails[section] ?? []).compactMap(Int.init) |
| 132 | + .flatMap { |
| 133 | + StatsDataHelper.childRowsForYear(StatsDataHelper.monthsFrom(yearsData: yearsData, forYear: $0)) |
| 134 | + } |
| 135 | + for (index, row) in rows.enumerated() { |
| 136 | + try #require(!rows[..<index].contains(row), "Two month rows are equal, so the snapshot would reject them") |
| 137 | + } |
| 138 | + return viewModel.tableViewSnapshot() |
| 139 | + } |
| 140 | + |
| 141 | + private final class MockDetailsDelegate: SiteStatsDetailsDelegate {} |
| 142 | + |
| 143 | + private final class MockReferrerDelegate: SiteStatsReferrerDelegate { |
| 144 | + func showReferrerDetails(_ data: StatsTotalRowData) {} |
| 145 | + } |
| 146 | +} |
0 commit comments