Skip to content

Commit

Permalink
Merge pull request #127 from surfstudio/SPT-1998-unit-tests-part3
Browse files Browse the repository at this point in the history
SPT-1998 Модульные тесты часть 3
  • Loading branch information
mrandrewsmith authored Apr 24, 2024
2 parents 915d6f0 + 0264256 commit 3deeb87
Show file tree
Hide file tree
Showing 19 changed files with 1,684 additions and 14 deletions.
64 changes: 60 additions & 4 deletions NodeKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

File renamed without changes.
207 changes: 207 additions & 0 deletions NodeKitTests/UnitTests/ArrayDTODecodableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//
// ArrayDTODecodableTests.swift
// NodeKitTests
//
// Created by Andrei Frolov on 09.04.24.
// Copyright © 2024 Surf. All rights reserved.
//

@testable import NodeKit
import XCTest

final class ArrayDTODecodableTests: XCTestCase {

// MARK: - Lifecycle

override func tearDown() {
super.tearDown()
DTODecodableMock.flush()
}

// MARK: - Tests

func testFromDTO_thenFromDTOCalledInEachElement() throws {
// given

let array: [RawDecodableMock] = [
RawDecodableMock(),
RawDecodableMock(),
RawDecodableMock()
]

DTODecodableMock.stubbedFromResult = .success(.init())

// when

_ = try? Array<DTODecodableMock>.from(dto: array)

// then

XCTAssertEqual(DTODecodableMock.invokedFromCount, array.count)

DTODecodableMock.invokedFromParameterList.enumerated().forEach {
XCTAssertTrue($0.element === array[$0.offset])
}
}

func testFromDTO_whenDecodingFailure_thenFailureReceived() throws {
// given

let array: [RawDecodableMock] = [
RawDecodableMock()
]

var result: [DTODecodableMock]?
var receivedError: Error?

DTODecodableMock.stubbedFromResult = .failure(MockError.firstError)

// when

do {
result = try [DTODecodableMock].from(dto: array)
} catch {
receivedError = error
}

// then

let error = try XCTUnwrap(receivedError as? MockError)

XCTAssertNil(result)
XCTAssertEqual(error, .firstError)
}

func testFromDTO_whenDecodingSuccess_thenSuccessResultReceived() throws {
// given

let array: [RawDecodableMock] = [
RawDecodableMock(),
RawDecodableMock(),
RawDecodableMock()
]
let dtoDecodableMock = DTODecodableMock()

var result: [DTODecodableMock]?
var receivedError: Error?

DTODecodableMock.stubbedFromResult = .success(dtoDecodableMock)

// when

do {
result = try Array<DTODecodableMock>.from(dto: array)
} catch {
receivedError = error
}

// then

let value = try XCTUnwrap(result)

XCTAssertNil(receivedError)
XCTAssertEqual(value.count, array.count)

value.forEach {
XCTAssertTrue($0 === dtoDecodableMock)
}
}

func testToDTO_thenToDTOCalledInEachElement() throws {
// given

let array: [DTOEncodableMock<RawEncodableMock<Json>>] = [
.init(),
.init(),
.init()
]

array[0].stubbedToDTOResult = .success(.init())
array[1].stubbedToDTOResult = .success(.init())
array[2].stubbedToDTOResult = .success(.init())

// when

_ = try? array.toDTO()

// then

array.forEach {
XCTAssertEqual($0.invokedToDTOCount, 1)
}
}

func testToDTO_whenEncodingFailure_thenFailureReceived() throws {
// given

let array: [DTOEncodableMock<RawEncodableMock<Json>>] = [
.init(),
.init(),
.init()
]

array[0].stubbedToDTOResult = .success(.init())
array[1].stubbedToDTOResult = .failure(MockError.secondError)
array[2].stubbedToDTOResult = .success(.init())

var result: [RawEncodableMock<Json>]?
var receivedError: Error?

// when

do {
result = try array.toDTO()
} catch {
receivedError = error
}

// then

let error = try XCTUnwrap(receivedError as? MockError)

XCTAssertNil(result)
XCTAssertEqual(error, .secondError)
}

func testToDTO_whenEncodingSuccess_thenSuccessResultReceived() throws {
// given

let array: [DTOEncodableMock<RawEncodableMock<Json>>] = [
.init(),
.init(),
.init()
]

let expectedResult: [RawEncodableMock<Json>] = [
.init(),
.init(),
.init()
]

array[0].stubbedToDTOResult = .success(expectedResult[0])
array[1].stubbedToDTOResult = .success(expectedResult[1])
array[2].stubbedToDTOResult = .success(expectedResult[2])

var result: [RawEncodableMock<Json>]?
var receivedError: Error?

// when

do {
result = try array.toDTO()
} catch {
receivedError = error
}

// then

let value = try XCTUnwrap(result)

XCTAssertNil(receivedError)
XCTAssertEqual(value.count, array.count)

value.enumerated().forEach {
XCTAssertTrue($0.element === expectedResult[$0.offset])
}
}
}
Loading

0 comments on commit 3deeb87

Please sign in to comment.