Skip to content

Commit b3097b7

Browse files
committed
feat(tests): ✨ add comprehensive tests for Get-PreviewPanel, Get-ShortcutKeyPanel, and Get-TitlePanel
* Implemented tests to validate the functionality of `Get-PreviewPanel`, ensuring it returns a `Spectre.Console.Panel` object and handles various scenarios. * Added tests for `Get-ShortcutKeyPanel` to verify it correctly renders known keys. * Created tests for `Get-TitlePanel` to check its output includes the current date and Pester object details. * Introduced a helper function `Get-RenderedText` to streamline text rendering in tests.
1 parent 2044548 commit b3097b7

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed

tests/Get-PreviewPanel.tests.ps1

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
Describe 'Get-PreviewPanel' {
2+
BeforeAll {
3+
. (Join-Path $PSScriptRoot 'Helpers.ps1')
4+
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
5+
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
6+
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
7+
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
8+
$outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1"
9+
10+
# Get module commands
11+
# Remove all versions of the module from the session. Pester can't handle multiple versions.
12+
Get-Module $env:BHProjectName | Remove-Module -Force -ErrorAction Ignore
13+
Import-Module -Name $outputModVerManifest -Verbose:$false -ErrorAction Stop
14+
15+
InModuleScope $env:BHProjectName {
16+
$script:ContainerWidth = 80
17+
$script:ContainerHeight = 100
18+
$size = [Spectre.Console.Size]::new($containerWidth, $containerHeight)
19+
$script:renderOptions = [Spectre.Console.Rendering.RenderOptions]::new(
20+
[Spectre.Console.AnsiConsole]::Console.Profile.Capabilities,
21+
$size
22+
)
23+
$script:renderOptions.Justification = $null
24+
$script:renderOptions.Height = $null
25+
$container = New-PesterContainer -Scriptblock {
26+
Describe 'Demo Tests' {
27+
Context 'Contextualize It' {
28+
It 'Test1' {
29+
$true | Should -Be $true
30+
}
31+
It 'Test2' {
32+
$false | Should -Be $true
33+
}
34+
}
35+
}
36+
}
37+
$script:run = Invoke-Pester -Container $container -PassThru -Output 'None'
38+
$script:Items = Get-ListFromObject -Object $run.Containers[0]
39+
40+
$script:getPreviewPanelSplat = @{
41+
Items = $script:Items
42+
SelectedItem = 'Demo Tests'
43+
PreviewHeight = $script:ContainerHeight
44+
PreviewWidth = $script:ContainerWidth
45+
}
46+
$script:panel = Get-PreviewPanel @getPreviewPanelSplat
47+
}
48+
}
49+
It 'should return a Spectre.Console.Panel object' {
50+
InModuleScope $env:BHProjectName {
51+
$script:panel.GetType().ToString() | Should -BeExactly 'Spectre.Console.Panel'
52+
}
53+
}
54+
55+
It 'should call breakdown chart' {
56+
InModuleScope $env:BHProjectName {
57+
Mock Format-SpectreBreakdownChart -Verifiable
58+
$script:panel = Get-PreviewPanel @script:getPreviewPanelSplat
59+
Should -Invoke Format-SpectreBreakdownChart -Exactly 1 -Scope It
60+
}
61+
}
62+
63+
It 'should print "Please select an item." when SelectedItem is ".."' {
64+
InModuleScope $env:BHProjectName {
65+
$getPreviewPanelSplat = @{
66+
Items = $script:Items
67+
SelectedItem = '..'
68+
PreviewHeight = $script:ContainerHeight
69+
PreviewWidth = $script:ContainerWidth
70+
}
71+
$panel = Get-PreviewPanel @getPreviewPanelSplat
72+
global:Get-RenderedText -panel $panel -renderOptions $script:renderOptions -containerWidth $script:ContainerWidth |
73+
Should -BeLike "*Please select an item.*"
74+
}
75+
}
76+
77+
It 'should print warning when the screen is too small' {
78+
InModuleScope $env:BHProjectName {
79+
$Items = Get-ListFromObject -Object $script:run.Containers[0].Blocks[0].Order[0]
80+
$size = [Spectre.Console.Size]::new(80, 10)
81+
$renderOptions = [Spectre.Console.Rendering.RenderOptions]::new(
82+
[Spectre.Console.AnsiConsole]::Console.Profile.Capabilities,
83+
$size
84+
)
85+
$getPreviewPanelSplat = @{
86+
Items = $Items
87+
SelectedItem = 'Test1'
88+
PreviewHeight = 5
89+
PreviewWidth = $script:ContainerWidth
90+
}
91+
$panel = Get-PreviewPanel @getPreviewPanelSplat
92+
global:Get-RenderedText -panel $panel -renderOptions $renderOptions -containerWidth $script:ContainerWidth |
93+
Should -BeLike "*resize your terminal*"
94+
}
95+
}
96+
97+
It 'should print the script block for a Test' {
98+
InModuleScope $env:BHProjectName {
99+
$Items = Get-ListFromObject -Object $script:run.Containers[0].Blocks[0].Order[0].Tests
100+
$getPreviewPanelSplat = @{
101+
Items = $Items
102+
SelectedItem = 'Test1'
103+
PreviewHeight = 100
104+
PreviewWidth = 100
105+
}
106+
Mock -CommandName 'Get-SpectreEscapedText' -Verifiable
107+
$panel = Get-PreviewPanel @getPreviewPanelSplat
108+
#global:Get-RenderedText -panel $panel -renderOptions $renderOptions -containerWidth 100 |
109+
# Should -BeLike 'Passed'
110+
Should -Invoke Get-SpectreEscapedText -Exactly 1 -Scope It
111+
}
112+
}
113+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Describe 'Get-ShortcutKeyPanel' {
2+
BeforeAll {
3+
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
4+
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
5+
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
6+
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
7+
$outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1"
8+
9+
# Get module commands
10+
# Remove all versions of the module from the session. Pester can't handle multiple versions.
11+
Get-Module $env:BHProjectName | Remove-Module -Force -ErrorAction Ignore
12+
Import-Module -Name $outputModVerManifest -Verbose:$false -ErrorAction Stop
13+
14+
InModuleScope $env:BHProjectName {
15+
$script:ContainerWidth = 80
16+
$script:ContainerHeight = 5
17+
$size = [Spectre.Console.Size]::new($containerWidth, $containerHeight)
18+
$script:renderOptions = [Spectre.Console.Rendering.RenderOptions]::new(
19+
[Spectre.Console.AnsiConsole]::Console.Profile.Capabilities,
20+
$size
21+
)
22+
$script:renderOptions.Justification = $null
23+
$script:renderOptions.Height = $null
24+
}
25+
}
26+
It 'should return a Spectre.Console.Panel object' {
27+
InModuleScope $env:BHProjectName {
28+
$panel = Get-ShortcutKeyPanel
29+
$panel.GetType().ToString() | Should -BeExactly 'Spectre.Console.Panel'
30+
}
31+
}
32+
33+
It 'should print some known keys' {
34+
InModuleScope $env:BHProjectName {
35+
Mock -CommandName 'Get-Date' -MockWith { '2025-01-10 12:00:00' }
36+
$panel = Get-ShortcutKeyPanel
37+
$render = $panel.Render($script:renderOptions, $script:ContainerWidth)
38+
# These are rendered segments.
39+
(
40+
'Navigate'
41+
) | ForEach-Object {
42+
$render.Text | Should -Contain $_
43+
}
44+
}
45+
}
46+
}

tests/Get-TitlePanel.tests.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Describe 'Get-TitlePanel' {
2+
BeforeAll {
3+
$manifest = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
4+
$outputDir = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
5+
$outputModDir = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
6+
$outputModVerDir = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
7+
$outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1"
8+
9+
# Get module commands
10+
# Remove all versions of the module from the session. Pester can't handle multiple versions.
11+
Get-Module $env:BHProjectName | Remove-Module -Force -ErrorAction Ignore
12+
Import-Module -Name $outputModVerManifest -Verbose:$false -ErrorAction Stop
13+
14+
InModuleScope $env:BHProjectName {
15+
$script:ContainerWidth = 80
16+
$script:ContainerHeight = 5
17+
$size = [Spectre.Console.Size]::new($containerWidth, $containerHeight)
18+
$script:renderOptions = [Spectre.Console.Rendering.RenderOptions]::new(
19+
[Spectre.Console.AnsiConsole]::Console.Profile.Capabilities,
20+
$size
21+
)
22+
$script:renderOptions.Justification = $null
23+
$script:renderOptions.Height = $null
24+
}
25+
}
26+
It 'should return a Spectre.Console.Panel object' {
27+
InModuleScope $env:BHProjectName {
28+
$title = Get-TitlePanel
29+
$title.GetType().ToString() | Should -BeExactly 'Spectre.Console.Panel'
30+
}
31+
}
32+
33+
It 'should print Pester Explorer with current date' {
34+
InModuleScope $env:BHProjectName {
35+
Mock -CommandName 'Get-Date' -MockWith { '2025-01-10 12:00:00' }
36+
$title = Get-TitlePanel
37+
$render = $title.Render($script:renderOptions, $script:ContainerWidth)
38+
# These are rendered segments.
39+
(
40+
'Pester',
41+
'Explorer',
42+
'2025-01-10',
43+
'12:00:00'
44+
) | ForEach-Object {
45+
$render.Text | Should -Contain $_
46+
}
47+
}
48+
}
49+
50+
It 'should include the Pester object type and name if provided' {
51+
InModuleScope $env:BHProjectName {
52+
$pesterBlock = [Pester.Block]::Create()
53+
$pesterBlock.Name = 'Blockhead'
54+
$pesterBlock.Result = 'Failed'
55+
$titleWithItem = Get-TitlePanel -Item $pesterBlock
56+
$renderWithItem = $titleWithItem.Render($script:renderOptions, $script:ContainerWidth)
57+
$renderWithItem.Text | Should -Contain 'Block:'
58+
$renderWithItem.Text | Should -Contain 'Blockhead'
59+
}
60+
}
61+
}

tests/Helpers.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
function global:Get-RenderedText {
3+
param (
4+
$panel,
5+
$renderOptions,
6+
$containerWidth
7+
)
8+
$render = $panel.Render($renderOptions, $ContainerWidth)
9+
10+
# These are rendered segments.
11+
$onlyText = $render |
12+
Where-Object {
13+
#$_.IsLineBreak -ne $true -and
14+
$_.IsControlCode -ne $true -and
15+
#$_.IsWhiteSpace -ne $true -and
16+
$_.Text -notin @('', '', '', '', '', '') -and
17+
$_.Text -notmatch '─{2,}'
18+
}
19+
# Join the text segments into a single string
20+
$output = [System.Text.StringBuilder]::new()
21+
22+
foreach ($textSegment in $onlyText) {
23+
if ($textSegment.IsLineBreak) {
24+
[void]$output.AppendLine() # Append a newline for line breaks
25+
} else {
26+
[void]$output.Append($textSegment.Text)
27+
}
28+
}
29+
return $output.ToString().Trim()
30+
}

0 commit comments

Comments
 (0)