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