Skip to content

Commit

Permalink
Refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
p-larson committed Aug 8, 2023
1 parent e76823c commit 7164b1e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 37 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct Section: Reducer {
case load(TaskResult<[Chapter]>)
case openChapter(Chapter)
case loadChapter(TaskResult<[Verse]>)
case select(Book, Chapter, [Verse], Verse)
case select(Book, Chapter, [Verse], Verse?)
}

@Dependency(\.bible) var bible: BibleClient
Expand Down
90 changes: 81 additions & 9 deletions BibleCore/Tests/ReaderCoreTests/PageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,40 @@ import BibleCore
final class PageTests: XCTestCase {

@MainActor func testTask() async {
let clock = TestClock()

let store = TestStore(
initialState: Page.State(),
reducer: {
Page()._printChanges()
}
)
initialState: Page.State()
) {
Page()._printChanges()
} withDependencies: {
$0.continuousClock = clock
}

await store.send(.task)

await store.receive(.open(.genesis, .mock, [.mock], focused: nil), timeout: .seconds(10)) {
$0.book = .genesis
$0.chapter = .mock
$0.verses = nil
}

await clock.advance(by: .seconds(10))

await store.receive(.add(.mock)) {
$0.verses = [.mock]
}
}

@MainActor func testForwardChapter() async {
let clock = TestClock()

let store = TestStore(
initialState: Page.State(book: .genesis, chapter: .mock, verses: .mock)
) {
Page()
} withDependencies: {
$0.continuousClock = clock
}

let nextChapter = Chapter(id: Chapter.mock.id + 1)
Expand All @@ -43,23 +56,33 @@ final class PageTests: XCTestCase {
}

await store.send(.paginateChapter(forward: true))

await clock.advance(by: .seconds(10))

await store.receive(.clear) {
$0.verses = nil
}

await store.receive(.open(.genesis, nextChapter, .mock, focused: nil)) {
$0.book = .genesis
$0.chapter = nextChapter
$0.verses = .mock
$0.verses = nil
}

await store.receive(.add(.mock)) {
$0.verses = [.mock]
}

}

@MainActor func testBackwardChapter() async {
let clock = TestClock()
let store = TestStore(
initialState: Page.State(book: .genesis, chapter: .mock, verses: .mock)
) {
Page()
} withDependencies: {
$0.continuousClock = clock
}

let lastChapter = Chapter(id: Chapter.mock.id - 1)
Expand All @@ -69,44 +92,73 @@ final class PageTests: XCTestCase {
}

await store.send(.paginateChapter(forward: false))

await clock.advance(by: .seconds(10))

await store.receive(.clear) {
$0.verses = nil
}

await store.receive(.open(.genesis, lastChapter, .mock, focused: nil)) {
$0.book = .genesis
$0.chapter = lastChapter
$0.verses = .mock
$0.verses = nil
}

await store.receive(.add(.mock)) {
$0.verses = [.mock]
}

}

@MainActor func testForwardBook() async {
let clock = TestClock()

let store = TestStore(
initialState: Page.State(book: .genesis, chapter: .mock, verses: .mock)
) {
Page()
} withDependencies: {
$0.continuousClock = clock
}

await store.send(.paginateBook(forward: true))

await clock.advance(by: .seconds(10))

await store.receive(.open(.exodus, .mock, .mock, focused: nil)) {
$0.book = .exodus
$0.verses = nil
}

await store.receive(.add(.mock)) {
$0.verses = [.mock]
}

}

@MainActor func testBackwardBook() async {
let clock = TestClock()

let store = TestStore(
initialState: Page.State(book: .genesis, chapter: .mock, verses: .mock)
) {
Page()
} withDependencies: {
$0.continuousClock = clock
}

await store.send(.paginateBook(forward: false))

await clock.advance(by: .seconds(10))

await store.receive(.open(.leviticus, .mock, .mock, focused: nil)) {
$0.book = .leviticus
$0.verses = nil
}

await store.receive(.add(.mock)) {
$0.verses = [.mock]
}
}

Expand All @@ -123,6 +175,8 @@ final class PageTests: XCTestCase {
Book.leviticus
]

let clock = TestClock()

