Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuhiro4949 committed Dec 19, 2019
1 parent b5593de commit 03851b8
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
20 changes: 18 additions & 2 deletions PagingKitTests/ContentAppearanceHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@
// ContentAppearanceHandlerTests.swift
// PagingKitTests
//
// Created by kahayash on 2019/12/19.
// Copyright © 2019 Kazuhiro Hayashi. All rights reserved.
// Copyright (c) 2019 Kazuhiro Hayashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import XCTest
@testable import PagingKit
Expand Down
112 changes: 112 additions & 0 deletions PagingKitTests/PagingContentViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,21 @@ import XCTest
class PagingContentViewControllerTests: XCTestCase {
var pagingContentViewController: PagingContentViewController?
var dataSource: PagingContentViewControllerDataSource?
var contentAppearanceHandler: ContentsAppearanceHandlerSpy!

override func setUp() {
super.setUp()
contentAppearanceHandler = ContentsAppearanceHandlerSpy()
pagingContentViewController = PagingContentViewController()
pagingContentViewController?.appearanceHandler = contentAppearanceHandler
pagingContentViewController?.view.frame = CGRect(x: 0, y: 0, width: 320, height: 667)
}

override func tearDown() {
super.tearDown()
dataSource = nil
contentAppearanceHandler = nil
pagingContentViewController = nil
}

func testCallingDataSource() {
Expand Down Expand Up @@ -254,3 +259,110 @@ class PagingContentVcDataSourceSpy: NSObject, PagingContentViewControllerDataSou
return vc
}
}

// MARK:- Appearance
extension PagingContentViewControllerTests {
func test_Appear_callApparance() {

let expectation = XCTestExpectation(description: "finish reloadData")
let dataSource = PagingContentVcDataSourceSpy()
pagingContentViewController?.dataSource = dataSource
pagingContentViewController?.loadViewIfNeeded()
pagingContentViewController?.reloadData(with: 2) { [unowned self] in
self.pagingContentViewController?.beginAppearanceTransition(true, animated: false)

XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.0, .viewWillAppear)
XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.1, false)
XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.2, 2)

self.pagingContentViewController?.endAppearanceTransition()

XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.0, .viewDidAppear)
XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.1, false)
XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.2, 2)

expectation.fulfill()
}

wait(for: [expectation], timeout: 0.2)
}


func test_Dissapear_callApparance() {
let expectation = XCTestExpectation(description: "finish reloadData")
let dataSource = PagingContentVcDataSourceSpy()
pagingContentViewController?.dataSource = dataSource
pagingContentViewController?.loadViewIfNeeded()
pagingContentViewController?.reloadData(with: 3) { [unowned self] in
self.pagingContentViewController?.beginAppearanceTransition(false, animated: false)

XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.0, .viewWillDisappear)
XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.1, false)
XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.2, 3)

self.pagingContentViewController?.endAppearanceTransition()

XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.0, .viewDidDisappear)
XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.1, false)
XCTAssertEqual(self.contentAppearanceHandler.callApparance_args!.2, 3)

expectation.fulfill()
}

wait(for: [expectation], timeout: 0.2)
}

func test_scrollAppearance() {
let expectation = XCTestExpectation(description: "finish reloadData")
let dataSource = PagingContentVcDataSourceSpy()
pagingContentViewController?.dataSource = dataSource
pagingContentViewController?.loadViewIfNeeded()
pagingContentViewController?.reloadData(with: 3) { [unowned self] in
self.pagingContentViewController!.scrollViewWillBeginDragging(self.pagingContentViewController!.scrollView)
self.pagingContentViewController?.scrollViewDidScroll(self.pagingContentViewController!.scrollView)

XCTAssertEqual(self.contentAppearanceHandler.beginDragging_args, 3)

self.pagingContentViewController?.scroll(to: 4, animated: false)
self.pagingContentViewController!.scrollViewDidEndDragging(self.pagingContentViewController!.scrollView, willDecelerate: false)


XCTAssertEqual(self.contentAppearanceHandler.stopScrolling_args, 4)

expectation.fulfill()
}

wait(for: [expectation], timeout: 0.2)
}
}


// MARK:- Test Double
class ContentsAppearanceHandlerSpy: ContentsAppearanceHandlerProtocol {
var contentsDequeueHandler: (() -> [UIViewController?]?)?

var beginDragging_args: Int?
func beginDragging(at index: Int) {
beginDragging_args = index
}

var stopScrolling_args: Int?
func stopScrolling(at index: Int) {
stopScrolling_args = index
}

var callApparance_args: (ContentsAppearanceHandler.Apperance, Bool, Int)?
func callApparance(_ apperance: ContentsAppearanceHandler.Apperance, animated: Bool, at index: Int) {
callApparance_args = (apperance, animated, index)
}

var preReload_args: Int?
func preReload(at index: Int) {
preReload_args = index
}

var postReload_ags: Int?
func postReload(at index: Int) {
postReload_ags = index
}
}

0 comments on commit 03851b8

Please sign in to comment.