__tests__/Remove-WorkSheet.tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#Requires -Modules Pester
Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force

Describe "Remove Worksheet" {
    Context "Remove a worksheet output" {
        BeforeEach {
            # Create three sheets
            $data = ConvertFrom-Csv @"
Name,Age
Jane,10
John,20
"@

            $xlFile1 = "$env:TEMP\RemoveWorsheet1.xlsx"
            Remove-Item $xlFile1 -ErrorAction SilentlyContinue

            $data | Export-Excel -Path $xlFile1 -WorksheetName Target1
            $data | Export-Excel -Path $xlFile1 -WorksheetName Target2
            $data | Export-Excel -Path $xlFile1 -WorksheetName Target3
            $data | Export-Excel -Path $xlFile1 -WorksheetName Sheet1

            $xlFile2 = "$env:TEMP\RemoveWorsheet2.xlsx"
            Remove-Item $xlFile2 -ErrorAction SilentlyContinue

            $data | Export-Excel -Path $xlFile2 -WorksheetName Target1
            $data | Export-Excel -Path $xlFile2 -WorksheetName Target2
            $data | Export-Excel -Path $xlFile2 -WorksheetName Target3
            $data | Export-Excel -Path $xlFile2 -WorksheetName Sheet1
        }

        it "Should throw about the Path" {
            {Remove-WorkSheet} | Should throw 'Remove-WorkSheet requires the and Excel file'
        }

        it "Should delete Target2" {
            Remove-WorkSheet -Path $xlFile1 -WorksheetName Target2

            $actual = Get-ExcelSheetInfo -Path $xlFile1

            $actual.Count   | Should Be 3
            $actual[0].Name | Should Be "Target1"
            $actual[1].Name | Should Be "Target3"
            $actual[2].Name | Should Be "Sheet1"
        }

        it "Should delete Sheet1" {
            Remove-WorkSheet -Path $xlFile1

            $actual = Get-ExcelSheetInfo -Path $xlFile1

            $actual.Count   | Should Be 3
            $actual[0].Name | Should Be "Target1"
            $actual[1].Name | Should Be "Target2"
            $actual[2].Name | Should Be "Target3"
        }

        it "Should delete multiple sheets" {
            Remove-WorkSheet -Path $xlFile1 -WorksheetName Target1, Sheet1

            $actual = Get-ExcelSheetInfo -Path $xlFile1

            $actual.Count   | Should Be 2
            $actual[0].Name | Should Be "Target2"
            $actual[1].Name | Should Be "Target3"
        }

        it "Should delete sheet from multiple workbooks" {

            Get-ChildItem "$env:TEMP\RemoveWorsheet*.xlsx" | Remove-WorkSheet

            $actual = Get-ExcelSheetInfo -Path $xlFile1

            $actual.Count   | Should Be 3
            $actual[0].Name | Should Be "Target1"
            $actual[1].Name | Should Be "Target2"
            $actual[2].Name | Should Be "Target3"
        }
    }
}