Functions/Get-TeamChannelObject.Tests.ps1

describe "BitTitan.Runbooks.MicrosoftTeams/Get-TeamChannelObject" -Tag "module", "unit" {

    # Import the function to test
    . "$($PSScriptRoot)\Get-TeamChannelObject.ps1"

    # Declare and mock our own external functions
    $defaultTeam = @{
        DisplayName = "Team Name"
        Description = "team description"
        GroupId     = [Guid]::Empty.Guid
    }
    $defaultChannel = @{
        DisplayName = "Team Channel"
        Description = "team channel description"
    }
    function Get-TeamObject {
        param ($DisplayName, $GroupId)
    }
    mock Get-TeamObject {
        return $defaultTeam
    }
    function Get-TeamChannel {
        param ($GroupId)
    }
    mock Get-TeamChannel {
        return $defaultChannel
    }

    it "retrieves the channel" {
        # Call the function
        $output = Get-TeamChannelObject -TeamDisplayName "Team Name" -ChannelDisplayName "Team Channel" `
            -ErrorAction SilentlyContinue -ErrorVariable errorVariable

        # Verify the mocks
        Assert-MockCalled Get-TeamObject -Times 1 -Exactly -Scope it
        Assert-MockCalled Get-TeamChannel -Times 1 -Exactly -ParameterFilter {
            $GroupId -eq [Guid]::Empty.Guid
        } -Scope it

        # Verify the output
        $errorVariable | Should BeNullOrEmpty
        $output | Should Be $defaultChannel
    }

    it "outputs an error when no channels match the display name" {
        # Call the function
        $output = Get-TeamChannelObject -TeamDisplayName "Team Name" -ChannelDisplayName "Team Channel 2" `
            -ErrorAction SilentlyContinue -ErrorVariable errorVariable

        # Verify the mocks
        Assert-MockCalled Get-TeamObject -Times 1 -Exactly -Scope it
        Assert-MockCalled Get-TeamChannel -Times 1 -Exactly -ParameterFilter {
            $GroupId -eq [Guid]::Empty.Guid
        } -Scope it

        # Verify the output
        $errorVariable | Should Not BeNullOrEmpty
        $output | Should Be $null
    }

    # Modify the mock of Get-TeamChannel
    mock Get-TeamChannel {
        return @($defaultChannel, $defaultChannel)
    }

    it "outputs an error when more than one channel matches the display name" {
        # Call the function
        $output = Get-TeamChannelObject -TeamDisplayName "Team Name" -ChannelDisplayName "Team Channel" `
            -ErrorAction SilentlyContinue -ErrorVariable errorVariable

        # Verify the mocks
        Assert-MockCalled Get-TeamObject -Times 1 -Exactly -Scope it
        Assert-MockCalled Get-TeamChannel -Times 1 -Exactly -ParameterFilter {
            $GroupId -eq [Guid]::Empty.Guid
        } -Scope it

        # Verify the output
        $errorVariable | Should Not BeNullOrEmpty
        $output | Should Be $null
    }
}