test: Add unit tests for custom profile attributes select options

This commit is contained in:
Julien Tant (aider)
2025-02-12 12:40:19 -07:00
parent 4a44d23095
commit 463ad161c5
@@ -0,0 +1,86 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package model
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewCustomProfileAttributeSelectOption(t *testing.T) {
t.Run("creates valid option with generated ID", func(t *testing.T) {
option := NewCustomProfileAttributeSelectOption("Test Option", "#FF0000")
assert.NotEmpty(t, option.ID)
assert.True(t, IsValidId(option.ID))
assert.Equal(t, "Test Option", option.Name)
assert.Equal(t, "#FF0000", option.Color)
})
}
func TestCustomProfileAttributeSelectOptionIsValid(t *testing.T) {
tests := []struct {
name string
option CustomProfileAttributesSelectOption
wantErr string
}{
{
name: "valid option",
option: CustomProfileAttributesSelectOption{
ID: NewId(),
Name: "Test Option",
Color: "#FF0000",
},
wantErr: "",
},
{
name: "empty ID",
option: CustomProfileAttributesSelectOption{
ID: "",
Name: "Test Option",
Color: "#FF0000",
},
wantErr: "id cannot be empty",
},
{
name: "invalid ID",
option: CustomProfileAttributesSelectOption{
ID: "invalid-id",
Name: "Test Option",
Color: "#FF0000",
},
wantErr: "id is not a valid ID",
},
{
name: "empty name",
option: CustomProfileAttributesSelectOption{
ID: NewId(),
Name: "",
Color: "#FF0000",
},
wantErr: "name cannot be empty",
},
{
name: "empty color",
option: CustomProfileAttributesSelectOption{
ID: NewId(),
Name: "Test Option",
Color: "",
},
wantErr: "color cannot be empty",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.option.IsValid()
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
}
})
}
}