11import XCTest
22import enum Networking. DotcomError
33import enum Yosemite. StatsActionV4
4+ import enum Yosemite. ProductAction
5+ import enum Yosemite. JustInTimeMessageAction
6+ import struct Yosemite. YosemiteJustInTimeMessage
47@testable import WooCommerce
58
69final class DashboardViewModelTests : XCTestCase {
10+ private let sampleSiteID : Int64 = 122
11+
712 func test_default_statsVersion_is_v4( ) {
813 // Given
914 let viewModel = DashboardViewModel ( )
@@ -24,7 +29,7 @@ final class DashboardViewModelTests: XCTestCase {
2429 XCTAssertEqual ( viewModel. statsVersion, . v4)
2530
2631 // When
27- viewModel. syncStats ( for: 122 , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
32+ viewModel. syncStats ( for: sampleSiteID , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
2833
2934 // Then
3035 XCTAssertEqual ( viewModel. statsVersion, . v3)
@@ -46,9 +51,9 @@ final class DashboardViewModelTests: XCTestCase {
4651 XCTAssertEqual ( viewModel. statsVersion, . v4)
4752
4853 // When
49- viewModel. syncStats ( for: 122 , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
50- viewModel. syncSiteVisitStats ( for: 122 , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) )
51- viewModel. syncTopEarnersStats ( for: 122 , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
54+ viewModel. syncStats ( for: sampleSiteID , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
55+ viewModel. syncSiteVisitStats ( for: sampleSiteID , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) )
56+ viewModel. syncTopEarnersStats ( for: sampleSiteID , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
5257
5358 // Then
5459 XCTAssertEqual ( viewModel. statsVersion, . v4)
@@ -65,14 +70,118 @@ final class DashboardViewModelTests: XCTestCase {
6570 }
6671 }
6772 let viewModel = DashboardViewModel ( stores: stores)
68- viewModel. syncStats ( for: 122 , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
73+ viewModel. syncStats ( for: sampleSiteID , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
6974 XCTAssertEqual ( viewModel. statsVersion, . v3)
7075
7176 // When
7277 storeStatsResult = . success( ( ) )
73- viewModel. syncStats ( for: 122 , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
78+ viewModel. syncStats ( for: sampleSiteID , siteTimezone: . current, timeRange: . thisMonth, latestDateToInclude: . init( ) , forceRefresh: false )
7479
7580 // Then
7681 XCTAssertEqual ( viewModel. statsVersion, . v4)
7782 }
83+
84+ func test_products_onboarding_announcements_take_precedence( ) {
85+ // Given
86+ let stores = MockStoresManager ( sessionManager: . makeForTesting( ) )
87+ stores. whenReceivingAction ( ofType: ProductAction . self) { action in
88+ switch action {
89+ case let . checkProductsOnboardingEligibility( _, completion) :
90+ completion ( . success( true ) )
91+ default :
92+ XCTFail ( " Received unsupported action: \( action) " )
93+ }
94+ }
95+ stores. whenReceivingAction ( ofType: JustInTimeMessageAction . self) { action in
96+ switch action {
97+ case let . loadMessage( _, _, _, completion) :
98+ completion ( . success( YosemiteJustInTimeMessage . fake ( ) ) )
99+ }
100+ }
101+ let viewModel = DashboardViewModel ( stores: stores)
102+
103+ // When
104+ viewModel. syncAnnouncements ( for: sampleSiteID)
105+
106+ // Then (check announcement image because it is unique and not localized)
107+ XCTAssertEqual ( viewModel. announcementViewModel? . image, . emptyProductsImage)
108+ }
109+
110+ func test_view_model_syncs_just_in_time_messages_when_ineligible_for_products_onboarding( ) {
111+ // Given
112+ let stores = MockStoresManager ( sessionManager: . makeForTesting( ) )
113+ stores. whenReceivingAction ( ofType: ProductAction . self) { action in
114+ switch action {
115+ case let . checkProductsOnboardingEligibility( _, completion) :
116+ completion ( . success( false ) )
117+ default :
118+ XCTFail ( " Received unsupported action: \( action) " )
119+ }
120+ }
121+ stores. whenReceivingAction ( ofType: JustInTimeMessageAction . self) { action in
122+ switch action {
123+ case let . loadMessage( _, _, _, completion) :
124+ completion ( . success( YosemiteJustInTimeMessage . fake ( ) . copy ( title: " JITM Message " ) ) )
125+ }
126+ }
127+ let viewModel = DashboardViewModel ( stores: stores)
128+
129+ // When
130+ viewModel. syncAnnouncements ( for: sampleSiteID)
131+
132+ // Then
133+ XCTAssertEqual ( viewModel. announcementViewModel? . title, " JITM Message " )
134+ }
135+
136+ func test_no_announcement_to_display_when_no_announcements_are_synced( ) {
137+ // Given
138+ let stores = MockStoresManager ( sessionManager: . makeForTesting( ) )
139+ stores. whenReceivingAction ( ofType: ProductAction . self) { action in
140+ switch action {
141+ case let . checkProductsOnboardingEligibility( _, completion) :
142+ completion ( . success( false ) )
143+ default :
144+ XCTFail ( " Received unsupported action: \( action) " )
145+ }
146+ }
147+ stores. whenReceivingAction ( ofType: JustInTimeMessageAction . self) { action in
148+ switch action {
149+ case let . loadMessage( _, _, _, completion) :
150+ completion ( . success( nil ) )
151+ }
152+ }
153+ let viewModel = DashboardViewModel ( stores: stores)
154+
155+ // When
156+ viewModel. syncAnnouncements ( for: sampleSiteID)
157+
158+ // Then
159+ XCTAssertNil ( viewModel. announcementViewModel)
160+ }
161+
162+ func test_no_announcement_synced_when_feature_flags_disabled( ) {
163+ // Given
164+ let stores = MockStoresManager ( sessionManager: . makeForTesting( ) )
165+ stores. whenReceivingAction ( ofType: ProductAction . self) { action in
166+ switch action {
167+ case let . checkProductsOnboardingEligibility( _, completion) :
168+ completion ( . success( true ) )
169+ default :
170+ XCTFail ( " Received unsupported action: \( action) " )
171+ }
172+ }
173+ stores. whenReceivingAction ( ofType: JustInTimeMessageAction . self) { action in
174+ switch action {
175+ case let . loadMessage( _, _, _, completion) :
176+ completion ( . success( YosemiteJustInTimeMessage . fake ( ) ) )
177+ }
178+ }
179+ let viewModel = DashboardViewModel ( stores: stores, featureFlags: MockFeatureFlagService ( ) )
180+
181+ // When
182+ viewModel. syncAnnouncements ( for: sampleSiteID)
183+
184+ // Then
185+ XCTAssertNil ( viewModel. announcementViewModel)
186+ }
78187}
0 commit comments