Skip to content

Commit

Permalink
Merge pull request #183 from adevinta/feature/component/switch/2-view
Browse files Browse the repository at this point in the history
[Component] Switch: View, VM, Demo
  • Loading branch information
robergro authored Jun 22, 2023
2 parents c692bf1 + 1440369 commit d8b5826
Show file tree
Hide file tree
Showing 29 changed files with 2,562 additions and 246 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ spark-ios-snapshots

#sourcery
*Generated/

vendor/
4 changes: 2 additions & 2 deletions core/Sources/Common/FullColorToken/FullColorToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protocol FullColorToken {

struct FullColorTokenDefault: FullColorToken {

// MARK: - Private Properties
// MARK: - Public Properties

var uiColor: UIColor {
return self.colorToken.uiColor.withAlphaComponent(self.opacity)
Expand All @@ -28,7 +28,7 @@ struct FullColorTokenDefault: FullColorToken {
return self.colorToken.color.opacity(self.opacity)
}

// MARK: - Public Properties
// MARK: - Private Properties

private let colorToken: ColorToken
private let opacity: CGFloat
Expand Down
2 changes: 1 addition & 1 deletion core/Sources/Components/Switch/Enum/SwitchAlignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

/// The alignment of the switch.
public enum SwitchAlignment {
public enum SwitchAlignment: CaseIterable {
/// Switch is on the left, text is on the right
case left
/// Switch is on the right, text is on the left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

/// The intent color of the switch.
public enum SwitchIntentColor {
public enum SwitchIntentColor: CaseIterable {
case alert
case error
case info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// Copyright © 2023 Adevinta. All rights reserved.
//

import Foundation

// sourcery: AutoMockable
protocol SwitchColorables {
var toggleBackgroundColors: SwitchStatusColorables { get }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class SwitchGetColorUseCaseTests: XCTestCase {
// WHEN

let colors = useCase.execute(
forIntentColor: .alert,
forIntentColor: intentColorMock,
colors: colorsMock,
dims: dimsMock
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,112 +12,135 @@ import SwiftUI

final class SwitchGetImageUseCaseTests: XCTestCase {

// MARK: - Tests
// MARK: - Properties

func test_execute_for_all_cases() throws {
// GIVEN / WHEN
let onImageMock = Image(systemName: "square.and.arrow.up")
let offImageMock = Image(systemName: "square.and.arrow.down")
private let onImageMock = Image(systemName: "square.and.arrow.up")
private let offImageMock = Image(systemName: "square.and.arrow.down")

let onUIImageMock = IconographyTests.shared.switchOn
let offUIImageMock = IconographyTests.shared.switchOff
private let onUIImageMock = IconographyTests.shared.switchOn
private let offUIImageMock = IconographyTests.shared.switchOff

let variantMock = SwitchVariant(
onImage: onImageMock,
offImage: offImageMock
private lazy var variantMock: SwitchVariant = {
return .init(
onImage: self.onImageMock,
offImage: self.offImageMock
)
let variantUIMock = SwitchVariant(
onImage: onUIImageMock,
offImage: offUIImageMock
}()
private lazy var variantUIMock: SwitchVariant = {
return .init(
onImage: self.onUIImageMock,
offImage: self.offUIImageMock
)
}()

let expectedOnImage = SwitchImage(
image: onImageMock,
uiImage: onUIImageMock
private lazy var expectedOnImage: SwitchImage = {
return .init(
image: self.onImageMock,
uiImage: self.onUIImageMock
)
let expectedOffImage = SwitchImage(
image: offImageMock,
uiImage: offUIImageMock
}()
private lazy var expectedOffImage: SwitchImage = {
return .init(
image: self.offImageMock,
uiImage: self.offUIImageMock
)
}()

let items: [(
givenIsOn: Bool,
givenVariant: SwitchVariant?,
expectedImage: SwitchImage?
)] = [
// **
// SwitfUI Variant
(
givenIsOn: true,
givenVariant: variantMock,
expectedImage: expectedOnImage
),
(
givenIsOn: false,
givenVariant: variantMock,
expectedImage: expectedOffImage
),
// **

// **
// UIKit Variant
(
givenIsOn: true,
givenVariant: variantUIMock,
expectedImage: expectedOnImage
),
(
givenIsOn: false,
givenVariant: variantUIMock,
expectedImage: expectedOffImage
),
// **

// **
// Without Variant
(
givenIsOn: true,
givenVariant: nil,
expectedImage: nil
),
(
givenIsOn: false,
givenVariant: nil,
expectedImage: nil
)
// **
]

for item in items {
let errorPrefixMessage = " for \(item.givenIsOn) isOn"

let useCase = SwitchGetImageUseCase()
let image = useCase.execute(
forIsOn: item.givenIsOn,
variant: item.givenVariant
)

// THEN
if let image = image {
if item.givenIsOn {
XCTAssertEqual(image.image,
item.givenVariant?.onImage,
"Wrong on image" + errorPrefixMessage)
XCTAssertEqual(image.uiImage,
item.givenVariant?.onUIImage,
"Wrong on UIImage" + errorPrefixMessage)
} else {
XCTAssertEqual(image.image,
item.givenVariant?.offImage,
"Wrong off image" + errorPrefixMessage)
XCTAssertEqual(image.uiImage,
item.givenVariant?.offUIImage,
"Wrong off UIImage" + errorPrefixMessage)
}
// MARK: - SwiftUI Variant Tests

func test_execute_when_isOn_is_true_and_variant_is_set_with_swiftUI_variant_images() throws {
try self.testExecute(
givenIsOn: true,
givenVariant: self.variantMock,
expectedImage: self.expectedOnImage
)
}

func test_execute_when_isOn_is_false_and_variant_is_set_with_swiftUI_variant_images() throws {
try self.testExecute(
givenIsOn: false,
givenVariant: self.variantMock,
expectedImage: self.expectedOffImage
)
}

// MARK: - UIKit Variant Tests

func test_execute_when_isOn_is_true_and_variant_is_set_with_UIKit_variant_images() throws {
try self.testExecute(
givenIsOn: true,
givenVariant: self.variantUIMock,
expectedImage: self.expectedOnImage
)
}

func test_execute_when_isOn_is_false_and_variant_is_set_with_UIKit_variant_images() throws {
try self.testExecute(
givenIsOn: false,
givenVariant: self.variantUIMock,
expectedImage: self.expectedOffImage
)
}

// MARK: - UIKit Variant Tests

func test_execute_when_isOn_is_true_and_variant_is_set_without_variant() throws {
try self.testExecute(
givenIsOn: true,
givenVariant: nil,
expectedImage: nil
)
}

func test_execute_when_isOn_is_false_and_variant_is_set_without_variant() throws {
try self.testExecute(
givenIsOn: true,
givenVariant: nil,
expectedImage: nil
)
}
}

// MARK: - Execute Testing

private extension SwitchGetImageUseCaseTests {

func testExecute(
givenIsOn: Bool,
givenVariant: SwitchVariant?,
expectedImage: SwitchImage?
) throws {
// GIVEN
let errorPrefixMessage = " for \(givenIsOn) isOn"

let useCase = SwitchGetImageUseCase()

// WHEN
let image = useCase.execute(
forIsOn: givenIsOn,
variant: givenVariant
)

// THEN
if let image = image {
if givenIsOn {
XCTAssertEqual(image.image,
givenVariant?.onImage,
"Wrong on image" + errorPrefixMessage)
XCTAssertEqual(image.uiImage,
givenVariant?.onUIImage,
"Wrong on UIImage" + errorPrefixMessage)
} else {
XCTAssertNil(item.expectedImage,
"Image should be nil" + errorPrefixMessage)
XCTAssertEqual(image.image,
givenVariant?.offImage,
"Wrong off image" + errorPrefixMessage)
XCTAssertEqual(image.uiImage,
givenVariant?.offUIImage,
"Wrong off UIImage" + errorPrefixMessage)
}
} else {
XCTAssertNil(expectedImage,
"Image should be nil" + errorPrefixMessage)
}
}
}
Loading

0 comments on commit d8b5826

Please sign in to comment.