From 463ad161c5141cc8994bab0278cdc1df54f4b4e3 Mon Sep 17 00:00:00 2001 From: "Julien Tant (aider)" Date: Wed, 12 Feb 2025 12:40:19 -0700 Subject: [PATCH] test: Add unit tests for custom profile attributes select options --- .../model/custom_profile_attributes_test.go | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 server/public/model/custom_profile_attributes_test.go diff --git a/server/public/model/custom_profile_attributes_test.go b/server/public/model/custom_profile_attributes_test.go new file mode 100644 index 00000000000..33e1dbc912c --- /dev/null +++ b/server/public/model/custom_profile_attributes_test.go @@ -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) + } + }) + } +}