Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions App/Course App UITests/Course_App_UITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,50 @@
import XCTest

final class Course_App_UITests: XCTestCase {

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.

// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = false

// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testExample() throws {
func testSignInView() throws {
// UI tests must launch the application that they test.
let app = XCUIApplication()
app.launch()


let collectionViewsQuery = app.collectionViews
collectionViewsQuery.textFields["Email"].tap()
app.keys["delete"].tap()
app.keys["t"].tap()

collectionViewsQuery.secureTextFields["password"].tap()
app.keys["delete"].tap()
app.keys["t"].tap()
app.keys["e"].tap()
app.keys["s"].tap()
app.keys["t"].tap()
app.keys["more"].tap()
app.keys["1"].tap()
app.keys["2"].tap()
app.keys["3"].tap()

XCUIApplication()/*@START_MENU_TOKEN@*/.buttons["Return"]/*[[".keyboards",".buttons[\"return\"]",".buttons[\"Return\"]"],[[[-1,2],[-1,1],[-1,0,1]],[[-1,2],[-1,1]]],[0]]@END_MENU_TOKEN@*/.tap()

collectionViewsQuery/*@START_MENU_TOKEN@*/.buttons["SignIn"]/*[[".cells.buttons[\"SignIn\"]",".buttons[\"SignIn\"]"],[[[-1,1],[-1,0]]],[0]]@END_MENU_TOKEN@*/.tap()



// Use XCTAssert and related functions to verify your tests produce the correct results.
}

func testLaunchPerformance() throws {
if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) {
// This measures how long it takes to launch your application.
Expand Down
35 changes: 0 additions & 35 deletions App/Course App UnitTests/Course_App_UnitTests.swift

This file was deleted.

67 changes: 67 additions & 0 deletions App/Course App UnitTests/Endpoint/EndpointTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// EndpointTests.swift
// Course App UnitTests
//
// Created by Tomáš Duchoslav on 20.06.2024.
//

@testable import App_Course_Dev
import XCTest

final class EndpointTests: XCTestCase {

enum MockEndpoint: Endpoint {
case mockMethodGet
case mockMethodPost
case mockParameterGet

var host: URL {
URL(string: "hhtps://example.com")!
}

var path: String {
"api/test/path"
}

var method: HTTPMethod {
switch self {
case .mockMethodGet, .mockParameterGet:
.get
case .mockMethodPost:
.post
}
}
}

override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testHTTPMethod() throws {
guard let urlRequestGet = try? MockEndpoint.mockParameterGet.asURLRequest() else {
XCTFail("Can't create url request")
return
}

XCTAssert(urlRequestGet.httpMethod == HTTPMethod.get.rawValue, "good")

guard let urlRequestPost = try? MockEndpoint.mockMethodPost.asURLRequest() else {
XCTFail("Can't create url request")
return
}

XCTAssert(urlRequestPost.httpMethod == HTTPMethod.post.rawValue, "good")
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
32 changes: 32 additions & 0 deletions App/Course App UnitTests/Managers/MockAPIManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// MockAPIManager.swift
// Course App UnitTests
//
// Created by Tomáš Duchoslav on 20.06.2024.
//

@testable import App_Course_Dev
import Foundation

enum MockError: Error {
case noMockDataError
}

final class MockAPIManager: APIManaging {
var apiError: NetworkingError?
var mockData: Data?

func request<T>(_ endpoint: any Endpoint) async throws -> T where T : Decodable {
if let apiError{
throw apiError
}

if let data = mockData {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
return try decoder.decode(T.self, from: data)
} else {
throw MockError.noMockDataError
}
}
}
32 changes: 32 additions & 0 deletions App/Course App UnitTests/Managers/MockKeychainManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// MockKeychainManager.swift
// Course App UnitTests
//
// Created by Tomáš Duchoslav on 20.06.2024.
//

@testable import App_Course_Dev
import Foundation
import KeychainAccess

final class MockKeychainManager: KeychainManaging {
let keychain = Keychain()

func store<T>(key: String, value: T) throws where T : Encodable {
keychain[data: key] = try JSONEncoder().encode(value)
}

func fetch<T>(key: String) throws -> T where T : Decodable {
guard let data = try keychain.getData(key) else {
throw KeychainManagerError.dataNotFound
}

return try JSONDecoder().decode(T.self, from: data)
}

func remove(key: String) throws {
try keychain.remove(key)
}


}
29 changes: 29 additions & 0 deletions App/Course App UnitTests/Model/MockDataResponses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// MockDataResponses.swift
// Course App UnitTests
//
// Created by Tomáš Duchoslav on 20.06.2024.
//

@testable import App_Course_Dev
import Foundation

enum MockDataResponses {
static let mockJokeResponse =
"""
{
"id": "12345",
"categories": ["programming", "funny"],
"created_at": "2024-06-18T12:00:00Z",
"url": "https://api.exampe.com/jokes/12345",
"value": "Why do programmers prefer dark mode? Because light attracts bugs!"
}
""".data(using: .utf8)!

static let mockCategoriesResponse =
"""
[
"programming", "funny"
]
""".data(using: .utf8)!
}
42 changes: 42 additions & 0 deletions App/Course App UnitTests/ServiceTests/JokeServiceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// JokeServiceTests.swift
// Course App UnitTests
//
// Created by Tomáš Duchoslav on 20.06.2024.
//

@testable import App_Course_Dev
import XCTest

final class JokeServiceTests: XCTestCase {

var mockApiManager: MockAPIManager!
var jokeService: JokeService!

override func setUpWithError() throws {
try super.setUpWithError()
mockApiManager = MockAPIManager()
jokeService = JokeService(apiManager: mockApiManager)
}

override func tearDownWithError() throws {
mockApiManager = nil
jokeService = nil
try super.tearDownWithError()
}

func testFetchCategories() async throws {
mockApiManager.mockData = MockDataResponses.mockCategoriesResponse
let categories = try await jokeService.loadCategories()
XCTAssert(categories.count == 2, "good")
XCTAssert(categories.contains(where: { $0 == "funny" }))
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
43 changes: 43 additions & 0 deletions App/Course App UnitTests/ServiceTests/KeychainServiceTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// KeychainServiceTests.swift
// Course App UnitTests
//
// Created by Tomáš Duchoslav on 20.06.2024.
//

@testable import App_Course_Dev
import XCTest

final class KeychainServiceTests: XCTestCase {

var mockKeychainManager: MockKeychainManager!
var keychainService: KeychainService!

override func setUpWithError() throws {
try super.setUpWithError()
mockKeychainManager = MockKeychainManager()
keychainService = KeychainService(keychainManager: mockKeychainManager)
}

override func tearDownWithError() throws {
mockKeychainManager = nil
keychainService = nil
try super.tearDownWithError()
}

func testStoreAuthData() throws {
try keychainService.storeAuthData(authData: "12345")
XCTAssert(try mockKeychainManager.keychain.contains("com.course.app.authData"))

try keychainService.removeAuthData()
XCTAssert(try !mockKeychainManager.keychain.contains("com.course.app.authData"))
}

func testPerformanceExample() throws {
// This is an example of a performance test case.
self.measure {
// Put the code you want to measure the time of here.
}
}

}
Loading