pEpForiOSTests/UI/Util/AccountPickerViewModel/AccountPickerViewModelTest.swift
author buff <andreas@pep-project.org>
Thu, 08 Aug 2019 14:36:37 +0200
changeset 9610 37b018b92b24
parent 9609 d518cb525a08
child 9661 790148e0f3c5
permissions -rw-r--r--
rm unused variable
     1 //
     2 //  AccountPickerViewModelTest.swift
     3 //  pEpForiOSTests
     4 //
     5 //  Created by Andreas Buff on 11.10.18.
     6 //  Copyright © 2018 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import XCTest
    10 
    11 @testable import pEpForiOS
    12 import MessageModel
    13 
    14 class AccountPickerViewModelTest: CoreDataDrivenTestBase {
    15 
    16     // MARK: - numAccounts
    17 
    18     func testNumAccounts_num0() {
    19         deleteAllAccounts()
    20         let expectedNumAccounts = 0
    21         assertPickerViewModel(numAccounts: expectedNumAccounts)
    22     }
    23 
    24     func testNumAccounts_num1() {
    25         let expectedNumAccounts = 1
    26         assertPickerViewModel(numAccounts: expectedNumAccounts)
    27     }
    28 
    29     func testNumAccounts_num2() {
    30         createAndSaveSecondAccount()
    31         let expectedNumAccounts = 2
    32         assertPickerViewModel(numAccounts: expectedNumAccounts)
    33     }
    34 
    35     // MARK: - account(at:)
    36 
    37     func testAccountAt_0() {
    38         let testIdx = 0
    39         assertPickerViewModel(accountAt: testIdx, expected: account)
    40     }
    41 
    42     func testAccountAt_1() {
    43         let _ = createAndSaveSecondAccount()
    44         let sndIdx = 1
    45 
    46         assertPickerViewModel(accountAt: sndIdx, expected: secondAccount())
    47     }
    48 
    49     func testAccountAt_not1() {
    50         createAndSaveSecondAccount()
    51         let testIdx = 1
    52 
    53         assertPickerViewModel(accountAt: testIdx,
    54                               expected: firstAccount(),
    55                               shouldFail: true)
    56     }
    57 
    58     // MARK: - handleUserSelected
    59 
    60     func testHandleUserSelected_oneAccount_correct() {
    61         let firstRowIdx = 0
    62         let pickedAccount = account
    63         assertUserSelection(selectIdx: firstRowIdx,
    64                             accountToCompare: pickedAccount,
    65                             mustEqualSelected: true)
    66     }
    67 
    68     func testHandleUserSelected_twoAccounts_correct() {
    69         let secondRowIdx = 1
    70         let _ = createAndSaveSecondAccount()
    71 
    72         assertUserSelection(selectIdx: secondRowIdx,
    73                             accountToCompare: secondAccount(),
    74                             mustEqualSelected: true)
    75     }
    76 
    77     func testHandleUserSelected_twoAccounts_shouldFail() {
    78         let firstAccountIdx = 0
    79         let _ = createAndSaveSecondAccount()
    80 
    81         assertUserSelection(selectIdx: firstAccountIdx,
    82                             accountToCompare: secondAccount(),
    83                             mustEqualSelected: false)
    84     }
    85 
    86     // MARK: - HELPER
    87 
    88     func assertUserSelection(selectIdx: Int, accountToCompare: Account, mustEqualSelected: Bool) {
    89         let expectDidSelectCalled = expectation(description: "expectDidSelectCalled")
    90         let testDelegate = TestResultDelegate(expectDidSelectCalled: expectDidSelectCalled,
    91                                               accountToCompare: accountToCompare,
    92                                               mustEqualSelected: mustEqualSelected)
    93         let testee = AccountPickerViewModel(resultDelegate: testDelegate)
    94         testee.handleUserSelected(row: selectIdx)
    95         waitForExpectations(timeout: UnitTestUtils.waitTime)
    96     }
    97 
    98     private func assertPickerViewModel(accountAt idx: Int,
    99                                        expected: Account,
   100                                        shouldFail: Bool = false) {
   101         let vm = AccountPickerViewModel(resultDelegate: nil)
   102         let testee = vm.account(at: idx)
   103         if shouldFail {
   104             XCTAssertNotEqual(testee, expected.user.address)
   105         } else {
   106             XCTAssertEqual(testee, expected.user.address)
   107         }
   108     }
   109 
   110     private func assertPickerViewModel(numAccounts: Int) {
   111         let testee = AccountPickerViewModel(resultDelegate: nil)
   112         let numAccountsInDB = Account.all().count
   113         XCTAssertEqual(testee.numAccounts, numAccountsInDB)
   114         XCTAssertEqual(testee.numAccounts, numAccounts)
   115     }
   116 
   117     @discardableResult private func createAndSaveSecondAccount () -> Account {
   118         let secondAccount = SecretTestData().createWorkingAccount(number: 1)
   119         secondAccount.save()
   120         return secondAccount
   121     }
   122 
   123     private func deleteAllAccounts() {
   124         Account.all().forEach { $0.delete() }
   125     }
   126 
   127     private func account(at: Int) -> Account {
   128         return Account.all()[at]
   129     }
   130 
   131     private func firstAccount() -> Account {
   132         return account(at: 0)
   133     }
   134 
   135     private func secondAccount() -> Account {
   136         return account(at: 1)
   137     }
   138 }
   139 
   140 // MARK: TestResultDelegate
   141 
   142 extension AccountPickerViewModelTest {
   143 
   144     class TestResultDelegate: AccountPickerViewModelResultDelegate {
   145         let expectDidSelectCalled: XCTestExpectation
   146         let accountToCompare: Account
   147         let mustEqualSelected: Bool
   148 
   149         init(expectDidSelectCalled: XCTestExpectation,
   150              accountToCompare: Account,
   151              mustEqualSelected: Bool = true) {
   152             self.expectDidSelectCalled = expectDidSelectCalled
   153             self.accountToCompare = accountToCompare
   154             self.mustEqualSelected = mustEqualSelected
   155         }
   156 
   157         func accountPickerViewModel(_ vm: AccountPickerViewModel, didSelect account: Account) {
   158             expectDidSelectCalled.fulfill()
   159             if mustEqualSelected {
   160                 XCTAssertEqual(accountToCompare, account)
   161             } else {
   162                 XCTAssertNotEqual(accountToCompare, account)
   163             }
   164         }
   165     }
   166 }