Skip to content

Commit

Permalink
Merge pull request #279 from boostcampwm2023/iOS/test#278
Browse files Browse the repository at this point in the history
test: 홈 테스트 코드 수정
  • Loading branch information
loinsir authored Dec 11, 2023
2 parents 55168bf + d4f9337 commit 7606181
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 85 deletions.
7 changes: 0 additions & 7 deletions iOS/Layover/Layover/Scenes/Home/HomePresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import UIKit

protocol HomePresentationLogic {
func presentPosts(with response: HomeModels.FetchPosts.Response)
func presentThumbnailImage(with response: HomeModels.FetchThumbnailImageData.Response)
func presentPlaybackScene(with response: HomeModels.PlayPosts.Response)
func presentTagPlayList(with response: HomeModels.ShowTagPlayList.Response)
}
Expand Down Expand Up @@ -42,12 +41,6 @@ final class HomePresenter: HomePresentationLogic {
viewController?.displayPosts(with: viewModel)
}

func presentThumbnailImage(with response: Models.FetchThumbnailImageData.Response) {
let viewModel = Models.FetchThumbnailImageData.ViewModel(imageData: response.imageData,
indexPath: response.indexPath)
viewController?.displayThumbnailImage(with: viewModel)
}

// MARK: - UseCase Present PlaybackScene

func presentPlaybackScene(with response: HomeModels.PlayPosts.Response) {
Expand Down
9 changes: 0 additions & 9 deletions iOS/Layover/Layover/Scenes/Home/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import UIKit

protocol HomeDisplayLogic: AnyObject {
func displayPosts(with viewModel: HomeModels.FetchPosts.ViewModel)
func displayThumbnailImage(with viewModel: HomeModels.FetchThumbnailImageData.ViewModel)
func routeToPlayback()
func routeToTagPlayList()
}
Expand Down Expand Up @@ -238,14 +237,6 @@ extension HomeViewController: HomeDisplayLogic {
}
}

func displayThumbnailImage(with viewModel: HomeModels.FetchThumbnailImageData.ViewModel) {
guard let cell = carouselCollectionView.cellForItem(at: viewModel.indexPath) as? HomeCarouselCollectionViewCell,
let thumbnailImage = UIImage(data: viewModel.imageData)
else { return }

cell.configure(thumbnailImage: thumbnailImage)
}

