Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SearchViewModel : ViewModelBase
- {
- private string _searchTerm;
- private ReactiveList<string> _searchResults = new ReactiveList<string>();
- public SearchViewModel()
- {
- ExecuteSearch = ReactiveCommand.CreateAsyncTask((o, ct) => DoSearchAsync(ct));
- this.ObservableForProperty(n => n.SearchTerm, (term) =>
- {
- return !String.IsNullOrWhiteSpace(term) && term.Length > 3;
- }).DistinctUntilChanged().Throttle(TimeSpan.FromMilliseconds(300),RxApp.MainThreadScheduler).Subscribe(
- b => ExecuteSearch.Execute(b));
- ExecuteSearch.Subscribe(list =>
- {
- _searchResults.Clear();
- _searchResults = list;
- NotifyOfPropertyChange(() => SearchResults);
- });
- }
- public ReactiveList<string> SearchResults
- {
- get { return _searchResults; }
- }
- private async Task<ReactiveList<string>> DoSearchAsync(CancellationToken ct)
- {
- //throw new NotImplementedException();
- return new ReactiveList<string> { "a", "b", "c" };
- }
- public IReactiveCommand<ReactiveList<string>> ExecuteSearch { get; protected set; }
- public string SearchTerm
- {
- get { return _searchTerm; }
- set { this.RaiseAndSetIfChanged(ref _searchTerm, value); }
- }
- }
- ///////////////TEST
- public class SearchViewModelTests :TestBase
- {
- //private SearchViewModel _viewModel;
- //[TestInitialize]
- //public void Setup()
- //{
- // _viewModel = new SearchViewModel();
- //}
- [TestMethod]
- public void SearchIs()
- {
- (new TestScheduler()).With(s =>
- {
- var _viewModel = getViewModel();
- _viewModel.SearchTerm = "q123wer";
- Assert.AreEqual(0, _viewModel.SearchResults.Count);
- s.AdvanceByMs(310);
- Assert.AreEqual(3, _viewModel.SearchResults.Count);
- });
- }
- private SearchViewModel getViewModel()
- {
- return MinicabsterKernel.Instance.Get<SearchViewModel>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement