Skip to content

Commit

Permalink
Fixed search vm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IngvarX committed Oct 15, 2020
1 parent 80bf72a commit 40e067d
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions tests/Camelot.ViewModels.Tests/SearchViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ namespace Camelot.ViewModels.Tests
public class SearchViewModelTests
{
private const string SearchText = "text";
private const string Error = "error";
private const string ResourceName = "ResourceName";

private readonly IResourceProvider _resourceProvider;

public SearchViewModelTests()
{
var resourceProviderMock = new Mock<IResourceProvider>();
resourceProviderMock
.Setup(m => m.GetResourceByName(ResourceName))
.Returns(Error);

_resourceProvider = resourceProviderMock.Object;
}

[Fact]
public void TestDefaults()
{
var regexServiceMock = new Mock<IRegexService>();
var resourceProviderMock = new Mock<IResourceProvider>();
var configuration = new SearchViewModelConfiguration();
var viewModel = new SearchViewModel(regexServiceMock.Object, resourceProviderMock.Object, configuration);
var configuration = GetConfiguration();
var viewModel = new SearchViewModel(regexServiceMock.Object, _resourceProvider, configuration);

Assert.False(viewModel.IsSearchEnabled);
Assert.False(viewModel.IsRegexSearchEnabled);
Expand All @@ -31,12 +44,11 @@ public void TestDefaults()
[Fact]
public void TestToggle()
{
var configuration = new SearchViewModelConfiguration();
var configuration = GetConfiguration();

var regexServiceMock = new Mock<IRegexService>();
var resourceProviderMock = new Mock<IResourceProvider>();

var viewModel = new SearchViewModel(regexServiceMock.Object, resourceProviderMock.Object, configuration);
var viewModel = new SearchViewModel(regexServiceMock.Object, _resourceProvider, configuration);
Assert.False(viewModel.IsSearchEnabled);

viewModel.SearchText = SearchText;
Expand Down Expand Up @@ -68,9 +80,8 @@ public void TestSpecification(bool isSearchEnabled, bool isRegexSearchEnabled,
regexServiceMock
.Setup(m => m.ValidateRegex(It.IsAny<string>()))
.Returns(isRegexValid);
var resourceProviderMock = new Mock<IResourceProvider>();
var configuration = new SearchViewModelConfiguration();
var viewModel = new SearchViewModel(regexServiceMock.Object, resourceProviderMock.Object, configuration)
var configuration = GetConfiguration();
var viewModel = new SearchViewModel(regexServiceMock.Object, _resourceProvider, configuration)
{
IsSearchEnabled = isSearchEnabled,
IsRegexSearchEnabled = isRegexSearchEnabled
Expand All @@ -87,12 +98,8 @@ public async Task TestTextChangedChanged()
var taskCompletionSource = new TaskCompletionSource<bool>();

var regexServiceMock = new Mock<IRegexService>();
var resourceProviderMock = new Mock<IResourceProvider>();
var configuration = new SearchViewModelConfiguration
{
TimeoutMs = 10
};
var viewModel = new SearchViewModel(regexServiceMock.Object, resourceProviderMock.Object, configuration);
var configuration = GetConfiguration();
var viewModel = new SearchViewModel(regexServiceMock.Object, _resourceProvider, configuration);
viewModel.SearchSettingsChanged += (sender, args) => taskCompletionSource.SetResult(true);

viewModel.SearchText = "test";
Expand All @@ -107,12 +114,8 @@ public async Task TestSearchCaseChanged()
var taskCompletionSource = new TaskCompletionSource<bool>();

var regexServiceMock = new Mock<IRegexService>();
var resourceProviderMock = new Mock<IResourceProvider>();
var configuration = new SearchViewModelConfiguration
{
TimeoutMs = 10
};
var viewModel = new SearchViewModel(regexServiceMock.Object, resourceProviderMock.Object, configuration);
var configuration = GetConfiguration();
var viewModel = new SearchViewModel(regexServiceMock.Object, _resourceProvider, configuration);
viewModel.SearchSettingsChanged += (sender, args) => taskCompletionSource.SetResult(true);

viewModel.IsSearchCaseSensitive = true;
Expand All @@ -132,12 +135,8 @@ public async Task TestRegexChanged(bool isRegexValid)
regexServiceMock
.Setup(m => m.ValidateRegex(SearchText))
.Returns(isRegexValid);
var resourceProviderMock = new Mock<IResourceProvider>();
var configuration = new SearchViewModelConfiguration
{
TimeoutMs = 10
};
var viewModel = new SearchViewModel(regexServiceMock.Object, resourceProviderMock.Object, configuration);
var configuration = GetConfiguration();
var viewModel = new SearchViewModel(regexServiceMock.Object, _resourceProvider, configuration);
viewModel.SearchSettingsChanged += (sender, args) => taskCompletionSource.SetResult(true);

viewModel.SearchText = SearchText;
Expand All @@ -146,5 +145,12 @@ public async Task TestRegexChanged(bool isRegexValid)
var task = await Task.WhenAny(Task.Delay(1000), taskCompletionSource.Task);
Assert.Equal(isRegexValid, taskCompletionSource.Task == task);
}

private static SearchViewModelConfiguration GetConfiguration() =>
new SearchViewModelConfiguration
{
InvalidRegexResourceName = ResourceName,
TimeoutMs = 10
};
}
}

0 comments on commit 40e067d

Please sign in to comment.