diff --git a/PSHTML/Examples/Example19/AssetContent.ps1 b/PSHTML/Examples/Example19/AssetContent.ps1 new file mode 100644 index 00000000..240875d8 --- /dev/null +++ b/PSHTML/Examples/Example19/AssetContent.ps1 @@ -0,0 +1,29 @@ +# This example highlight how to write assets directly as content. +# This is useful when you want to write the asset content directly in the HTML document so that they can be shared with all the dynamic content. +import-module pshtml -Force + +$html = html { + head { + title "Asset Content" + #Use -AsContent to write the asset content directly in the HTML document + Write-PSHTMLAsset -AsContent + } + body { + h1 "Asset Content" + p "This is the asset content" + $PieCanvasID = "piecanvas" + canvas -Height 400px -Width 400px -Id $PieCanvasID { + + } + Script -content { + + $Data1 = @(34,7,11,19) + $Labels = @("Closed","Unresolved","Pending","Open") + $colors = @("Lightgreen","red","Blue","Yellow") + $dsp1 = New-PSHTMLChartPieDataSet -Data $data1 -label "March" -BackgroundColor $colors + New-PSHTMLChart -type pie -DataSet $dsp1 -title "Pie Chart v2" -Labels $Labels -CanvasID $PieCanvasID + } + } +} + +Out-PSHTMLDocument -HTMLDocument $html -OutPath "C:\Users\Stephane\Code\PSHTML\PSHTML\Examples\Example19\AssetContent.html" -Show \ No newline at end of file diff --git a/PSHTML/PSHTML.psd1 b/PSHTML/PSHTML.psd1 index cbc1b916..8ddb3a9e 100644 --- a/PSHTML/PSHTML.psd1 +++ b/PSHTML/PSHTML.psd1 @@ -12,7 +12,7 @@ RootModule = 'PSHTML.psm1' # Version number of this module. -ModuleVersion = '0.8.2' +ModuleVersion = '0.9.0' # Supported PSEditions # CompatiblePSEditions = @() @@ -128,7 +128,7 @@ PrivateData = @{ ReleaseNotes = 'https://github.com/Stephanevg/PSHTML/blob/master/Change_Log.md' # Prerelease string of this module - # Prerelease = '' + Prerelease = 'alpha' # Flag to indicate whether the module requires explicit user acceptance for install/update/save # RequireLicenseAcceptance = $false diff --git a/PSHTML/PSHTML.psm1 b/PSHTML/PSHTML.psm1 index c3c88177..f4af718c 100644 --- a/PSHTML/PSHTML.psm1 +++ b/PSHTML/PSHTML.psm1 @@ -12,9 +12,20 @@ Enum AssetType { cdn } +Enum LocationType { + Module + Project +} + Class ConfigurationDocument { [System.IO.FileInfo]$Path = "$PSScriptRoot/pshtml.configuration.json" + [system.Io.FileInfo]$ModuleFolder = $PSScriptRoot + [System.IO.DirectoryInfo]$ProjectFolderPath + [System.IO.DirectoryInfo]$IncludesProjectFolderPath + [System.IO.DirectoryInfo]$AssetsProjectFolderPath + [System.IO.DirectoryInfo]$IncludesModuleFolderPath + [System.IO.DirectoryInfo]$AssetsModuleFolderPath [Setting[]]$Settings [Asset[]]$Assets [Include[]]$Includes @@ -31,69 +42,128 @@ Class ConfigurationDocument { #Methods [void]Load(){ + #Reminder: Logger CANNOT be used during the load process.(This includes the LoadIncludes() and LoadAssets() methods) #Read data from json $this.Settings = [SettingFactory]::Parse($This.Path) + <# $EC = Get-Variable ExecutionContext -ValueOnly $ProjectRootFolder = $ec.SessionState.Path.CurrentLocation.Path - $ModuleFolder = $This.Path.Directory + #> - #Assets - $ModuleAssetsFolder = Join-Path $ModuleFolder -ChildPath "Assets" - $ProjectAssetsFolder = Join-Path $ProjectRootFolder -ChildPath "Assets" + $ScriptCaller = $Script:MyInvocation.PSCommandPath - $ModuleAssets = [AssetsFactory]::CreateAsset($ModuleAssetsFolder) - $ProjectAssets = [AssetsFactory]::CreateAsset($ProjectAssetsFolder) + if($null -ne $ScriptCaller){ + $ProjectRootFolder = Split-Path -Parent -Path $ScriptCaller + $this.ProjectFolderPath = $ProjectRootFolder + #Write-Verbose "[ConfiguratinDocument][Load()] Project folder found at $($this.ProjectFolderPath.FullName)" + } - $this.Assets += $ProjectAssets + $this.LoadIncludes() + $this.loadAssets() + } - foreach ($modass in $ModuleAssets){ - if($this.Assets.name -contains $modass.name){ - - $PotentialConflictingAsset = $this.Assets | ? {$_.Name -eq $modass.Name} - if($PotentialConflictingAsset.Type -eq $modass.type){ + [void]LoadIncludes(){ + #Reminder: Logger CANNOT be used during the load process.(This includes LoadAssets() and Load() methods) + $ModuleIncludes = $null + [System.IO.DirectoryInfo]$IncludeModuleFolder = Join-Path $this.Path.Directory -ChildPath "Includes" + if($IncludeModuleFolder.Exists){ + $This.IncludesModuleFolderPath = $IncludeModuleFolder + $ModuleIncludes = [IncludeFactory]::Create($this.IncludesModuleFolderPath) - #write-verbose "Identical asset found at $($modass.name). Keeping project asset." - Continue - }else{ - $This.Assets += $modass + foreach($ModInc in $ModuleIncludes){ + $ModInc.LocationType = [LocationType]::Module + #write-verbose "[ConfigurationOBject][LoadIncludes()] -> Module includes $($ModInc.Name)" + } + } + + + if($this.ProjectFolderPath.Exists){ + + [System.IO.DirectoryInfo]$IncludesFP = Join-Path -Path $this.ProjectFolderPath -ChildPath "Includes" + + if($IncludesFP.Exists){ + $this.SetIncludesProjectFolderPath($IncludesFP) + #write-verbose "[ConfigurationObject][LoadIncludes()] Project includes folder found at $($IncludesFP.FullName)" + $projectIncludes = [IncludeFactory]::Create($IncludesFP) + if($projectIncludes){ + foreach($ProjInc in $projectIncludes){ + $ProjInc.LocationType = [LocationType]::Project + #write-verbose "[ConfigurationOBject][LoadIncludes()] -> Project includes $($ProjInc.Name)" } - }else{ - $This.Assets += $modass + $this.Includes += $projectIncludes + } + } + } + + foreach ($modinc in $ModuleIncludes){ + if($this.Includes.name -contains $modinc.name){ + + $PotentialConflictingInclude = $this.Includes | ? {$_.Name -eq $modinc.Name} + if($PotentialConflictingInclude){ + + #write-verbose "Identical asset found $($modinc.name) at $($modinc.FolderPath.FullName). Keeping project asset." + Continue } + + Continue + }else{ + $This.Includes += $modinc } + } + - #Includes - #$IncludesFolder = Join-Path -Path $ExecutionContext.SessionState.Path.CurrentLocation.Path -ChildPath "Includes" #Join-Path $this.Path.Directory -ChildPath 'Includes' - $IncludesFolder = Join-Path -Path $ProjectRootFolder -ChildPath "Includes" - $this.Includes = [IncludeFactory]::Create($IncludesFolder) + } - $ModuleIncludesFolder = Join-Path $ModuleFolder -ChildPath "Includes" - $ProjectIncludesFolder = Join-Path $ProjectRootFolder -ChildPath "Assets" + [Void]LoadAssets(){ + #Reminder: Logger CANNOT be used during the load process.(This includes LoadIncludes() and Load() methods) + + if($this.ProjectFolderPath){ - $ModuleIncludes = [IncludeFactory]::Create($ModuleIncludesFolder) - $ProjectIncludes = [IncludeFactory]::Create($ProjectIncludesFolder) + $ProjectAssetsFolder = Join-Path $this.ProjectFolderPath -ChildPath "Assets" + $this.Assets = [AssetsFactory]::CreateAsset($ProjectAssetsFolder) - $this.Includes += $ProjectIncludes + foreach($ProjAss in $this.Assets){ + $ProjAss.LocationType = [LocationType]::Project + #write-verbose "[ConfigurationObject][LoadAssets()] -> Project asset $($ProjAss.Name)" + } + } + + + $ModuleAssetsFolder = Join-Path $this.Path.Directory -ChildPath "Assets" + + + $ModuleAssets = [AssetsFactory]::CreateAsset($ModuleAssetsFolder) + + if(-not $ModuleAssets){ + return + } + + foreach($ModAss in $ModuleAssets){ + $ModAss.LocationType = [LocationType]::Module + #write-verbose "[ConfigurationObject][LoadAssets()] -> Module asset $($ModAss.Name)" + } - foreach ($modinc in $ModuleIncludes){ - if($this.Includes.name -contains $modinc.name){ + foreach ($modass in $ModuleAssets){ + if($this.Assets.name -contains $modass.name){ - $PotentialConflictingInclude = $this.Includes | ? {$_.Name -eq $modinc.Name} - if($PotentialConflictingInclude.Type -eq $modinc.type){ + $PotentialConflictingAsset = $this.Assets | ? {$_.Name -eq $modass.Name} + if($PotentialConflictingAsset.Type -eq $modass.type){ - #write-verbose "Identical asset found at $($modinc.name). Keeping project asset." + #write-verbose "Identical asset found at $($modass.name). Keeping project asset." Continue + }else{ + $This.Assets += $modass } - - Continue }else{ - $This.Includes += $modinc + $This.Assets += $modass } } + #> } [void]Load([System.IO.FileInfo]$Path){ + #Reminder: Logger CANNOT be used during the load process.(This includes LoadAssets() LoadIncludes() and Load() methods) $this.Path = $Path $this.Load() } @@ -134,9 +204,6 @@ Class ConfigurationDocument { } - [void]hidden LoadLogSettings(){ - - } [String]GetDefaultLogFilePath(){ return $this.GetSetting("Log").GetLogfilePath() @@ -150,6 +217,29 @@ Class ConfigurationDocument { Return $this.Includes | ? {$_.Name -eq $Name} } + [void]SetIncludesProjectFolderPath([System.IO.DirectoryInfo]$IncludesProjectFolderPath){ + $this.IncludesProjectFolderPath = $IncludesProjectFolderPath + } + + [void]SetIncludesModuleFolderPath([System.IO.DirectoryInfo]$IncludesModuleFolderPath){ + $this.IncludesModuleFolderPath = $IncludesModuleFolderPath + } + + [void]SetAssetsProjectFolderPath([System.IO.DirectoryInfo]$AssetsProjectFolderPath){ + $this.AssetsProjectFolderPath = $AssetsProjectFolderPath + } + + [void]SetAssetsModuleFolderPath([System.IO.DirectoryInfo]$AssetsModuleFolderPath){ + $this.AssetsModuleFolderPath = $AssetsModuleFolderPath + } + + [bool]HasAssetsProjectFolder(){ + return $this.AssetsProjectFolderPath.Exists + } + + [bool] HasIncludesProjectFolder(){ + return $this.IncludesProjectFolderPath.Exists + } } Class Setting{ @@ -428,25 +518,31 @@ Class AssetsFactory{ } hidden Static [Asset[]] CreateAssets([System.IO.DirectoryInfo]$AssetsFolderPath) { + <# + Assets MUST be located in a folder named 'Assets/NAME_OF_ASSET' + Example: C:\WoopiDoopy\WebSiteReportTool\Assets\ContosoCommon + Contains a file named Woop.js which contains all the "woop's company" specific javascript code. + + PSHTML Will create the following asset: + Name : ContosoCommon + FolderPath : C:\WoopiDoopy\WebSiteReportTool\Assets\ContosoCommon + FilePath : C:\WoopiDoopy\WebSiteReportTool\Assets\ContosoCommon\Woop.js + RelativePath : Assets/ContosoCommon/Woop.js + Type : Script + LocationType : Project + + #> $Directories = Get-ChildItem $AssetsFolderPath -Directory $AllItems = @() - Foreach($Directory in $Directories){ - $Items = $Directory | Get-ChildItem -File | ? {$_.Extension -eq ".js" -or $_.Extension -eq ".css" -or $_.Extension -eq ".cdn"} #If performance becomes important. Change this to -Filter + Foreach ($Directory in $Directories) { + $Items = $Directory.FullName | Get-ChildItem -Recurse -File | ? { $_.Extension -eq ".js" -or $_.Extension -eq ".css" -or $_.Extension -eq ".cdn" } #If performance becomes important. Change this to -Filter Foreach($Item in $Items){ if(!($Item)){ Continue } - <# - try{ - $Type = [AssetsFactory]::GetAssetType($Item) - }Catch{ - - continue - } - #> $AllItems += [AssetsFactory]::CreateAsset($Item) } @@ -515,6 +611,7 @@ Class Asset{ [System.IO.FileInfo]$FilePath [String]$RelativePath [AssetType]$Type + [LocationType]$LocationType Asset(){} @@ -569,6 +666,16 @@ Class ScriptAsset : Asset { $S = "<{0} src='{1}'>" -f "Script",$this.GetFullFilePath() Return $S } + + [String] ToStringAsContent(){ + $TagName = "Script" + $FileContents = Get-Content -Path $this.GetFullFilePath() -Raw -Encoding utf8 + $StringBuilder = [System.Text.StringBuilder]::new() + $StringBuilder.AppendLine("<$TagName>") + $StringBuilder.AppendLine($FileContents) + $StringBuilder.Append("") + Return $StringBuilder + } } Class StyleAsset : Asset { @@ -585,6 +692,16 @@ Class StyleAsset : Asset { $S = "<{0} rel='{1}' type={2} href='{3}' >" -f "Link","stylesheet","text/css",$this.GetFullFilePath() Return $S } + + [String] ToStringAsContent() { + $TagName = "Style" + $FileContents = Get-Content -Path $this.GetFullFilePath() -Raw -Encoding utf8 + $StringBuilder = [System.Text.StringBuilder]::new() + $StringBuilder.AppendLine("<$TagName>") + $StringBuilder.AppendLine($FileContents) + $StringBuilder.Append("") + Return $StringBuilder + } } Class CDNAsset : Asset { @@ -639,6 +756,10 @@ Class CDNAsset : Asset { $S = "<{0} {1}='{2}' {3} {4}>" -f $t,$p,$this.raw.source,$full_CrossOrigin,$full_Integrity Return $S } + + [String] ToStringAsContent(){ + return $this.ToString() + } } function New-Logfile { @@ -1635,23 +1756,45 @@ Class polarAreaChart : Chart{ #endregion -Class IncludeFile { +Class Include { } -Class Include : IncludeFile { +Class IncludeFile : Include { [String]$Name [System.IO.DirectoryInfo]$FolderPath [System.IO.FileInfo]$FilePath + [LocationType]$LocationType + + IncludeFile(){} - Include([System.IO.FileInfo]$FilePath){ + IncludeFile([System.IO.FileInfo]$FilePath){ $this.FilePath = $FilePath $this.FolderPath = $FilePath.Directory $this.Name = $FilePath.BaseName + #If the parent folder of the includes folder is the PSHTML repository, then the include file is a of locationType 'Module' + if($this.FolderPath.Parent.Name -eq 'PSHTML'){ + $this.LocationType = [LocationType]::Module + }else{ + $this.LocationType = [LocationType]::Project + } + } + + [void] SetLocationType([LocationType]$LocationType){ + $this.LocationType = $LocationType } [String]ToString(){ + #Tostring() shouold not execute code in a tostring methode. shall be replaced by render() + $Rawcontent = [IO.File]::ReadAllText($this.FilePath.FullName) + $Content = [scriptBlock]::Create($Rawcontent).Invoke() + return $content + + } + + [String]Render(){ + $Rawcontent = [IO.File]::ReadAllText($this.FilePath.FullName) $Content = [scriptBlock]::Create($Rawcontent).Invoke() return $content @@ -1662,12 +1805,12 @@ Class Include : IncludeFile { Class IncludeFactory { Static [Include[]] Create([System.IO.DirectoryInfo]$Path){ - If(test-Path $Path){ + If(test-Path $Path.FullName){ $Items = Get-ChildItem $Path.FullName -Filter "*.ps1" $AllIncludes = @() Foreach($Item in $Items){ - $AllIncludes += [Include]::New($Item) + $AllIncludes += [IncludeFile]::New($Item) } @@ -9452,6 +9595,10 @@ function Write-PSHTMLAsset { The CDN file type must have a specifiy structure, which can be obtained by using the cmdlet New-CDNAssetFile + .PARAMETER AsContent + Use this switch to generate the content of the asset, instead of the link to the asset. + Use this switch to allow dynamic content to be directly integrated in your HTML pages (Allows to have no internet access / send per email.) + .EXAMPLE Write-PSHTMLAsset @@ -9482,7 +9629,8 @@ function Write-PSHTMLAsset { #> [CmdletBinding()] param ( - [ValidateSet("Script","Style","CDN")]$Type + [ValidateSet("Script","Style","CDN")]$Type, + [Switch]$AsContent ) @@ -9534,7 +9682,11 @@ function Write-PSHTMLAsset { } Foreach($A in $Asset){ - $A.ToString() + if($AsContent){ + $A.ToStringAsContent() + }else{ + $A.ToString() + } } } @@ -9875,16 +10027,14 @@ function Write-PSHTMLSymbol { } #Post Content -$ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Path -$ScriptPath = Split-Path -Path $PSScriptRoot New-Alias -Name Include -Value 'Write-PSHTMLInclude' -Description "Include parts of PSHTML documents using include files" -Force function Get-ScriptDirectory { Split-Path -Parent $PSCommandPath } $ScriptPath = Get-ScriptDirectory -$CF = Join-Path -Path $ScriptPath -ChildPath "pshtml.configuration.json" -#Write-host "loading config file: $($CF)" -ForegroundColor Blue +$ConfigFile = Join-Path -Path $ScriptPath -ChildPath "pshtml.configuration.json" + #Setting module variables - $Script:PSHTML_CONFIGURATION = Get-ConfigurationDocument -Path $CF -Force + $Script:PSHTML_CONFIGURATION = Get-ConfigurationDocument -Path $ConfigFile -Force $Script:Logfile = $Script:PSHTML_CONFIGURATION.GetDefaultLogFilePath() $Script:Logger = [Logger]::New($Script:LogFile) diff --git a/Tests/Chart.New-PSHTMLChart.Tests.ps1 b/Tests/Chart.New-PSHTMLChart.Tests.ps1 index 3aa7019c..1e8265cd 100644 --- a/Tests/Chart.New-PSHTMLChart.Tests.ps1 +++ b/Tests/Chart.New-PSHTMLChart.Tests.ps1 @@ -1,7 +1,5 @@ $TestsPath = Split-Path $MyInvocation.MyCommand.Path -#$FunctionsPath = join-Path -Path (get-item $TestsPath).Parent -ChildPath "Functions" - $RootFolder = (get-item $TestsPath).Parent Push-Location -Path $RootFolder.FullName @@ -28,96 +26,51 @@ InModuleScope PSHTML { $Data = @(3, 5) $Title = "Test Title" $CanvasID = "TestCanvasID" - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> + $bds = New-PSHTMLChartBarDataSet -Data $Data - - it '[New-PSHTMLChart][-Type Bar][-DataSet BarDataSet][Label][Title][CanvasId] Should not throw' { - {New-PSHTMLChart -Type bar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw - } + + $Is = New-PSHTMLChart -Type bar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $SplittedIsString = $Is -Split 'new Chart\(ctx,' #the .split() returns different results depepending on the powershell + + $ChartJsonbject = ConvertFrom-Json $SplittedIsString[-1].TrimEnd(");") it '[New-PSHTMLChart][-Type Bar][-DataSet BarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { - $Is = New-PSHTMLChart -Type bar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID - #don't touche this part, as the regex is very 'fragile' - - <# - $Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); -var myChart = new Chart(ctx, { - "type": "bar", - "data": { - "labels": [ - "january", - "february" - ], - "datasets": [ - { - "borderWidth": 1, - "xAxisID": null, - "yAxisID": null, - "backgroundColor": null, - "borderColor": null, - "borderSkipped": null, - "hoverBackgroundColor": null, - "hoverBorderColor": null, - "hoverBorderWidth": 0, - "data": [ - - ], - "label": null - } - ] - }, - "options": { - "barPercentage": 1, - "categoryPercentage": 1, - "responsive": false, - "barThickness": null, - "maxBarThickness": 0, - "offsetGridLines": true, - "scales": { - "yAxes": [ - { - "ticks": { - "beginAtZero": true - } - } - ], - "xAxes": [ - - ] - }, - "title": { - "display": true, - "text": "Test Title" - }, - "animation": { - "onComplete":null - } - } -} -); + $JavaScriptStartString = @' +var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = '@ -#> -<# -$Should = @' - -'@ -#> + $SplittedIsString[0] | Should -be $JavaScriptStartString -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"bar","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"xAxisID":null,"yAxisID":null,"backgroundColor":null,"borderColor":"","borderSkipped":null,"hoverBackgroundColor":null,"hoverBorderColor":null,"hoverBorderWidth":0,"data":[3,5],"label":null}]},"options":{"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ + #Converting Json data back to an object, to ease testing. + #In powershell 7, some properties are not located in the correct place, but the data is there. This was breaking the orignal tests, although all the data was still there. + #This is why the test is done in this way. - $Is | should be $Should - } - + + } + + it '[New-PSHTMLChart][-Type Bar][-DataSet BarDataSet][Label][Title][CanvasId] Javascript string should have: Dataset(s)' { + $ChartJsonbject.data.datasets.count | Should -Be 1 + + } + + it '[New-PSHTMLChart][-Type Bar][-DataSet BarDataSet][Label][Title][CanvasId] Javascript string should have: Type should be radar' { + $ChartJsonbject.type | Should -be 'bar' + } + + it '[New-PSHTMLChart][-Type Bar][-DataSet BarDataSet][Label][Title][CanvasId] Javascript string should have: correct labels' { + foreach ($Label in $Labels) { + $Label | Should -BeIn $ChartJsonbject.data.labels + } + + } + + it '[New-PSHTMLChart][-Type Bar][-DataSet BarDataSet][Label][Title][CanvasId] Javascript string should have: option object with right title.' { + $ChartJsonbject.options.title.text | Should -Be "Test Title" + + } } -tag "Chart", "Bar" @@ -128,35 +81,51 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $Data = @(3, 5) $Title = "Test Title" $CanvasID = "TestCanvasID" - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> + $bds = New-PSHTMLChartBarDataSet -Data $Data - - it '[New-PSHTMLChart][-Type horizontalBar][-DataSet BarDataSet][Label][Title][CanvasId] Should not throw' { - {New-PSHTMLChart -Type horizontalBar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw - } + + $Is = New-PSHTMLChart -Type horizontalBar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $SplittedIsString = $Is -Split 'new Chart\(ctx,' #the .split() returns different results depepending on the powershell + + $ChartJsonbject = ConvertFrom-Json $SplittedIsString[-1].TrimEnd(");") it '[New-PSHTMLChart][-Type horizontalBar][-DataSet BarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { - $Is = New-PSHTMLChart -Type horizontalBar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $JavaScriptStartString = @' +var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = +'@ + + + $SplittedIsString[0] | Should -be $JavaScriptStartString + + #Converting Json data back to an object, to ease testing. + #In powershell 7, some properties are not located in the correct place, but the data is there. This was breaking the orignal tests, although all the data was still there. + #This is why the test is done in this way. -<# -$Should = @' - -'@ -#> -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"horizontalBar","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"xAxisID":null,"yAxisID":null,"backgroundColor":null,"borderColor":"","borderSkipped":null,"hoverBackgroundColor":null,"hoverBorderColor":null,"hoverBorderWidth":0,"data":[3,5],"label":null}]},"options":{"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ - #$Is | should be $Should - $Is | should be $Should } + + it '[New-PSHTMLChart][-Type horizontalBar][-DataSet BarDataSet][Label][Title][CanvasId] Javascript string should have: Dataset(s)' { + $ChartJsonbject.data.datasets.count | Should -Be 1 - + } + + it '[New-PSHTMLChart][-Type horizontalBar][-DataSet BarDataSet][Label][Title][CanvasId] Javascript string should have: Type should be radar' { + $ChartJsonbject.type | Should -be 'HorizontalBar' + } + + it '[New-PSHTMLChart][-Type horizontalBar][-DataSet BarDataSet][Label][Title][CanvasId] Javascript string should have: correct labels' { + foreach ($Label in $Labels) { + $Label | Should -BeIn $ChartJsonbject.data.labels + } + + } + + it '[New-PSHTMLChart][-Type horizontalBar][-DataSet BarDataSet][Label][Title][CanvasId] Javascript string should have: option object with right title.' { + $ChartJsonbject.options.title.text | Should -Be "Test Title" + + } } -tag "Chart", "horizontalBar" @@ -173,72 +142,105 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart #Microsoft.PowerShell.Management\Get-Clipboard | Out-Null Add-Type -Assembly System.Drawing $bds = New-PSHTMLChartRadarDataSet -Data $data1 -label "2018" -borderColor (get-pshtmlColor -color blue) -backgroundColor "transparent" -hoverBackgroundColor (get-pshtmlColor -color green) -PointRadius 2 - - it '[New-PSHTMLChart][-Type Radar][-DataSet BarDataSet][Label][Title][CanvasId] Should not throw' { - {New-PSHTMLChart -Type Radar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw - } - it '[New-PSHTMLChart][-Type Radar][-DataSet BarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { - $Is = New-PSHTMLChart -Type radar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $Is = New-PSHTMLChart -Type radar -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $SplittedIsString = $Is -Split 'new Chart\(ctx,' #the .split() returns different results depepending on the powershell + + $ChartJsonbject = ConvertFrom-Json $SplittedIsString[-1].TrimEnd(");") - If($PSVersionTable.PsEdition -eq 'Core'){ - $Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"radar","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"pointBackgroundColor":"rgba(0, 0, 0, 0.1)","pointBorderColor":"rgba(0, 0, 0, 0.1)","pointBorderWidth":[1],"pointRadius":2.0,"pointStyle":"circle","xAxisID":null,"yAxisID":null,"backgroundColor":"transparent","borderColor":"rgb(0,0,255)","borderSkipped":null,"hoverBackgroundColor":"rgb(0,128,0)","hoverBorderColor":null,"hoverBorderWidth":0,"pointRotation":null,"pointHitRadius":0.0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0.0,"data":[17,25,18,17,22,30,35,44,4,1,6,12],"label":["2018"]}]},"options":{"scales":null,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); + it '[New-PSHTMLChart][-Type Radar][-DataSet ChartRadarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { + $JavaScriptStartString = @' +var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = '@ - } - else { - $Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"radar","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"pointBackgroundColor":"rgba(0, 0, 0, 0.1)","pointBorderColor":"rgba(0, 0, 0, 0.1)","pointBorderWidth":[1],"pointRadius":2,"pointStyle":"circle","xAxisID":null,"yAxisID":null,"backgroundColor":"transparent","borderColor":"rgb(0,0,255)","borderSkipped":null,"hoverBackgroundColor":"rgb(0,128,0)","hoverBorderColor":null,"hoverBorderWidth":0,"pointRotation":null,"pointHitRadius":0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0,"data":[17,25,18,17,22,30,35,44,4,1,6,12],"label":["2018"]}]},"options":{"scales":null,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ - } - $Is | should be $Should + + $SplittedIsString[0] | Should -be $JavaScriptStartString + + #Converting Json data back to an object, to ease testing. + #In powershell 7, some properties are not located in the correct place, but the data is there. This was breaking the orignal tests, although all the data was still there. + #This is why the test is done in this way. + + + + } + + it '[New-PSHTMLChart][-Type Radar][-DataSet ChartRadarDataSet][Label][Title][CanvasId] Javascript string should have: Dataset(s)' { + $ChartJsonbject.data.datasets.count | Should -Be 1 + + } + + it '[New-PSHTMLChart][-Type Radar][-DataSet ChartRadarDataSet][Label][Title][CanvasId] Javascript string should have: Type should be radar' { + $ChartJsonbject.type | Should -be 'radar' + } + + it '[New-PSHTMLChart][-Type Radar][-DataSet ChartRadarDataSet][Label][Title][CanvasId] Javascript string should have: correct labels' { + foreach($Label in $Labels) { + $Label | Should -BeIn $ChartJsonbject.data.labels + } + } + + it '[New-PSHTMLChart][-Type Radar][-DataSet ChartRadarDataSet][Label][Title][CanvasId] Javascript string should have: option object with right title.' { + $ChartJsonbject.options.title.text | Should -Be "Test Title" + + } } -tag "Chart", "Radar" Describe "Testing New-PSHTMLChart -Type polarArea" { - $Labels = @('red', 'green', 'yellow', 'grey', 'blue') $Data = @(3, 5,7,2,9) $Title = "Test Title" $CanvasID = "TestCanvasID" $BackgroundColor = @('red', 'green', 'yellow', 'grey', 'blue') - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> - - it '[New-PSHTMLChartPolarAreaDataSet][-Data $Data][-BackgroundColor $BackgroundColor][-label $Labels] Should not throw' { - { New-PSHTMLChartPolarAreaDataSet -Data $Data -BackgroundColor $BackgroundColor -label $Labels } | should not throw - } - $bds = New-PSHTMLChartPolarAreaDataSet -Data $Data -BackgroundColor $BackgroundColor -label $Labels - it '[New-PSHTMLChart][-Type polarArea][-DataSet BarDataSet][Label][Title][CanvasId] Should not throw' { - {New-PSHTMLChart -Type polarArea -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw - } + $bds = New-PSHTMLChartPolarAreaDataSet -Data $data1 -label "2018" -borderColor (get-pshtmlColor -color blue) -backgroundColor "transparent" -hoverBackgroundColor (get-pshtmlColor -color green) - it '[New-PSHTMLChart][-Type polarArea][-DataSet BarDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { - $Is = New-PSHTMLChart -Type polarArea -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID -<# + $Is = New-PSHTMLChart -Type polarArea -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $SplittedIsString = $Is -Split 'new Chart\(ctx,' #the .split() returns different results depepending on the powershell + + $ChartJsonbject = ConvertFrom-Json $SplittedIsString[-1].TrimEnd(");") -$Should = @' - + it '[New-PSHTMLChart][-Type polarArea][-DataSet PolarAreaDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { + $JavaScriptStartString = @' +var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = '@ -#> -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"polarArea","data":{"labels":["red","green","yellow","grey","blue"],"datasets":[{"borderWidth":1,"backgroundColor":["red","green","yellow","grey","blue"],"borderColor":[""],"borderSkipped":null,"hoverBackgroundColor":[""],"hoverBorderColor":[""],"hoverBorderWidth":0,"data":[3,5,7,2,9],"label":["red","green","yellow","grey","blue"]}]},"options":{"scales":null,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ - #$Is | should be $Should + $SplittedIsString[0] | Should -be $JavaScriptStartString + + #Converting Json data back to an object, to ease testing. + #In powershell 7, some properties are not located in the correct place, but the data is there. This was breaking the orignal tests, although all the data was still there. + #This is why the test is done in this way. + + + - $Is | should be $Should } + + it '[New-PSHTMLChart][-Type polarArea][-DataSet PolarAreaDataSet][Label][Title][CanvasId] Javascript string should have: Dataset(s)' { + $ChartJsonbject.data.datasets.count | Should -Be 1 + } + + it '[New-PSHTMLChart][-Type polarArea][-DataSet PolarAreaDataSet][Label][Title][CanvasId] Javascript string should have: Type should be radar' { + $ChartJsonbject.type | Should -be 'PolarArea' + } + + it '[New-PSHTMLChart][-Type polarArea][-DataSet PolarAreaDataSet][Label][Title][CanvasId] Javascript string should have: correct labels' { + foreach ($Label in $Labels) { + $Label | Should -BeIn $ChartJsonbject.data.labels + } + + } + + it '[New-PSHTMLChart][-Type polarArea][-DataSet PolarAreaDataSet][Label][Title][CanvasId] Javascript string should have: option object with right title.' { + $ChartJsonbject.options.title.text | Should -Be "Test Title" + + } } -tag "Chart", "polarArea" @@ -250,93 +252,48 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $Data = @(3, 5) $Title = "Test Title" $CanvasID = "TestCanvasID" - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> - $TestData = New-PSHTMLChartPieDataSet -Data $Data - - it '[New-PSHTMLChart][-Type Pie][-DataSet PieDataSet][Label][Title][CanvasId] Should not throw' { - {New-PSHTMLChart -Type Pie -DataSet $TestData -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw - } - - it '[New-PSHTMLChart][-Type Bar][-DataSet PieDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { - $IsTemp = New-PSHTMLChart -Type Pie -DataSet $TestData -Labels $Labels -Title $Title -CanvasID $CanvasID - #$Is = $IsTemp.Trim() - <# - $Should =@' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); -var myChart = new Chart(ctx, { - "type": "pie", - "data": { - "labels": [ - "january", - "february" - ], - "datasets": [ - { - "borderColor": "white", - "borderWidth": 1, - "backgroundColor": null, - "hoverBackgroundColor": [ - null - ], - "HoverBorderColor": null, - "HoverBorderWidth": 0, - "data": [ - 3, - 5 - ], - "label": null - } - ] - }, - "options": { - "barPercentage": 1, - "categoryPercentage": 1, - "responsive": false, - "barThickness": null, - "maxBarThickness": 0, - "offsetGridLines": true, - "scales": { - "yAxes": [ - { - "ticks": { - "beginAtZero": true - } - } - ], - "xAxes": [ - - ] - }, - "title": { - "display": true, - "text": "Test Title" - }, - "animation": { - "onComplete":null - } - } -} -); -'@ -#> + $bds = New-PSHTMLChartPieDataSet -Data $Data -<# + $Is = New-PSHTMLChart -Type Pie -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $SplittedIsString = $Is -Split 'new Chart\(ctx,' #the .split() returns different results depepending on the powershell + + $ChartJsonbject = ConvertFrom-Json $SplittedIsString[-1].TrimEnd(");") -$Should = @' - + it '[New-PSHTMLChart][-Type Pie][-DataSet PieDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { + $JavaScriptStartString = @' +var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = '@ -#> -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"pie","data":{"labels":["january","february"],"datasets":[{"borderColor":"white","borderWidth":1,"backgroundColor":null,"hoverBackgroundColor":[null],"HoverBorderColor":null,"HoverBorderWidth":0,"data":[3,5],"label":null}]},"options":{"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ + $SplittedIsString[0] | Should -be $JavaScriptStartString - $IsTemp | should be $Should + #Converting Json data back to an object, to ease testing. + #In powershell 7, some properties are not located in the correct place, but the data is there. This was breaking the orignal tests, although all the data was still there. + #This is why the test is done in this way. + + } + + it '[New-PSHTMLChart][-Type Pie][-DataSet PieDataSet][Label][Title][CanvasId] Javascript string should have: Dataset(s)' { + $ChartJsonbject.data.datasets.count | Should -Be 1 + + } + + it '[New-PSHTMLChart][-Type Pie][-DataSet PieDataSet][Label][Title][CanvasId] Javascript string should have: Type should be radar' { + $ChartJsonbject.type | Should -be 'Pie' } + + it '[New-PSHTMLChart][-Type Pie][-DataSet PieDataSet][Label][Title][CanvasId] Javascript string should have: correct labels' { + foreach ($Label in $Labels) { + $Label | Should -BeIn $ChartJsonbject.data.labels + } + + } + + it '[New-PSHTMLChart][-Type Pie][-DataSet PieDataSet][Label][Title][CanvasId] Javascript string should have: option object with right title.' { + $ChartJsonbject.options.title.text | Should -Be "Test Title" + + } + } -Tag "Chart","Pie" Describe "Testing New-PSHTMLChart -Type Doughnut" { @@ -348,105 +305,49 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $Labels = @("Closed","Unresolved","Pending","Open") $colors = @("LightGreen","Red","LightBlue","LightYellow") - #$bds = - <# mock -CommandName New-PSHTMLChartBarDataSet -MockWith { - New-MockObject -Type "datasetbar" - } #> - - $TestData = New-PSHTMLChartDoughnutDataSet -Data $data1 -label "March" -backgroundcolor $colors - - it '[New-PSHTMLChart][-Type Doughnut][-DataSet PieDataSet][Label][Title][CanvasId] Should not throw' { - {New-PSHTMLChart -Type Doughnut -DataSet $TestData -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw - } - - it '[New-PSHTMLChart][-Type Doughnut][-DataSet PieDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { - $Is = New-PSHTMLChart -Type Doughnut -DataSet $TestData -Labels $Labels -Title $Title -CanvasID $CanvasID - - #$Is = $Is.Trim() - <# -$Should =@' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); -var myChart = new Chart(ctx, { - "type": "doughnut", - "data": { - "labels": [ - "Closed", - "Unresolved", - "Pending", - "Open" - ], - "datasets": [ - { - "borderColor": "white", - "borderWidth": 1, - "backgroundColor": [ - "LightGreen", - "Red", - "LightBlue", - "LightYellow" - ], - "hoverBackgroundColor": [ - "LightGreen", - "Red", - "LightBlue", - "LightYellow" - ], - "HoverBorderColor": null, - "HoverBorderWidth": 0, - "data": [ - 34, - 7, - 11, - 19 - ], - "label": "March" - } - ] - }, - "options": { - "barPercentage": 1, - "categoryPercentage": 1, - "responsive": false, - "barThickness": null, - "maxBarThickness": 0, - "offsetGridLines": true, - "scales": { - "yAxes": [ - { - "ticks": { - "beginAtZero": true - } - } - ], - "xAxes": [ - - ] - }, - "title": { - "display": true, - "text": "Test Title" - }, - "animation": { - "onComplete":null - } - } -} -); -'@ -#> + + $bds = New-PSHTMLChartDoughnutDataSet -Data $data1 -label "March" -backgroundcolor $colors -<# + $Is = New-PSHTMLChart -Type doughnut -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $SplittedIsString = $Is -Split 'new Chart\(ctx,' #the .split() returns different results depepending on the powershell + + $ChartJsonbject = ConvertFrom-Json $SplittedIsString[-1].TrimEnd(");") -$Should = @' - + it '[New-PSHTMLChart][-Type Doughnut][-DataSet DoughnutDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { + $JavaScriptStartString = @' +var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = '@ -#> -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"doughnut","data":{"labels":["Closed","Unresolved","Pending","Open"],"datasets":[{"borderColor":"white","borderWidth":1,"backgroundColor":["LightGreen","Red","LightBlue","LightYellow"],"hoverBackgroundColor":["LightGreen","Red","LightBlue","LightYellow"],"HoverBorderColor":null,"HoverBorderWidth":0,"data":[34,7,11,19],"label":["March"]}]},"options":{"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ - $Is | should be $Should + $SplittedIsString[0] | Should -be $JavaScriptStartString + + #Converting Json data back to an object, to ease testing. + #In powershell 7, some properties are not located in the correct place, but the data is there. This was breaking the orignal tests, although all the data was still there. + #This is why the test is done in this way. + + } + + it '[New-PSHTMLChart][-Type Doughnut][-DataSet DoughnutDataSet][Label][Title][CanvasId] Javascript string should have: Dataset(s)' { + $ChartJsonbject.data.datasets.count | Should -Be 1 + + } + + it '[New-PSHTMLChart][-Type Doughnut][-DataSet DoughnutDataSet][Label][Title][CanvasId] Javascript string should have: Type should be radar' { + $ChartJsonbject.type | Should -be 'Doughnut' } + + it '[New-PSHTMLChart][-Type Doughnut][-DataSet DoughnutDataSet][Label][Title][CanvasId] Javascript string should have: correct labels' { + foreach ($Label in $Labels) { + $Label | Should -BeIn $ChartJsonbject.data.labels + } + + } + + it '[New-PSHTMLChart][-Type Doughnut][-DataSet DoughnutDataSet][Label][Title][CanvasId] Javascript string should have: option object with right title.' { + $ChartJsonbject.options.title.text | Should -Be "Test Title" + + } + + } -Tag "Chart","Doughnut" Describe "Testing New-PSHTMLChart -Type Line" { @@ -464,136 +365,52 @@ var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart $bds = New-PSHTMLChartLineDataSet -Data $Data $bds2 = New-PSHTMLChartLineDataSet -Data $Data2 - it '[New-PSHTMLChart][-Type Line][-DataSet LineDataSet][Label][Title][CanvasId] Should not throw' { - {New-PSHTMLChart -Type Line -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID} | should not throw - } + $Is = New-PSHTMLChart -Type Line -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID + $SplittedIsString = $Is -Split 'new Chart\(ctx,' #the .split() returns different results depepending on the powershell + + $ChartJsonbject = ConvertFrom-Json $SplittedIsString[-1].TrimEnd(");") it '[New-PSHTMLChart][-Type Line][-DataSet LineDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { - $Is = New-PSHTMLChart -Type Line -DataSet $bds -Labels $Labels -Title $Title -CanvasID $CanvasID - #don't touche this part, as the regex is very 'fragile' -<# -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); -var myChart = new Chart(ctx, { - "type": "line", - "data": { - "labels": [ - "january", - "february" - ], - "datasets": [ - { - "borderWidth": 1, - "borderDash": [ - 0 - ], - "borderDashOffSet": 0, - "cubicInterpolationMode": "default", - "fill": false, - "lineTension": 0.5, - "pointBackgroundColor": "rgb(255,255,255)", - "pointBorderColor": "rgb(0,0,0)", - "pointBorderWidth": [ - 1 - ], - "pointRadius": 4, - "pointStyle": "circle", - "showLine": true, - "backgroundColor": null, - "borderColor": null, - "borderCapStyle": null, - "borderJoinStyle": null, - "pointRotation": null, - "pointHitRadius": null, - "PointHoverBackgroundColor": null, - "pointHoverBorderColor": null, - "pointHoverBorderWidth": 0, - "pointHoverRadius": null, - "spanGaps": false, - "data": [ - - ], - "label": null - } - ] - }, - "options": { - "showLines": true, - "spanGaps": false, - "barPercentage": 1, - "categoryPercentage": 1, - "responsive": false, - "barThickness": null, - "maxBarThickness": 0, - "offsetGridLines": true, - "scales": { - "yAxes": [ - { - "ticks": { - "beginAtZero": true - } - } - ], - "xAxes": [ - - ] - }, - "title": { - "display": true, - "text": "Test Title" - }, - "animation": { - "onComplete":null - } - } -} -); -'@ -#> - -If($PSVersionTable.PsEdition -eq 'Core'){ - - <# - - $Should = @' - -'@ - #> - -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"line","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"borderDash":[0],"borderDashOffSet":0,"cubicInterpolationMode":"default","fill":false,"lineTension":0.5,"pointBackgroundColor":"rgb(255,255,255)","pointBorderColor":"rgb(0,0,0)","pointBorderWidth":[1],"pointRadius":4.0,"pointStyle":"circle","showLine":true,"backgroundColor":null,"borderColor":null,"borderCapStyle":null,"borderJoinStyle":null,"pointRotation":null,"pointHitRadius":0.0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0.0,"spanGaps":false,"data":[3,5],"label":null}]},"options":{"showLines":true,"spanGaps":false,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); + $JavaScriptStartString = @' +var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = '@ -}else{ + $SplittedIsString[0] | Should -be $JavaScriptStartString - <# - - $Should = @' - -'@ - #> - -$Should = @' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"line","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"borderDash":[0],"borderDashOffSet":0,"cubicInterpolationMode":"default","fill":false,"lineTension":0.5,"pointBackgroundColor":"rgb(255,255,255)","pointBorderColor":"rgb(0,0,0)","pointBorderWidth":[1],"pointRadius":4,"pointStyle":"circle","showLine":true,"backgroundColor":null,"borderColor":null,"borderCapStyle":null,"borderJoinStyle":null,"pointRotation":null,"pointHitRadius":0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0,"spanGaps":false,"data":[3,5],"label":null}]},"options":{"showLines":true,"spanGaps":false,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ - -} + #Converting Json data back to an object, to ease testing. + #In powershell 7, some properties are not located in the correct place, but the data is there. This was breaking the orignal tests, although all the data was still there. + #This is why the test is done in this way. - $Is | should be $Should } + + it '[New-PSHTMLChart][-Type Line][-DataSet LineDataSet][Label][Title][CanvasId] Javascript string should have: Dataset(s)' { + $ChartJsonbject.data.datasets.count | Should -Be 1 + + } + + it '[New-PSHTMLChart][-Type Line][-DataSet LineDataSet][Label][Title][CanvasId] Javascript string should have: Type should be radar' { + $ChartJsonbject.type | Should -be 'Line' + } + + it '[New-PSHTMLChart][-Type Line][-DataSet LineDataSet][Label][Title][CanvasId] Javascript string should have: correct labels' { + foreach ($Label in $Labels) { + $Label | Should -BeIn $ChartJsonbject.data.labels + } + + } + + it '[New-PSHTMLChart][-Type Line][-DataSet LineDataSet][Label][Title][CanvasId] Javascript string should have: option object with right title.' { + $ChartJsonbject.options.title.text | Should -Be "Test Title" + + } + it '[New-PSHTMLChart][-Type Line][-DataSet Multiple LineDataSet][Label][Title][CanvasId] Should create ChartJS javascript Code' { $Is = New-PSHTMLChart -Type Line -DataSet $bds,$bds2 -Labels $Labels -Title $Title -CanvasID $CanvasID - If($PSVersionTable.PsEdition -eq 'Core'){ - $Should =@' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"line","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"borderDash":[0],"borderDashOffSet":0,"cubicInterpolationMode":"default","fill":false,"lineTension":0.5,"pointBackgroundColor":"rgb(255,255,255)","pointBorderColor":"rgb(0,0,0)","pointBorderWidth":[1],"pointRadius":4.0,"pointStyle":"circle","showLine":true,"backgroundColor":null,"borderColor":null,"borderCapStyle":null,"borderJoinStyle":null,"pointRotation":null,"pointHitRadius":0.0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0.0,"spanGaps":false,"data":[3,5],"label":null},{"borderWidth":1,"borderDash":[0],"borderDashOffSet":0,"cubicInterpolationMode":"default","fill":false,"lineTension":0.5,"pointBackgroundColor":"rgb(255,255,255)","pointBorderColor":"rgb(0,0,0)","pointBorderWidth":[1],"pointRadius":4.0,"pointStyle":"circle","showLine":true,"backgroundColor":null,"borderColor":null,"borderCapStyle":null,"borderJoinStyle":null,"pointRotation":null,"pointHitRadius":0.0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0.0,"spanGaps":false,"data":[12,15],"label":null}]},"options":{"showLines":true,"spanGaps":false,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ - }Else{ - $Should =@' -var ctx = document.getElementById("TestCanvasID").getContext('2d'); var myChart = new Chart(ctx, {"type":"line","data":{"labels":["january","february"],"datasets":[{"borderWidth":1,"borderDash":[0],"borderDashOffSet":0,"cubicInterpolationMode":"default","fill":false,"lineTension":0.5,"pointBackgroundColor":"rgb(255,255,255)","pointBorderColor":"rgb(0,0,0)","pointBorderWidth":[1],"pointRadius":4,"pointStyle":"circle","showLine":true,"backgroundColor":null,"borderColor":null,"borderCapStyle":null,"borderJoinStyle":null,"pointRotation":null,"pointHitRadius":0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0,"spanGaps":false,"data":[3,5],"label":null},{"borderWidth":1,"borderDash":[0],"borderDashOffSet":0,"cubicInterpolationMode":"default","fill":false,"lineTension":0.5,"pointBackgroundColor":"rgb(255,255,255)","pointBorderColor":"rgb(0,0,0)","pointBorderWidth":[1],"pointRadius":4,"pointStyle":"circle","showLine":true,"backgroundColor":null,"borderColor":null,"borderCapStyle":null,"borderJoinStyle":null,"pointRotation":null,"pointHitRadius":0,"PointHoverBackgroundColor":null,"pointHoverBorderColor":null,"pointHoverBorderWidth":0,"pointHoverRadius":0,"spanGaps":false,"data":[12,15],"label":null}]},"options":{"showLines":true,"spanGaps":false,"barPercentage":1,"categoryPercentage":1,"responsive":false,"barThickness":null,"maxBarThickness":0,"offsetGridLines":true,"scales":{"yAxes":[{"ticks":{"beginAtZero":true}}],"xAxes":[""]},"title":{"display":true,"text":"Test Title"},"animation":{"onComplete":null}}} ); -'@ - } - $Is | should be $Should + $SplittedIsString = $Is -Split 'new Chart\(ctx,' #the .split() returns different results depepending on the powershell + + $ChartJsonbject = ConvertFrom-Json $SplittedIsString[-1].TrimEnd(");") + $ChartJsonbject.data.datasets.count | Should -Be 2 } diff --git a/Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 b/Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 index 077fa83b..600c5e50 100644 --- a/Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 +++ b/Tests/Pshtml.Utilities.Assets.Unit.Tests.Ps1 @@ -48,25 +48,25 @@ InModuleScope PSHTML { It '[Styles] Bootstrap'{ $var | ? {$_.Type -eq 'Style' -and $_.Name -eq 'Bootstrap'} | Should not BeNullOrEmpty - ($var | ? {$_.Type -eq 'Style' -and $_.Name -eq 'Bootstrap'} | Measure).Count | Should be 1 + ($var | ? {$_.Type -eq 'Style' -and $_.Name -eq 'Bootstrap'} | Measure-Object).Count | Should be 1 Test-Path ($var | ? {$_.Type -eq 'Style' -and $_.Name -eq 'Bootstrap'}).GetFullfilePath() | Should be $true } It '[Javascript] Bootstrap'{ $var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Bootstrap'} | Should not BeNullOrEmpty - ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Bootstrap'} | Measure).Count | Should be 1 + ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Bootstrap'} | Measure-Object).Count | Should be 1 Test-Path ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Bootstrap'}).GetFullfilePath() | Should be $true } It '[Javascript] Jquery'{ $var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Jquery'} | Should not BeNullOrEmpty - ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Jquery'} | Measure).Count | Should be 1 + ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Jquery'} | Measure-Object).Count | Should be 1 Test-Path ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'Jquery'}).GetFullfilePath() | Should be $true } It '[Javascript] ChartJs'{ $var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'ChartJS'} | Should not BeNullOrEmpty - ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'ChartJS'} | Measure).Count | Should be 1 + ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'ChartJS'} | Measure-Object).Count | Should be 1 Test-Path ($var | ? {$_.Type -eq 'Script' -and $_.Name -eq 'ChartJS'}).GetFullfilePath() | Should be $true } @@ -182,7 +182,7 @@ InModuleScope PSHTML { #If no type is specified, and that the Asset has one or several script / style tags. All of them should be printed out. $HtmlScripts = Write-PSHTMLAsset -Name BootStrap - ($HtmlScripts | Measure).Count | Should be 2 # Bootstrap has one .js file and .css file. + ($HtmlScripts | Measure-Object).Count | Should be 2 # Bootstrap has one .js file and .css file. $HasScript = $false $HasStyle = $False Foreach($sci in $HtmlScripts){ @@ -202,6 +202,24 @@ InModuleScope PSHTML { } + It '[-Name BootStrap -Type Script -AsContent] Should write asset as content in HTML document in Script tags ' { + + $Assets = Write-PSHTMLAsset -Name BootStrap -Type Style -AsContent + Foreach ($Asset in $Assets) { + $Regex = "^