|
| 1 | +// Copyright 2019-2024 Spotify AB. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | +@testable import MobiusCore |
| 17 | +import Nimble |
| 18 | +import Quick |
| 19 | + |
| 20 | +class InitializationTests: QuickSpec { |
| 21 | + // swiftlint:disable:next function_body_length |
| 22 | + override func spec() { |
| 23 | + describe("Initialization") { |
| 24 | + var builder: Mobius.Builder<String, String, String>! |
| 25 | + var updateFunction: Update<String, String, String>! |
| 26 | + var loop: MobiusLoop<String, String, String>! |
| 27 | + var receivedModels: [String]! |
| 28 | + var modelObserver: Consumer<String>! |
| 29 | + var effectHandler: RecordingTestConnectable! |
| 30 | + var eventSource: TestEventSource<String>! |
| 31 | + var connectableEventSource: TestConnectableEventSource<String, String>! |
| 32 | + |
| 33 | + beforeEach { |
| 34 | + receivedModels = [] |
| 35 | + |
| 36 | + modelObserver = { receivedModels.append($0) } |
| 37 | + |
| 38 | + updateFunction = Update<String, String, String> { _, event in |
| 39 | + if event == "event that triggers effect" { |
| 40 | + return Next.next(event, effects: [event]) |
| 41 | + } else { |
| 42 | + return Next.next(event) |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + effectHandler = RecordingTestConnectable() |
| 47 | + eventSource = TestEventSource() |
| 48 | + connectableEventSource = .init() |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + it("should process init") { |
| 53 | + builder = Mobius.loop(update: updateFunction, effectHandler: effectHandler) |
| 54 | + |
| 55 | + loop = builder.start(from: "the first model") |
| 56 | + |
| 57 | + loop.addObserver(modelObserver) |
| 58 | + |
| 59 | + expect(receivedModels).to(equal(["the first model"])) |
| 60 | + } |
| 61 | + |
| 62 | + it("should process init and then events") { |
| 63 | + builder = Mobius.loop(update: updateFunction, effectHandler: effectHandler) |
| 64 | + |
| 65 | + loop = builder.start(from: "the first model") |
| 66 | + |
| 67 | + loop.addObserver(modelObserver) |
| 68 | + loop.dispatchEvent("event that triggers effect") |
| 69 | + |
| 70 | + expect(receivedModels).to(equal(["the first model", "event that triggers effect"])) |
| 71 | + } |
| 72 | + |
| 73 | + it("should process init before events from connectable event source") { |
| 74 | + builder = Mobius.loop(update: updateFunction, effectHandler: effectHandler) |
| 75 | + .withEventSource(connectableEventSource) |
| 76 | + |
| 77 | + connectableEventSource.dispatch("ignored event from connectable event source") |
| 78 | + loop = builder.start(from: "the first model") |
| 79 | + loop.addObserver(modelObserver) |
| 80 | + |
| 81 | + connectableEventSource.dispatch("second event from connectable event source") |
| 82 | + |
| 83 | + // The first event was sent before the loop started so it should be ignored. The second should go through |
| 84 | + expect(receivedModels).to(equal(["the first model", "second event from connectable event source"])) |
| 85 | + } |
| 86 | + |
| 87 | + it("should process init before events from event source") { |
| 88 | + builder = Mobius.loop(update: updateFunction, effectHandler: effectHandler) |
| 89 | + .withEventSource(eventSource) |
| 90 | + |
| 91 | + eventSource.dispatch("ignored event from event source") |
| 92 | + loop = builder.start(from: "the first model") |
| 93 | + loop.addObserver(modelObserver) |
| 94 | + |
| 95 | + eventSource.dispatch("second event from event source") |
| 96 | + |
| 97 | + // The first event was sent before the loop started so it should be ignored. The second should go through |
| 98 | + expect(receivedModels).to(equal(["the first model", "second event from event source"])) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Emits values before returning the connection |
| 105 | +class EagerTestConnectable: Connectable { |
| 106 | + private(set) var consumer: Consumer<String>? |
| 107 | + private(set) var recorder: Recorder<String> |
| 108 | + private(set) var eagerValue: String |
| 109 | + |
| 110 | + private(set) var connection: Connection<String>! |
| 111 | + |
| 112 | + init(eagerValue: String) { |
| 113 | + self.recorder = Recorder() |
| 114 | + self.eagerValue = eagerValue |
| 115 | + } |
| 116 | + |
| 117 | + func connect(_ consumer: @escaping (String) -> Void) -> Connection<String> { |
| 118 | + self.consumer = consumer |
| 119 | + connection = Connection(acceptClosure: accept, disposeClosure: dispose) // Will retain self |
| 120 | + connection.accept(eagerValue) // emit before returning |
| 121 | + return connection |
| 122 | + } |
| 123 | + |
| 124 | + func dispatch(_ string: String) { |
| 125 | + consumer?(string) |
| 126 | + } |
| 127 | + |
| 128 | + func accept(_ value: String) { |
| 129 | + recorder.append(value) |
| 130 | + } |
| 131 | + |
| 132 | + func dispose() { |
| 133 | + } |
| 134 | +} |
0 commit comments