Skip to content

Commit

Permalink
better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimillian committed Jun 12, 2024
1 parent 0032c1d commit f82b2b4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
39 changes: 21 additions & 18 deletions Packages/Timeline/Tests/TimelineTests/TimelineFilterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ import Testing
import Foundation
@testable import Timeline

@Test
func testTimelineCodableHome() {
#expect(testCodableOn(filter: .home))
#expect(testCodableOn(filter: .local))
#expect(testCodableOn(filter: .federated))
#expect(testCodableOn(filter: .remoteLocal(server: "me.dm", filter: .local)))
#expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: nil)))
#expect(testCodableOn(filter: .tagGroup(title: "test", tags: ["test"], symbolName: "test")))
#expect(testCodableOn(filter: .hashtag(tag: "test", accountId: nil)))
#expect(testCodableOn(filter: .list(list: .init(id: "test", title: "test"))))
}
@Suite("Timeline Filter Tests")
struct TimelineFilterTests {
@Test("All timeline filter can be decoded and encoded",
arguments: [TimelineFilter.home, TimelineFilter.local, TimelineFilter.federated,
TimelineFilter.remoteLocal(server: "me.dm", filter: .local),
TimelineFilter.tagGroup(title: "test", tags: ["test"], symbolName: nil),
TimelineFilter.tagGroup(title: "test", tags: ["test"], symbolName: "test"),
TimelineFilter.hashtag(tag: "test", accountId: nil),
TimelineFilter.list(list: .init(id: "test", title: "test"))])
func timelineCanEncodeAndDecode(filter: TimelineFilter) {
#expect(testCodableOn(filter: filter))
}

func testCodableOn(filter: TimelineFilter) -> Bool {
let encoder = JSONEncoder()
let decoder = JSONDecoder()
guard let data = try? encoder.encode(filter) else {
return false
}
let newFilter = try? decoder.decode(TimelineFilter.self, from: data)
return newFilter == filter

fileprivate func testCodableOn(filter: TimelineFilter) -> Bool {
let encoder = JSONEncoder()
let decoder = JSONDecoder()
guard let data = try? encoder.encode(filter) else {
return false
}
let newFilter = try? decoder.decode(TimelineFilter.self, from: data)
return newFilter == filter

}
57 changes: 33 additions & 24 deletions Packages/Timeline/Tests/TimelineTests/TimelineViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,71 @@ import Models
import Network
@testable import Timeline
import XCTest
import Testing

@MainActor
final class TimelineViewModelTests: XCTestCase {
var subject = TimelineViewModel()

override func setUp() async throws {
subject = TimelineViewModel()
@Suite("Timeline View Model tests")
struct Tests {
func makeSubject() -> TimelineViewModel {
let subject = TimelineViewModel()
let client = Client(server: "localhost")
subject.client = client
subject.timeline = .home
subject.isTimelineVisible = true
subject.timelineTask?.cancel()
return subject
}

func testStreamEventInsertNewStatus() async throws {

@Test
func streamEventInsertNewStatus() async throws {
let subject = makeSubject()
let isEmpty = await subject.datasource.isEmpty
XCTAssertTrue(isEmpty)
#expect(isEmpty)
await subject.datasource.append(.placeholder())
var count = await subject.datasource.count()
XCTAssertTrue(count == 1)
#expect(count == 1)
await subject.handleEvent(event: StreamEventUpdate(status: .placeholder()))
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
XCTAssertTrue(isEmpty)
#expect(isEmpty)
let status = Status.placeholder()
await subject.datasource.append(status)
var count = await subject.datasource.count()
XCTAssertTrue(count == 1)
#expect(count == 1)
await subject.handleEvent(event: StreamEventUpdate(status: status))
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
XCTAssertTrue(isEmpty)
#expect(isEmpty)
let status = Status.placeholder()
await subject.datasource.append(status)
var count = await subject.datasource.count()
XCTAssertTrue(count == 1)
#expect(count == 1)
await subject.handleEvent(event: StreamEventDelete(status: status.id))
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()
let isEmpty = await subject.datasource.isEmpty
XCTAssertTrue(isEmpty)
#expect(isEmpty)
await subject.datasource.append(status)
var count = await subject.datasource.count()
XCTAssertTrue(count == 1)
#expect(count == 1)
status = .init(id: status.id,
content: .init(stringValue: "test"),
account: status.account,
Expand Down Expand Up @@ -88,7 +97,7 @@ final class TimelineViewModelTests: XCTestCase {
await subject.handleEvent(event: StreamEventStatusUpdate(status: status))
let statuses = await subject.datasource.get()
count = await subject.datasource.count()
XCTAssertTrue(count == 1)
XCTAssertTrue(statuses.first?.content.asRawText == "test")
#expect(count == 1)
#expect(statuses.first?.content.asRawText == "test")
}
}

0 comments on commit f82b2b4

Please sign in to comment.