let store = TestStore(
initialState: Page.State(book: .genesis, chapter: chapters.last!, verses: .mock)
) {
Expand All @@ -134,6 +188,8 @@ final class PageTests: XCTestCase {
$0.bible.books = {
return books
}

$0.continuousClock = clock
}

await store.send(.paginateChapter(forward: false))
Expand All @@ -142,9 +198,16 @@ final class PageTests: XCTestCase {
$0.verses = nil
}


await store.receive(.open(.genesis, chapters.first!, .mock, focused: nil)) {
$0.chapter = chapters.first!
$0.verses = .mock
$0.verses = nil
}

await clock.advance(by: .seconds(10))

await store.receive(.add(.mock)) {
$0.verses = [.mock]
}

// New book, should have "different" chapters
Expand All @@ -154,6 +217,8 @@ final class PageTests: XCTestCase {

await store.send(.paginateChapter(forward: false))

await clock.advance(by: .seconds(10))

await store.receive(.clear) {
$0.verses = nil
}
Expand All @@ -163,7 +228,14 @@ final class PageTests: XCTestCase {
await store.receive(.open(.leviticus, .mock, .mock, focused: nil)) {
$0.book = books.last!
$0.chapter = .mock
$0.verses = .mock
$0.verses = nil
}

await clock.advance(by: .seconds(10))

await store.receive(.add(.mock)) {
$0.verses = [.mock]
}

}
}
72 changes: 45 additions & 27 deletions BibleCore/Tests/ReaderCoreTests/ReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,68 +24,80 @@ final class ReaderTests: XCTestCase {
$0.isDirectoryOpen = true
}

await store.send(.directory(.task))
await store.send(.menuDirectory(.task))


await store.receive(.directory(.load(.success(.mock)))) {
$0.directory.sections = IdentifiedArray(uniqueElements: [Book].mock.map(Section.State.init(book:)))
await store.receive(.menuDirectory(.load(.success(.mock)))) {
$0.menuDirectory.sections = IdentifiedArray(uniqueElements: [Book].mock.map(Section.State.init(book:)))
}
}

@MainActor func testNextChapter() async {
let clock = TestClock()

let store = TestStore(
initialState: Reader.State(isDirectoryOpen: false),
reducer: {
Reader()
}
)
initialState: Reader.State(isDirectoryOpen: false)
) {
Reader()
._printChanges()
} withDependencies: {
$0.continuousClock = clock
}

// Open directory
// Open menuDirectory
await store.send(.openDirectory) {
$0.isDirectoryOpen = true
}

// Load directory
await store.send(.directory(.task))
// Load menuDirectory
await store.send(.menuDirectory(.task))

// Receive books
await store.receive(.directory(.load(.success(.mock)))) {
$0.directory.sections = IdentifiedArray(uniqueElements: [Book].mock.map(Section.State.init(book:)))
await store.receive(.menuDirectory(.load(.success(.mock)))) {
$0.menuDirectory.sections = IdentifiedArray(uniqueElements: [Book].mock.map(Section.State.init(book:)))
}

// Expand book
await store.send(.directory(.book(id: Book.genesis.id, action: .toggle))) {
$0.directory.sections[id: Book.genesis.id]?.isExpanded = true
$0.directory.focused = Book.genesis.id
await store.send(.menuDirectory(.book(id: Book.genesis.id, action: .toggle))) {
$0.menuDirectory.sections[id: Book.genesis.id]?.isExpanded = true
$0.menuDirectory.focused = Book.genesis.id
}

// Load chapters from book
await store.receive(.directory(.book(id: Book.genesis.id, action: .load(.success(.mock))))) {
$0.directory.sections[id: Book.genesis.id]?.chapters = .mock
await store.receive(.menuDirectory(.book(id: Book.genesis.id, action: .load(.success(.mock))))) {
$0.menuDirectory.sections[id: Book.genesis.id]?.chapters = .mock
}

// Select chapter
await store.send(.directory(.book(id: Book.genesis.id, action: .openChapter(.mock)))) {
$0.directory.sections[id: Book.genesis.id]?.chapter = .mock
await store.send(.menuDirectory(.book(id: Book.genesis.id, action: .openChapter(.mock)))) {
$0.menuDirectory.sections[id: Book.genesis.id]?.chapter = .mock
}

// Load verses from chapter
await store.receive(.directory(.book(id: Book.genesis.id, action: .loadChapter(.success(.mock))))) {
$0.directory.sections[id: Book.genesis.id]?.verses = .mock
// Load verses from menuDirectory
await store.receive(.menuDirectory(.book(id: Book.genesis.id, action: .loadChapter(.success(.mock))))) {
$0.menuDirectory.sections[id: Book.genesis.id]?.verses = .mock
}

// Select
await store.send(.directory(.book(id: Book.genesis.id, action: .select(.genesis, .mock, .mock, .mock)))) {
await store.send(.menuDirectory(.book(id: Book.genesis.id, action: .select(.genesis, .mock, .mock, nil)))) {
$0.isDirectoryOpen = false
}

// Open page
await store.receive(.page(.open(.genesis, .mock, .mock, focused: .mock))) {
$0.page = .init(book: .genesis, chapter: .mock, verses: .mock, verse: .mock)
await store.receive(.page(.open(.genesis, .mock, [.mock], focused: nil))) {
$0.page = .init(book: .genesis, chapter: .mock, verses: nil, verse: nil)
}

await clock.advance(by: .seconds(10))

await store.receive(.page(.add(.mock))) {
$0.page.verses = [.mock]
}

await store.send(.page(.paginateChapter(forward: true)))

await clock.advance(by: .seconds(10))

await store.receive(.page(.clear)) {
$0.page.verses = nil
}
Expand All @@ -95,8 +107,14 @@ final class ReaderTests: XCTestCase {
await store.receive(.page(.open(.exodus, .mock, .mock, focused: nil))) {
$0.page.book = .exodus
$0.page.chapter = .mock
$0.page.verses = .mock
$0.page.verses = nil
$0.page.verse = nil
}

await clock.advance(by: .seconds(10))

await store.receive(.page(.add(.mock))) {
$0.page.verses = [.mock]
}
}
}

0 comments on commit 7164b1e

Please sign in to comment.