func routeToPlayback() {
router?.routeToPlayback()
}
Expand Down
16 changes: 13 additions & 3 deletions iOS/Layover/LayoverTests/Mocks/Workers/MockHomeWorker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ final class MockHomeWorker: HomeWorkerProtocol {

func fetchPosts() async -> [Post]? {
guard let fileLocation = Bundle(for: type(of: self)).url(forResource: "PostList",
withExtension: "json") else { return nil }
withExtension: "json"),
let imageDataLocation = Bundle(for: type(of: self)).url(forResource: "sample",
withExtension: "jpeg")
else { return nil }

do {
let mockData = try Data(contentsOf: fileLocation)
Expand All @@ -36,8 +39,15 @@ final class MockHomeWorker: HomeWorkerProtocol {
let endPoint: EndPoint = EndPoint<Response<[PostDTO]>>(path: "/board/home",
method: .GET)
let response = try await provider.request(with: endPoint)
guard let data = response.data else { return nil }
return data.map { $0.toDomain() }
guard let responseData = response.data else { return nil }

let data = responseData.map {
var domainData = $0.toDomain()
domainData.thumbnailImageData = try? Data(contentsOf: imageDataLocation)
return domainData
}

return data
} catch {
os_log(.error, log: .data, "%@", error.localizedDescription)
return nil
Expand Down
64 changes: 17 additions & 47 deletions iOS/Layover/LayoverTests/Scenes/Home/HomeInteractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,18 @@ final class HomeInteractorTests: XCTestCase {
final class HomePresentationLogicSpy: HomePresentationLogic { // 호출 테스트를 위한 Spy
var presentPostsCalled = false
var presentPostsReceivedResponse: Models.FetchPosts.Response!
var presentThumbnailImageCalled = false
var presentThumbnailImageReceivedResponse: Models.FetchThumbnailImageData.Response!
var presentPlaybackSceneCalled = false
var presentTagPlayListCalled = false

func presentPosts(with response: Models.FetchPosts.Response) {
presentPostsCalled = true
presentPostsReceivedResponse = response
}

func presentThumbnailImage(with response: Models.FetchThumbnailImageData.Response) {
presentThumbnailImageCalled = true
presentThumbnailImageReceivedResponse = response
}


func presentPlaybackScene(with response: Models.PlayPosts.Response) {
presentPlaybackSceneCalled = true
}

func presentTagPlayList(with response: Models.ShowTagPlayList.Response) {
presentTagPlayListCalled = true
}
Expand Down Expand Up @@ -90,44 +83,21 @@ final class HomeInteractorTests: XCTestCase {
XCTAssertEqual(spy.presentPostsReceivedResponse.posts.count, 4, "fetchPost()가 presenter에게 올바른 데이터를 저장했다.")
}

// func test_fetchThumbnailImageData는_presenter의_presentThumbnailImage를_호출한다() async throws {
// // Arrange
// let spy = HomePresentationLogicSpy()
// sut.presenter = spy
//
// guard let imageURL = URL(string: "https://cdnimg.melon.co.kr/resource/image/cds/musicstory/imgUrl20210831030133473.jpg/melon/quality/90/optimize") else {
// XCTFail("URL 생성 실패")
// return
// }
//
// let request = Models.FetchThumbnailImageData.Request(imageURL: imageURL, indexPath: IndexPath())
//
// // Act
// _ = await sut.fetchThumbnailImageData(with: request).value
//
// // Assert
// XCTAssertTrue(spy.presentThumbnailImageCalled, "fetchThumbnailImageData()가 presenter의 presentThumbnailImage()를 호출했다.")
// }
//
// func test_fetchThumbnailImageData는_presenter에게_올바른_데이터를_전달한다() async throws {
// // Arrange
// let spy = HomePresentationLogicSpy()
// sut.presenter = spy
//
// guard let imageURL = URL(string: "https://cdnimg.melon.co.kr/resource/image/cds/musicstory/imgUrl20210831030133473.jpg/melon/quality/90/optimize") else {
// XCTFail("URL 생성 실패")
// return
// }
//
// let request = Models.FetchThumbnailImageData.Request(imageURL: imageURL, indexPath: IndexPath())
//
// // Act
// _ = await sut.fetchThumbnailImageData(with: request).value
//
// // Assert
// let assertImageData = try Data(contentsOf: Bundle(for: type(of: self)).url(forResource: "sample", withExtension: "jpeg")!)
// XCTAssertEqual(spy.presentThumbnailImageReceivedResponse.imageData, assertImageData,"fetchThumbnailImageData()가 presenter에게 올바른 데이터를 저장했다.")
// }
func test_fetchPost는_presenter에게_올바른_이미지_데이터를_전달한다() async throws {
// Arrange
let spy = HomePresentationLogicSpy()
sut.presenter = spy
let request = Models.FetchPosts.Request()
let imageData = try Data(contentsOf: Bundle(for: type(of: self)).url(forResource: "sample", withExtension: "jpeg")!)

// Act
_ = await sut.fetchPosts(with: request).value

// Assert
spy.presentPostsReceivedResponse.posts.forEach {
XCTAssertEqual($0.thumbnailImageData, imageData, "fetchPost()가 presenter에게 올바른 이미지 데이터를 전달했다.")
}
}

func test_playPosts는_자신의_selectedIndex값을_변경한다() async throws {
// Arrange
Expand Down
19 changes: 0 additions & 19 deletions iOS/Layover/LayoverTests/Scenes/Home/HomePresenterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ final class HomePresenterTests: XCTestCase {
displayPostsReceivedViewModel = viewModel
}

func displayThumbnailImage(with viewModel: Layover.HomeModels.FetchThumbnailImageData.ViewModel) {
displayThumbnailImageCalled = true
displayThumbnailImageReceivedViewModel = viewModel
}

func routeToPlayback() {
routeToPlaybackCalled = true
}
Expand Down Expand Up @@ -172,20 +167,6 @@ final class HomePresenterTests: XCTestCase {
XCTAssertEqual(spy.displayPostsReceivedViewModel.displayedPosts.count, 0, "비디오 URL이 nil인 데이터는 뷰에게 전달하지 않는다.")
}

func test_presentThumbnailImage는_데이터를_받아오면_뷰의_displayThumbnailImage를_실행한다() {
// arrange
let spy = HomeDisplayLogicSpy()
sut.viewController = spy

let response = Models.FetchThumbnailImageData.Response(imageData: Data(), indexPath: IndexPath())

// act
sut.presentThumbnailImage(with: response)

// assert
XCTAssertTrue(spy.displayThumbnailImageCalled, "presentThumbnailImage는 displayThumbnailImage를 실행해서 뷰에게 데이터를 전달했다.")
}

func test_presentPlaybackScene은_뷰의_routeToPlayback을_실행한다() {
// arrange
let spy = HomeDisplayLogicSpy()
Expand Down

0 comments on commit 7606181

Please sign in to comment.