@@ -266,6 +266,105 @@ final class ItemListViewModelTests: XCTestCase {
266266 // Then
267267 XCTAssertEqual ( sut. state. isLoaded, false )
268268 }
269+
270+ func test_populatePointOfSaleItems_when_no_items_are_loaded_then_itemsPublisher_emits_no_items( ) async throws {
271+ let itemProvider = MockPOSItemProvider ( )
272+ itemProvider. shouldReturnZeroItems = true
273+ let sut = ItemListViewModel ( itemProvider: itemProvider)
274+
275+ let expectation = XCTestExpectation ( description: " Publisher should emit nothing " )
276+ var receivedItems : [ POSItem ] = [ ]
277+ sut. itemsPublisher. sink { items in
278+ receivedItems = items
279+ expectation. fulfill ( )
280+ }
281+ . store ( in: & cancellables)
282+
283+ // When
284+ await sut. populatePointOfSaleItems ( )
285+
286+ // Then
287+ XCTAssertTrue ( sut. state == . empty)
288+ XCTAssertTrue ( receivedItems. isEmpty)
289+ }
290+
291+ func test_populatePointOfSaleItems_when_items_are_loaded_then_itemsPublisher_emits_items( ) async throws {
292+ // Given
293+ let items = Self . makeItems ( )
294+ let expectation = XCTestExpectation ( description: " Publisher should emit populated items " )
295+ var receivedItems : [ POSItem ] = [ ]
296+ sut. itemsPublisher. sink { items in
297+ receivedItems = items
298+ expectation. fulfill ( )
299+ }
300+ . store ( in: & cancellables)
301+
302+ // When
303+ await sut. populatePointOfSaleItems ( )
304+ guard let firstItem = items. first, let lastItem = items. last else {
305+ return XCTFail ( " Expected two items, got \( receivedItems) . " )
306+ }
307+
308+ // Then
309+ XCTAssertTrue ( sut. state == . loaded( receivedItems) )
310+ XCTAssertEqual ( receivedItems. first? . productID, firstItem. productID)
311+ XCTAssertEqual ( receivedItems. last? . productID, lastItem. productID)
312+ }
313+
314+ func test_populatePointOfSaleItems_when_no_items_are_loaded_then_statePublisher_emits_expected_empty_state( ) async throws {
315+ // Given
316+ XCTAssertEqual ( sut. state, . loading, " Initial state " )
317+
318+ let itemProvider = MockPOSItemProvider ( )
319+ itemProvider. shouldReturnZeroItems = true
320+ let sut = ItemListViewModel ( itemProvider: itemProvider)
321+ let expectation = XCTestExpectation ( description: " Publisher should emit state changes " )
322+
323+ var receivedStates : [ ItemListViewModel . ItemListState ] = [ ]
324+ sut. statePublisher
325+ . removeDuplicates ( )
326+ . sink { state in
327+ receivedStates. append ( state)
328+ expectation. fulfill ( )
329+ }
330+ . store ( in: & cancellables)
331+
332+ // When
333+ await sut. populatePointOfSaleItems ( )
334+
335+ // Then
336+ XCTAssertEqual ( receivedStates, [ . loading, . empty] )
337+ }
338+
339+ func test_populatePointOfSaleItems_when_items_are_loaded_then_statePublisher_emits_expected_loaded_state( ) async throws {
340+ // Given
341+ XCTAssertEqual ( sut. state, . loading, " Initial state " )
342+ let expectation = XCTestExpectation ( description: " Publisher should emit state changes " )
343+ let items = Self . makeItems ( )
344+
345+ var receivedStates : [ ItemListViewModel . ItemListState ] = [ ]
346+ sut. statePublisher
347+ . removeDuplicates ( )
348+ . sink { state in
349+ receivedStates. append ( state)
350+ expectation. fulfill ( )
351+ }
352+ . store ( in: & cancellables)
353+
354+ // When
355+ await sut. populatePointOfSaleItems ( )
356+
357+ // Then
358+ XCTAssertEqual ( receivedStates, [ . loading, . loaded( items) ] )
359+ }
360+
361+ func test_simpleProductsInfoButtonTapped_when_tapped_then_showSimpleProductsModal_toggled( ) {
362+ XCTAssertFalse ( sut. showSimpleProductsModal)
363+
364+ sut. simpleProductsInfoButtonTapped ( )
365+
366+ XCTAssertTrue ( sut. showSimpleProductsModal)
367+ }
269368}
270369
271370private extension ItemListViewModelTests {
0 commit comments