Skip to content

Commit

Permalink
Pass IMarkdownPreview into the viewmodel instead of building it there
Browse files Browse the repository at this point in the history
  • Loading branch information
jgonz120 committed Nov 20, 2024
1 parent 9949b7f commit 69523d1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Markdown.Platform;
using NuGet.VisualStudio;
using NuGet.VisualStudio.Internal.Contracts;
using NuGet.VisualStudio.Telemetry;
Expand Down Expand Up @@ -38,8 +39,9 @@ public async Task InitializeAsync(DetailControlModel detailControlModel, INuGetP
{
var nuGetFeatureFlagService = await ServiceLocator.GetComponentModelServiceAsync<INuGetFeatureFlagService>();
_readmeTabEnabled = await nuGetFeatureFlagService.IsFeatureEnabledAsync(NuGetFeatureFlagConstants.RenderReadmeInPMUI);

ReadmePreviewViewModel = new ReadmePreviewViewModel(nugetPackageFileService, currentFilter, _readmeTabEnabled);
#pragma warning disable CS0618 // Type or member is obsolete
ReadmePreviewViewModel = new ReadmePreviewViewModel(nugetPackageFileService, currentFilter, new PreviewBuilder().Build(), _readmeTabEnabled);
#pragma warning restore CS0618 // Type or member is obsolete
DetailControlModel = detailControlModel;

if (_readmeTabEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public sealed class ReadmePreviewViewModel : TitledPageViewModelBase, IDisposabl
private bool _canRenderLocalReadme;
private bool _disposed = false;

public ReadmePreviewViewModel(INuGetPackageFileService packageFileService, ItemFilter itemFilter, bool isReadmeFeatureEnabled)
#pragma warning disable CS0618 // Type or member is obsolete
public ReadmePreviewViewModel(INuGetPackageFileService packageFileService, ItemFilter itemFilter, IMarkdownPreview markdownPreview, bool isReadmeFeatureEnabled)
#pragma warning restore CS0618 // Type or member is obsolete
{
_nugetPackageFileService = packageFileService ?? throw new ArgumentNullException(nameof(packageFileService));
_canRenderLocalReadme = CanRenderLocalReadme(itemFilter);
Expand All @@ -30,9 +32,7 @@ public ReadmePreviewViewModel(INuGetPackageFileService packageFileService, ItemF
_packageMetadata = null;
Title = Resources.Label_Readme_Tab;
IsVisible = isReadmeFeatureEnabled;
#pragma warning disable CS0618 // Type or member is obsolete
MarkdownPreview = new PreviewBuilder().Build();
#pragma warning restore CS0618 // Type or member is obsolete
MarkdownPreview = markdownPreview;
}

public bool ErrorLoadingReadme
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void Constructor_WithNullPackageFileService_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() =>
{
new ReadmePreviewViewModel(null, ItemFilter.All, true);
new ReadmePreviewViewModel(null, ItemFilter.All, null, true);
});
}

Expand All @@ -33,7 +33,7 @@ public void Constructor_Defaults()
var mockFileService = new Mock<INuGetPackageFileService>();

//Act
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.All, true);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.All, null, true);

//Assert
Assert.False(target.ErrorLoadingReadme);
Expand All @@ -48,7 +48,7 @@ public async Task SetPackageMetadataAsync_WithoutReadmeUrl_NoReadmeReturned(stri
{
//Arrange
var mockFileService = new Mock<INuGetPackageFileService>();
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.Installed, true);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.Installed, null, true);
var package = new DetailedPackageMetadata();
package.ReadmeFileUrl = readmeUrl;

Expand All @@ -68,7 +68,7 @@ public async Task SetCurrentFilter_ItemFilterAll_NoLocalReadmeReturned()
using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(readmeContents));
var mockFileService = new Mock<INuGetPackageFileService>();
mockFileService.Setup(x => x.GetReadmeAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>())).ReturnsAsync(stream);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.Installed, true);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.Installed, null, true);
var package = new DetailedPackageMetadata();
package.ReadmeFileUrl = "C://path/to/readme.md";
await target.SetPackageMetadataAsync(package, CancellationToken.None);
Expand All @@ -92,7 +92,7 @@ public async Task SetCurrentFilter_ItemFilterRenderingLocalReadme_LocalReadmeRet
using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(readmeContents));
var mockFileService = new Mock<INuGetPackageFileService>();
mockFileService.Setup(x => x.GetReadmeAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>())).ReturnsAsync(stream);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.All, true);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.All, null, true);
var package = new DetailedPackageMetadata();
package.ReadmeFileUrl = "C://path/to/readme.md";
await target.SetPackageMetadataAsync(package, CancellationToken.None);
Expand All @@ -113,7 +113,7 @@ public async Task SetPackageMetadataAsync_WithLocalReadmeUrl_RenderLocalReadmeTr
using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(readmeContents));
var mockFileService = new Mock<INuGetPackageFileService>();
mockFileService.Setup(x => x.GetReadmeAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>())).ReturnsAsync(stream);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.Installed, true);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.Installed, null, true);
var package = new DetailedPackageMetadata();
package.ReadmeFileUrl = "C://path/to/readme.md";

Expand All @@ -133,7 +133,7 @@ public async Task SetPackageMetadataAsync_WithLocalReadmeUrl_RenderLocalReadmeFa
using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(readmeContents));
var mockFileService = new Mock<INuGetPackageFileService>();
mockFileService.Setup(x => x.GetReadmeAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>())).ReturnsAsync(stream);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.All, true);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.All, null, true);
var package = new DetailedPackageMetadata();
package.ReadmeFileUrl = "C://path/to/readme.md";

Expand All @@ -151,7 +151,7 @@ public async Task SetPackageMetadataAsync_WithLocalReadmeUrl_FileNotFound_NoRead
//Arrange
var mockFileService = new Mock<INuGetPackageFileService>();
mockFileService.Setup(x => x.GetReadmeAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>())).Returns(null);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.Installed, true);
var target = new ReadmePreviewViewModel(mockFileService.Object, ItemFilter.Installed, null, true);
var package = new DetailedPackageMetadata();
package.ReadmeFileUrl = "C://path/to/readme.md";

Expand Down

0 comments on commit 69523d1

Please sign in to comment.