Skip to content

Commit 18ab4f4

Browse files
Add comprehensive test for list scrolling feature and complete validation
Co-authored-by: HeyItsGilbert <[email protected]>
1 parent 58d45d0 commit 18ab4f4

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

tests/Get-ListPanel.tests.ps1

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
Describe 'Get-ListPanel Scrolling' {
2+
BeforeAll {
3+
# Import the function for testing
4+
. (Join-Path $PSScriptRoot '../PesterExplorer/Private/Get-ListPanel.ps1')
5+
6+
# Mock Spectre.Console functions to avoid dependency issues
7+
Mock Write-SpectreHost {
8+
param($Message, [switch]$PassThru)
9+
if ($PassThru) { return $Message } else { return $Message }
10+
}
11+
Mock Format-SpectrePadded {
12+
param($Padding, [Parameter(ValueFromPipeline)]$InputObject)
13+
process { return "[$Padding] $InputObject" }
14+
}
15+
Mock Format-SpectreRows {
16+
param([Parameter(ValueFromPipeline)]$InputObject)
17+
begin { $items = @() }
18+
process { $items += $InputObject }
19+
end { return $items }
20+
}
21+
Mock Format-SpectrePanel {
22+
param($Header, $Color, [switch]$Expand, [Parameter(ValueFromPipeline)]$InputObject)
23+
process { return "Panel[$Header,$Color]: $($InputObject -join '|')" }
24+
}
25+
Mock Format-SpectreTextPath {
26+
param($Path, $PathStyle)
27+
return "Path: $Path"
28+
}
29+
# Mock Spectre.Console.Color to avoid type errors
30+
Add-Type -TypeDefinition @'
31+
namespace Spectre.Console {
32+
public enum Color {
33+
Grey = 0
34+
}
35+
}
36+
'@ -ErrorAction SilentlyContinue
37+
}
38+
39+
Context 'Without scrolling (ListHeight = 0 or list fits)' {
40+
It 'Shows all items when ListHeight is 0' {
41+
$testList = @('Test1', 'Test2', 'Test3', 'Test4', 'Test5')
42+
$result = Get-ListPanel -List $testList -SelectedItem 'Test1' -ListScrollPosition 0 -ListHeight 0
43+
44+
# Should contain all items
45+
$result.ToString() | Should -Match 'Test5'
46+
}
47+
48+
It 'Shows all items when list is shorter than height' {
49+
$testList = @('Test1', 'Test2', 'Test3')
50+
$result = Get-ListPanel -List $testList -SelectedItem 'Test1' -ListScrollPosition 0 -ListHeight 5
51+
52+
# Should contain all items, no scroll indicators
53+
$result.ToString() | Should -Match 'Test3'
54+
$result.ToString() | Should -Not -Match '\.\.\.'
55+
}
56+
}
57+
58+
Context 'With scrolling (ListHeight > 0 and list is long)' {
59+
It 'Shows limited items with more indicator when at top' {
60+
$testList = @('Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8')
61+
$result = Get-ListPanel -List $testList -SelectedItem 'Test1' -ListScrollPosition 0 -ListHeight 5
62+
63+
# Should contain first few items and more indicator
64+
$result.ToString() | Should -Match 'Test1'
65+
$result.ToString() | Should -Match 'Test4'
66+
$result.ToString() | Should -Match '\.\.\.more'
67+
$result.ToString() | Should -Not -Match 'Test8'
68+
}
69+
70+
It 'Shows scroll up indicator when scrolled down' {
71+
$testList = @('Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8')
72+
$result = Get-ListPanel -List $testList -SelectedItem 'Test5' -ListScrollPosition 2 -ListHeight 5
73+
74+
# Should contain scroll up indicator
75+
$resultStr = $result.ToString()
76+
$resultStr | Should -Match '\.\.\.' # Up indicator
77+
$resultStr | Should -Match 'Test5'
78+
}
79+
80+
It 'Shows both scroll indicators when in middle' {
81+
$testList = @('Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8', 'Test9', 'Test10')
82+
$result = Get-ListPanel -List $testList -SelectedItem 'Test6' -ListScrollPosition 3 -ListHeight 5
83+
84+
$resultStr = $result.ToString()
85+
# Should have both up and down indicators
86+
$resultStr | Should -Match '\.\.\.' # Should have some scroll indicator
87+
$resultStr | Should -Match 'Test6'
88+
}
89+
90+
It 'Handles boundary conditions correctly' {
91+
$testList = @('Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6')
92+
$result = Get-ListPanel -List $testList -SelectedItem 'Test6' -ListScrollPosition 2 -ListHeight 5
93+
94+
# Should show items starting from scroll position
95+
$resultStr = $result.ToString()
96+
$resultStr | Should -Match 'Test6'
97+
}
98+
}
99+
100+
Context 'Parameter validation' {
101+
It 'Has required new parameters' {
102+
$params = (Get-Command Get-ListPanel).Parameters
103+
$params.Keys | Should -Contain 'ListScrollPosition'
104+
$params.Keys | Should -Contain 'ListHeight'
105+
}
106+
107+
It 'Accepts all parameter types correctly' {
108+
$testList = @('Test1', 'Test2')
109+
{ Get-ListPanel -List $testList -SelectedItem 'Test1' -ListScrollPosition 0 -ListHeight 5 } | Should -Not -Throw
110+
}
111+
}
112+
113+
Context 'Selected item highlighting' {
114+
It 'Highlights selected item correctly with scrolling' {
115+
$testList = @('Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6')
116+
$result = Get-ListPanel -List $testList -SelectedItem 'Test3' -ListScrollPosition 1 -ListHeight 4
117+
118+
# Selected item should be highlighted (contains Turquoise2)
119+
$result.ToString() | Should -Match 'Turquoise2.*Test3'
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)