Skip to content

Commit

Permalink
Remove the model property in favor of readonly model.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Usbergo committed Mar 20, 2021
1 parent 362a799 commit e07dc7b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
1 change: 0 additions & 1 deletion Sources/Store/store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ open class Store<M>: MutableStore, ObservableObject, Identifiable {
// See `AnyStore`.
public var middleware: [Middleware] = []
public private(set) var modelStorage: ModelStorageBase<M>
public var model: M { modelStorage.model }

// Internal
public let parent: AnyStore?
Expand Down
54 changes: 27 additions & 27 deletions Tests/StoreTests/ChildStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ final class ChildStoreTests: XCTestCase {
rootStore.noteStore.register(middleware: LoggerMiddleware())
rootStore.listStore.register(middleware: LoggerMiddleware())

XCTAssertFalse(rootStore.model.todo.done)
XCTAssertFalse(rootStore.todoStore.model.done)
XCTAssertFalse(rootStore.readOnlyModel.todo.done)
XCTAssertFalse(rootStore.todoStore.readOnlyModel.done)
rootStore.todoStore.run(action: Root.Todo.Action_MarkAsDone(), mode: .sync)
XCTAssertTrue(rootStore.todoStore.model.done)
XCTAssertTrue(rootStore.model.todo.done)
XCTAssertTrue(rootStore.todoStore.readOnlyModel.done)
XCTAssertTrue(rootStore.readOnlyModel.todo.done)
}

func testChildStoreChangesTriggersRootObserver() {
Expand All @@ -109,13 +109,13 @@ final class ChildStoreTests: XCTestCase {
rootStore.listStore.register(middleware: LoggerMiddleware())

sink = rootStore.objectWillChange.sink {
XCTAssertTrue(rootStore.model.todo.done)
XCTAssertTrue(rootStore.todoStore.model.done)
XCTAssertTrue(rootStore.readOnlyModel.todo.done)
XCTAssertTrue(rootStore.todoStore.readOnlyModel.done)
observerExpectation.fulfill()
}
rootStore.todoStore.run(action: Root.Todo.Action_MarkAsDone(), mode: .sync)
XCTAssertTrue(rootStore.todoStore.model.done)
XCTAssertTrue(rootStore.model.todo.done)
XCTAssertTrue(rootStore.todoStore.readOnlyModel.done)
XCTAssertTrue(rootStore.readOnlyModel.todo.done)
waitForExpectations(timeout: 1)
}

Expand All @@ -127,32 +127,32 @@ final class ChildStoreTests: XCTestCase {
rootStore.register(middleware: LoggerMiddleware())
rootStore.listStore.register(middleware: LoggerMiddleware())

XCTAssertTrue(rootStore.listStore.model.count == 1)
XCTAssertTrue(rootStore.listStore.model[0].name == "New")
XCTAssertTrue(rootStore.listStore.model[0].description == "New")
XCTAssertTrue(rootStore.listStore.model[0].done == false)
XCTAssertTrue(rootStore.model.list.count == 1)
XCTAssertTrue(rootStore.model.list[0].name == "New")
XCTAssertTrue(rootStore.model.list[0].description == "New")
XCTAssertTrue(rootStore.model.list[0].done == false)
XCTAssertTrue(rootStore.listStore.readOnlyModel.count == 1)
XCTAssertTrue(rootStore.listStore.readOnlyModel[0].name == "New")
XCTAssertTrue(rootStore.listStore.readOnlyModel[0].description == "New")
XCTAssertTrue(rootStore.listStore.readOnlyModel[0].done == false)
XCTAssertTrue(rootStore.readOnlyModel.list.count == 1)
XCTAssertTrue(rootStore.readOnlyModel.list[0].name == "New")
XCTAssertTrue(rootStore.readOnlyModel.list[0].description == "New")
XCTAssertTrue(rootStore.readOnlyModel.list[0].done == false)

let listStore = rootStore.listStore

let todoStore = listStore.makeChildStore(keyPath: \.[0])
todoStore.register(middleware: LoggerMiddleware())

todoStore.run(action: Root.Todo.Action_MarkAsDone(), mode: .sync)
XCTAssertTrue(todoStore.model.name == "New")
XCTAssertTrue(todoStore.model.description == "New")
XCTAssertTrue(todoStore.model.done == true)
XCTAssertTrue(rootStore.listStore.model.count == 1)
XCTAssertTrue(rootStore.listStore.model[0].name == "New")
XCTAssertTrue(rootStore.listStore.model[0].description == "New")
XCTAssertTrue(rootStore.listStore.model[0].done == true)
XCTAssertTrue(rootStore.model.list.count == 1)
XCTAssertTrue(rootStore.model.list[0].name == "New")
XCTAssertTrue(rootStore.model.list[0].description == "New")
XCTAssertTrue(rootStore.model.list[0].done == true)
XCTAssertTrue(todoStore.readOnlyModel.name == "New")
XCTAssertTrue(todoStore.readOnlyModel.description == "New")
XCTAssertTrue(todoStore.readOnlyModel.done == true)
XCTAssertTrue(rootStore.listStore.readOnlyModel.count == 1)
XCTAssertTrue(rootStore.listStore.readOnlyModel[0].name == "New")
XCTAssertTrue(rootStore.listStore.readOnlyModel[0].description == "New")
XCTAssertTrue(rootStore.listStore.readOnlyModel[0].done == true)
XCTAssertTrue(rootStore.readOnlyModel.list.count == 1)
XCTAssertTrue(rootStore.readOnlyModel.list[0].name == "New")
XCTAssertTrue(rootStore.readOnlyModel.list[0].description == "New")
XCTAssertTrue(rootStore.readOnlyModel.list[0].done == true)
}
}

22 changes: 11 additions & 11 deletions Tests/StoreTests/StoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class StoreTests: XCTestCase {
store.register(middleware: LoggerMiddleware())
store.run(action: TestAction.increase(amount: 42)) { error in
XCTAssert(error == nil)
XCTAssert(store.model.count == 42)
XCTAssert(store.readOnlyModel.count == 42)
transactionExpectation.fulfill()
}
waitForExpectations(timeout: 1)
Expand All @@ -28,7 +28,7 @@ final class StoreTests: XCTestCase {
TestAction.increase(amount: 1),
TestAction.increase(amount: 1),
]) { context in
XCTAssert(store.model.count == 3)
XCTAssert(store.readOnlyModel.count == 3)
transactionExpectation.fulfill()
}
waitForExpectations(timeout: 10)
Expand All @@ -38,18 +38,18 @@ final class StoreTests: XCTestCase {
let store = CodableStore(model: TestModel(), diffing: .sync)
store.register(middleware: LoggerMiddleware())
store.run(action: TestAction.updateLabel(newLabel: "Bar"), mode: .sync)
XCTAssert(store.model.label == "Bar")
XCTAssert(store.model.nested.label == "Bar")
XCTAssert(store.readOnlyModel.label == "Bar")
XCTAssert(store.readOnlyModel.nested.label == "Bar")
store.run(action: TestAction.updateLabel(newLabel: "Foo"), mode: .sync)
XCTAssert(store.model.label == "Foo")
XCTAssert(store.model.nested.label == "Foo")
XCTAssert(store.readOnlyModel.label == "Foo")
XCTAssert(store.readOnlyModel.nested.label == "Foo")
}

func testAccessNestedKeyPathInArray() {
let store = CodableStore(model: TestModel(), diffing: .sync)
store.register(middleware: LoggerMiddleware())
store.run(action: TestAction.setArray(index: 1, value: "Foo"), mode: .sync)
XCTAssert(store.model.array[1].label == "Foo")
XCTAssert(store.readOnlyModel.array[1].label == "Foo")
}

func testDiffResult() {
Expand All @@ -74,7 +74,7 @@ final class StoreTests: XCTestCase {
let transaction = store.transaction(action: TestAction.increase(amount: 1))
sink = transaction.$state.sink { state in
XCTAssert(state != .completed)
XCTAssert(store.model.count == 0)
XCTAssert(store.readOnlyModel.count == 0)
if state == .canceled {
transactionExpectation.fulfill()
}
Expand All @@ -93,7 +93,7 @@ final class StoreTests: XCTestCase {
sink = store.futureOf(action: action1)
.replaceError(with: ())
.sink {
XCTAssert(store.model.count == 5)
XCTAssert(store.readOnlyModel.count == 5)
transactionExpectation.fulfill()
}
waitForExpectations(timeout: 1)
Expand All @@ -102,13 +102,13 @@ final class StoreTests: XCTestCase {
func testAccessToBindingProxy() {
let store = CodableStore(model: TestModel(), diffing: .sync)
store.binding.count = 3
XCTAssert(store.model.count == 3)
XCTAssert(store.readOnlyModel.count == 3)
}

func testMutateSynchronous() {
let store = CodableStore(model: TestModel(), diffing: .sync)
store.mutate { $0.count = 3 }
XCTAssert(store.model.count == 3)
XCTAssert(store.readOnlyModel.count == 3)
}

static var allTests = [
Expand Down

0 comments on commit e07dc7b

Please sign in to comment.