Skip to content

Instantly share code, notes, and snippets.

@ncreated
Created August 11, 2017 21:24
Show Gist options
  • Save ncreated/5e0ab17bcad536cf64b1a1a42d04e6ce to your computer and use it in GitHub Desktop.
Save ncreated/5e0ab17bcad536cf64b1a1a42d04e6ce to your computer and use it in GitHub Desktop.
Medium blogpost snippet
func test_givenStorageThatSucceedsOnSave_whenChangingToggleValue_itEmitsUpdatedValue() {
let mockStorage = MockToggleStorage(readValue: true, saveShouldFail: false)
let simulatedValueSubject = PublishSubject<Bool>() // simulated user input
let toggle = Toggle(storage: mockStorage)
let initialValueExpectation = self.expectation(description: "will emit initial value")
let updatedValueExpectation = self.expectation(description: "will emit 2 updated values")
updatedValueExpectation.expectedFulfillmentCount = 2
let (value, _) = toggle.manage(change: simulatedValueSubject.asObservable())
var recordedValues: [ToggleValue] = []
_ = value
.drive(onNext: { value in
recordedValues.append(value)
if value.isInitial { initialValueExpectation.fulfill() }
if value.isUpdated { updatedValueExpectation.fulfill() }
})
wait(for: [initialValueExpectation], timeout: 1) // wait for receiving initial value
simulatedValueSubject.onNext(false) // then simulate user switching toggle off
simulatedValueSubject.onNext(true) // then simulate user switching toggle on
wait(for: [updatedValueExpectation], timeout: 1)
XCTAssertEqual(recordedValues, [ToggleValue.initial(true), ToggleValue.updated(false), ToggleValue.updated(true)])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment