mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-12-03 22:06:29 +00:00
better tests
This commit is contained in:
parent
0032c1dbf5
commit
f82b2b4578
2 changed files with 55 additions and 43 deletions
|
@ -4,25 +4,28 @@ import Testing
|
||||||
import Foundation
|
import Foundation
|
||||||
@testable import Timeline
|
@testable import Timeline
|
||||||
|
|
||||||
@Test
|
@Suite("Timeline Filter Tests")
|
||||||
func testTimelineCodableHome() {
|
struct TimelineFilterTests {
|
||||||
#expect(testCodableOn(filter: .home))
|
@Test("All timeline filter can be decoded and encoded",
|
||||||
#expect(testCodableOn(filter: .local))
|
arguments: [TimelineFilter.home, TimelineFilter.local, TimelineFilter.federated,
|
||||||
#expect(testCodableOn(filter: .federated))
|
TimelineFilter.remoteLocal(server: "me.dm", filter: .local),
|
||||||
#expect(testCodableOn(filter: .remoteLocal(server: "me.dm", filter: .local)))
|
TimelineFilter.tagGroup(title: "test", tags: ["test"], symbolName: nil),
|
||||||
#expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: nil)))
|
TimelineFilter.tagGroup(title: "test", tags: ["test"], symbolName: "test"),
|
||||||
#expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: "test")))
|
TimelineFilter.hashtag(tag: "test", accountId: nil),
|
||||||
#expect(testCodableOn(filter: .hashtag(tag: "test", accountId: nil)))
|
TimelineFilter.list(list: .init(id: "test", title: "test"))])
|
||||||
#expect(testCodableOn(filter: .list(list: .init(id: "test", title: "test"))))
|
func timelineCanEncodeAndDecode(filter: TimelineFilter) {
|
||||||
}
|
#expect(testCodableOn(filter: filter))
|
||||||
|
}
|
||||||
fileprivate func testCodableOn(filter: TimelineFilter) -> Bool {
|
|
||||||
let encoder = JSONEncoder()
|
func testCodableOn(filter: TimelineFilter) -> Bool {
|
||||||
let decoder = JSONDecoder()
|
let encoder = JSONEncoder()
|
||||||
guard let data = try? encoder.encode(filter) else {
|
let decoder = JSONDecoder()
|
||||||
return false
|
guard let data = try? encoder.encode(filter) else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
let newFilter = try? decoder.decode(TimelineFilter.self, from: data)
|
||||||
|
return newFilter == filter
|
||||||
|
|
||||||
}
|
}
|
||||||
let newFilter = try? decoder.decode(TimelineFilter.self, from: data)
|
|
||||||
return newFilter == filter
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,62 +2,71 @@ import Models
|
||||||
import Network
|
import Network
|
||||||
@testable import Timeline
|
@testable import Timeline
|
||||||
import XCTest
|
import XCTest
|
||||||
|
import Testing
|
||||||
|
|
||||||
@MainActor
|
@MainActor
|
||||||
final class TimelineViewModelTests: XCTestCase {
|
@Suite("Timeline View Model tests")
|
||||||
var subject = TimelineViewModel()
|
struct Tests {
|
||||||
|
func makeSubject() -> TimelineViewModel {
|
||||||
override func setUp() async throws {
|
let subject = TimelineViewModel()
|
||||||
subject = TimelineViewModel()
|
|
||||||
let client = Client(server: "localhost")
|
let client = Client(server: "localhost")
|
||||||
subject.client = client
|
subject.client = client
|
||||||
subject.timeline = .home
|
subject.timeline = .home
|
||||||
subject.isTimelineVisible = true
|
subject.isTimelineVisible = true
|
||||||
subject.timelineTask?.cancel()
|
subject.timelineTask?.cancel()
|
||||||
|
return subject
|
||||||
}
|
}
|
||||||
|
|
||||||
func testStreamEventInsertNewStatus() async throws {
|
@Test
|
||||||
|
func streamEventInsertNewStatus() async throws {
|
||||||
|
let subject = makeSubject()
|
||||||
let isEmpty = await subject.datasource.isEmpty
|
let isEmpty = await subject.datasource.isEmpty
|
||||||
XCTAssertTrue(isEmpty)
|
#expect(isEmpty)
|
||||||
await subject.datasource.append(.placeholder())
|
await subject.datasource.append(.placeholder())
|
||||||
var count = await subject.datasource.count()
|
var count = await subject.datasource.count()
|
||||||
XCTAssertTrue(count == 1)
|
#expect(count == 1)
|
||||||
await subject.handleEvent(event: StreamEventUpdate(status: .placeholder()))
|
await subject.handleEvent(event: StreamEventUpdate(status: .placeholder()))
|
||||||
count = await subject.datasource.count()
|
count = await subject.datasource.count()
|
||||||
XCTAssertTrue(count == 2)
|
#expect(count == 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testStreamEventInsertDuplicateStatus() async throws {
|
@Test
|
||||||
|
func streamEventInsertDuplicateStatus() async throws {
|
||||||
|
let subject = makeSubject()
|
||||||
let isEmpty = await subject.datasource.isEmpty
|
let isEmpty = await subject.datasource.isEmpty
|
||||||
XCTAssertTrue(isEmpty)
|
#expect(isEmpty)
|
||||||
let status = Status.placeholder()
|
let status = Status.placeholder()
|
||||||
await subject.datasource.append(status)
|
await subject.datasource.append(status)
|
||||||
var count = await subject.datasource.count()
|
var count = await subject.datasource.count()
|
||||||
XCTAssertTrue(count == 1)
|
#expect(count == 1)
|
||||||
await subject.handleEvent(event: StreamEventUpdate(status: status))
|
await subject.handleEvent(event: StreamEventUpdate(status: status))
|
||||||
count = await subject.datasource.count()
|
count = await subject.datasource.count()
|
||||||
XCTAssertTrue(count == 1)
|
#expect(count == 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testStreamEventRemove() async throws {
|
@Test
|
||||||
|
func streamEventRemove() async throws {
|
||||||
|
let subject = makeSubject()
|
||||||
let isEmpty = await subject.datasource.isEmpty
|
let isEmpty = await subject.datasource.isEmpty
|
||||||
XCTAssertTrue(isEmpty)
|
#expect(isEmpty)
|
||||||
let status = Status.placeholder()
|
let status = Status.placeholder()
|
||||||
await subject.datasource.append(status)
|
await subject.datasource.append(status)
|
||||||
var count = await subject.datasource.count()
|
var count = await subject.datasource.count()
|
||||||
XCTAssertTrue(count == 1)
|
#expect(count == 1)
|
||||||
await subject.handleEvent(event: StreamEventDelete(status: status.id))
|
await subject.handleEvent(event: StreamEventDelete(status: status.id))
|
||||||
count = await subject.datasource.count()
|
count = await subject.datasource.count()
|
||||||
XCTAssertTrue(count == 0)
|
#expect(count == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testStreamEventUpdateStatus() async throws {
|
@Test
|
||||||
|
func streamEventUpdateStatus() async throws {
|
||||||
|
let subject = makeSubject()
|
||||||
var status = Status.placeholder()
|
var status = Status.placeholder()
|
||||||
let isEmpty = await subject.datasource.isEmpty
|
let isEmpty = await subject.datasource.isEmpty
|
||||||
XCTAssertTrue(isEmpty)
|
#expect(isEmpty)
|
||||||
await subject.datasource.append(status)
|
await subject.datasource.append(status)
|
||||||
var count = await subject.datasource.count()
|
var count = await subject.datasource.count()
|
||||||
XCTAssertTrue(count == 1)
|
#expect(count == 1)
|
||||||
status = .init(id: status.id,
|
status = .init(id: status.id,
|
||||||
content: .init(stringValue: "test"),
|
content: .init(stringValue: "test"),
|
||||||
account: status.account,
|
account: status.account,
|
||||||
|
@ -88,7 +97,7 @@ final class TimelineViewModelTests: XCTestCase {
|
||||||
await subject.handleEvent(event: StreamEventStatusUpdate(status: status))
|
await subject.handleEvent(event: StreamEventStatusUpdate(status: status))
|
||||||
let statuses = await subject.datasource.get()
|
let statuses = await subject.datasource.get()
|
||||||
count = await subject.datasource.count()
|
count = await subject.datasource.count()
|
||||||
XCTAssertTrue(count == 1)
|
#expect(count == 1)
|
||||||
XCTAssertTrue(statuses.first?.content.asRawText == "test")
|
#expect(statuses.first?.content.asRawText == "test")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue