<commit_before>//
//  LinkInlineSignupElementSnapshotTests.swift
//  StripeiOS
//
//  Created by <NAME> on 1/21/22.
//  Copyright © 2022 Stripe, Inc. All rights reserved.
//

import UIKit
import FBSnapshotTestCase

@testable import Stripe
@_spi(STP) import StripeUICore
@_spi(STP) import StripeCoreTestUtils

class LinkInlineSignupElementSnapshotTests: FBSnapshotTestCase {

    override func setUp() {
        super.setUp()
//        recordMode = true
    }

    func testDefaultState() {
        let sut = makeSUT()
        verify(sut)
    }

    func testExpandedState() {
        let sut = makeSUT(saveCheckboxChecked: true, emailAddress: "<EMAIL>")
        verify(sut)
    }

    func testExpandedState_nonUS() {
        let sut = makeSUT(saveCheckboxChecked: true, emailAddress: "<EMAIL>", country: "CA")
        verify(sut)
    }

    func verify(
        _ element: LinkInlineSignupElement,
        identifier: String? = nil,
        file: StaticString = #filePath,
        line: UInt = #line
    ) {
        element.view.autosizeHeight(width: 340)
        STPSnapshotVerifyView(element.view, identifier: identifier, file: file, line: line)
    }

}

extension LinkInlineSignupElementSnapshotTests {

    struct MockAccountService: LinkAccountServiceProtocol {
        func lookupAccount(
            withEmail email: String?,
            completion: @escaping (Result<PaymentSheetLinkAccount?, Error>) -> Void
        ) {
            completion(.success(
                PaymentSheetLinkAccount(email: "<EMAIL>", session: nil, publishableKey: nil)
            ))
        }
        
        func hasEmailLoggedOut(email: String) -> Bool {
            // TODO(porter): Determine if we want to implement this in tests
            return false
        }
    }

    func makeSUT(
        saveCheckboxChecked: Bool = false,
        emailAddress: String? = nil,
        country: String = "US"
    ) -> LinkInlineSignupElement {
        var configuration = PaymentSheet.Configuration()
        configuration.merchantDisplayName = "[Merchant]"

        let viewModel = LinkInlineSignupViewModel(
            configuration: configuration,
            accountService: MockAccountService(),
            country: country
        )

        viewModel.saveCheckboxChecked = saveCheckboxChecked
        viewModel.emailAddress = emailAddress

        if emailAddress != nil {
            // Wait for account to load
            let expectation = notNullExpectation(for: viewModel, keyPath: \.linkAccount)
            wait(for: [expectation], timeout: 2)
        }

        return .init(viewModel: viewModel)
    }<commit_msg>Reset the current ElementsUITheme to fix flaky snapshot tests (#1146)<commit_after>//
//  LinkInlineSignupElementSnapshotTests.swift
//  StripeiOS
//
//  Created by <NAME> on 1/21/22.
//  Copyright © 2022 Stripe, Inc. All rights reserved.
//

import UIKit
import FBSnapshotTestCase

@testable import Stripe
@_spi(STP) import StripeUICore
@_spi(STP) import StripeCoreTestUtils

class LinkInlineSignupElementSnapshotTests: FBSnapshotTestCase {

    override func setUp() {
        super.setUp()
        // Reset the theme to avoid test flakiness
        ElementsUITheme.current = .default
//        recordMode = true
    }

    func testDefaultState() {
        let sut = makeSUT()
        verify(sut)
    }

    func testExpandedState() {
        let sut = makeSUT(saveCheckboxChecked: true, emailAddress: "<EMAIL>")
        verify(sut)
    }

    func testExpandedState_nonUS() {
        let sut = makeSUT(saveCheckboxChecked: true, emailAddress: "<EMAIL>", country: "CA")
        verify(sut)
    }

    func verify(
        _ element: LinkInlineSignupElement,
        identifier: String? = nil,
        file: StaticString = #filePath,
        line: UInt = #line
    ) {
        element.view.autosizeHeight(width: 340)
        STPSnapshotVerifyView(element.view, identifier: identifier, file: file, line: line)
    }

}

extension LinkInlineSignupElementSnapshotTests {

    struct MockAccountService: LinkAccountServiceProtocol {
        func lookupAccount(
            withEmail email: String?,
            completion: @escaping (Result<PaymentSheetLinkAccount?, Error>) -> Void
        ) {
            completion(.success(
                PaymentSheetLinkAccount(email: "<EMAIL>", session: nil, publishableKey: nil)
            ))
        }
        
        func hasEmailLoggedOut(email: String) -> Bool {
            // TODO(porter): Determine if we want to implement this in tests
            return false
        }
    }

    func makeSUT(
        saveCheckboxChecked: Bool = false,
        emailAddress: String? = nil,
        country: String = "US"
    ) -> LinkInlineSignupElement {
        var configuration = PaymentSheet.Configuration()
        configuration.merchantDisplayName = "[Merchant]"

        let viewModel = LinkInlineSignupViewModel(
            configuration: configuration,
            accountService: MockAccountService(),
            country: country
        )

        viewModel.saveCheckboxChecked = saveCheckboxChecked
        viewModel.emailAddress = emailAddress

        if emailAddress != nil {
            // Wait for account to load
            let expectation = notNullExpectation(for: viewModel, keyPath: \.linkAccount)
            wait(for: [expectation], timeout: 10)
        }

        return .init(viewModel: viewModel)
    }
{'fraction_non_alphanumeric': 0.08987929433611885, 'fraction_numerical': 0.005756731662024141, 'mean_word_length': 2.393824826717076, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2364445', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1457, 'n_words': 368}