Skip to content

Commit

Permalink
Refactor UIWindow system
Browse files Browse the repository at this point in the history
Add more tests to AddCardViewController
  • Loading branch information
dogo committed Nov 26, 2023
1 parent 36d27c6 commit ecbda64
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 15 deletions.
16 changes: 16 additions & 0 deletions SWDestinyTradesTests/Extension/CGRect+Testable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CGRect+Testable.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 26/11/23.
// Copyright © 2023 Diogo Autilio. All rights reserved.
//

import CoreGraphics

extension CGRect {

static var testDevice: CGRect {
return CGRect(x: 0, y: 0, width: 375, height: 812)
}
}
4 changes: 0 additions & 4 deletions SWDestinyTradesTests/Extension/UIWindow+Testable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import UIKit

extension UIWindow {

static func framed(frame: CGRect = CGRect(x: 0, y: 0, width: 375, height: 812)) -> UIWindow {
return UIWindow(frame: frame)
}

func showTestWindow(controller: UIViewController) {
rootViewController = controller
makeKeyAndVisible()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ final class AboutViewControllerTests: QuickSpec {

override class func spec() {

let window = UIWindow.framed()
var window: UIWindow!

describe("About view controller") {

var sut: AboutViewController!
var view: AboutViewSpy!

beforeEach {
window = UIWindow(frame: .testDevice)
view = AboutViewSpy()
sut = AboutViewController(with: view)
let navigationController = UINavigationController(rootViewController: sut)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Nimble
import Quick
import UIKit

@testable import PKHUD
@testable import SWDestinyTrades

final class AddCardViewControllerTests: QuickSpec {
Expand All @@ -20,15 +21,25 @@ final class AddCardViewControllerTests: QuickSpec {

var sut: AddCardViewController!
var view: AddCardViewSpy!
var database: DatabaseProtocol!
var keyWindow: UIWindow!

beforeEach {
keyWindow = UIWindow(frame: .testDevice)
database = RealmDatabaseHelper.createMemoryDatabase(identifier: "UserCollection")
view = AddCardViewSpy()
sut = AddCardViewController(with: view,
service: SWDestinyService(client: HttpClientMock()),
database: nil,
database: database,
person: .stub(),
userCollection: .stub(),
type: .collection)
let navigationController = UINavigationController(rootViewController: sut)
keyWindow.showTestWindow(controller: navigationController)
}

afterEach {
keyWindow.cleanTestWindow()
}

it("should be able to create a controller") {
Expand All @@ -40,14 +51,66 @@ final class AddCardViewControllerTests: QuickSpec {
}

it("should have the expected navigation title") {
_ = UINavigationController(rootViewController: sut)
sut.viewWillAppear(true)
sut.viewWillAppear(false)

expect(sut.navigationItem.title).to(equal(L10n.addCard))
}

it("didSelectCard") {
sut.viewDidLoad()
view.didSelectCard?(.stub())
context("binding actions") {

var collection: UserCollectionDTO!

context("didSelectCard") {

beforeEach {
collection = UserCollectionDTO.stub()
}

afterEach {
keyWindow.cleanTestWindow()
}

it("should insert a card into the collection database successfuly") {

sut = AddCardViewController(with: view,
service: SWDestinyService(client: HttpClientMock()),
database: database,
person: .stub(),
userCollection: collection,
type: .collection)
sut.viewDidLoad()
view.didSelectCard?(.stub())

expect(keyWindow.subviews.contains { $0 is ContainerView }).to(beTrue())
}

it("should't insert a card into the collection database") {

collection.addCard(.stub())

sut = AddCardViewController(with: view,
service: SWDestinyService(client: HttpClientMock()),
database: database,
person: .stub(),
userCollection: collection,
type: .collection)
let navigationController = UINavigationController(rootViewController: sut)
keyWindow.showTestWindow(controller: navigationController)
sut.viewDidLoad()

view.didSelectCard?(.stub())

expect(keyWindow.subviews.contains { $0 is UIImage }).to(beTrue())
}
}

it("didSelectAccessory") {
view.didSelectAccessory?(.stub())
}

it("doingSearch") {
view.doingSearch?("sss")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class AddCardViewTests: QuickSpec {
var sut: AddCardViewController!
var service: SWDestinyService!
var navigation: UINavigationController!
let window = UIWindow.framed()
var window: UIWindow!

describe("AddCardViewController layout") {

Expand All @@ -31,6 +31,7 @@ final class AddCardViewTests: QuickSpec {
context("when it's initialized from Loan screen") {

beforeEach {
window = UIWindow(frame: .testDevice)
service = SWDestinyService(client: HttpClientMock())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ final class DeckGraphViewControllerTests: QuickSpec {

var sut: DeckGraphViewController!
var navigationController: UINavigationController!
let window = UIWindow.framed(frame: CGRect(x: 0, y: 0, width: 375, height: 1350))
var window: UIWindow!

describe("DeckGraphViewController layout") {

context("when it's initialized") {

beforeEach {
window = UIWindow(frame: CGRect(x: 0, y: 0, width: 375, height: 1350))
let deck = DeckDTO.stub()
let memoryDB = RealmDatabaseHelper.createMemoryDatabase(identifier: "DeckGraph")
try? memoryDB?.save(object: deck)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class PeopleListViewControllerTests: QuickSpec {
var sut: PeopleListViewController!
var database: DatabaseProtocol!
var navigation: UINavigationController!
let window = UIWindow.framed()
var window: UIWindow!

describe("PeopleListViewController layout") {

Expand All @@ -31,6 +31,7 @@ final class PeopleListViewControllerTests: QuickSpec {
context("when it's initialized from the tabbar") {

beforeEach {
window = UIWindow(frame: .testDevice)
database = RealmDatabaseHelper.createMemoryDatabase(identifier: "PeopleList")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class SetsListViewControllerTests: QuickSpec {
var service: SWDestinyService!
var client: HttpClientMock!
var navigation: UINavigationController!
let window = UIWindow.framed()
var window: UIWindow!

describe("SetsListViewController layout") {

Expand All @@ -32,6 +32,7 @@ final class SetsListViewControllerTests: QuickSpec {
context("when it's initialized") {

beforeEach {
window = UIWindow(frame: .testDevice)
client = HttpClientMock()
service = SWDestinyService(client: client)
}
Expand Down
4 changes: 4 additions & 0 deletions SWDestinyTradesTests/Stub/UserCollectionDTO+Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ extension UserCollectionDTO {
static func stub() -> UserCollectionDTO {
return UserCollectionDTO()
}

func addCard(_ card: CardDTO) {
myCollection.append(card)
}
}

0 comments on commit ecbda64

Please sign in to comment.