-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#95): fetch all available users for assign user view
- Loading branch information
Showing
10 changed files
with
214 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package jira | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
|
@@ -9,6 +10,7 @@ import ( | |
func Test_httpJiraApi_FindUsers(t *testing.T) { | ||
type args struct { | ||
project string | ||
query string | ||
} | ||
tests := []struct { | ||
name string | ||
|
@@ -24,6 +26,14 @@ func Test_httpJiraApi_FindUsers(t *testing.T) { | |
}, | ||
false, | ||
}, | ||
{"should find users with query without error", | ||
args{project: "FJIR", query: "test"}, | ||
[]User{ | ||
{AccountId: "456", EmailAddress: "[email protected]", DisplayName: "Mateusz Kulawik", Active: true, TimeZone: "Europe/Warsaw", Locale: "en_GB", AvatarUrls: nil}, | ||
{AccountId: "123", EmailAddress: "", DisplayName: "mateusz.test", Active: true, TimeZone: "Europe/Warsaw", Locale: "en_US", AvatarUrls: nil}, | ||
}, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
@@ -53,7 +63,13 @@ func Test_httpJiraApi_FindUsers(t *testing.T) { | |
` | ||
w.Write([]byte(body)) //nolint:errcheck | ||
}) | ||
got, err := api.FindUsers(tt.args.project) | ||
var got []User | ||
var err error | ||
if tt.args.query == "" { | ||
got, err = api.FindUsers(tt.args.project) | ||
} else { | ||
got, err = api.FindUsersWithQuery(tt.args.project, tt.args.query) | ||
} | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("FindUsers() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
|
@@ -64,3 +80,34 @@ func Test_httpJiraApi_FindUsers(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func Test_httpJiraApi_FindUsers_returnError(t *testing.T) { | ||
type args struct { | ||
project string | ||
query string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{"should return error when search failed", | ||
args{project: "FJIR"}, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// given | ||
api := NewJiraApiMock(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(500) | ||
}) | ||
|
||
// when | ||
_, err := api.FindUsersWithQuery(tt.args.project, tt.args.query) | ||
|
||
// then | ||
assert.Error(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package users | ||
|
||
import ( | ||
"github.com/mk-5/fjira/internal/app" | ||
"github.com/mk-5/fjira/internal/jira" | ||
) | ||
|
||
type RecordsProvider interface { | ||
FetchUsers(projectKey string, query string) []jira.User | ||
} | ||
|
||
type apiRecordsProvider struct { | ||
api jira.Api | ||
} | ||
|
||
func NewApiRecordsProvider(api jira.Api) RecordsProvider { | ||
return &apiRecordsProvider{ | ||
api: api, | ||
} | ||
} | ||
|
||
func (r *apiRecordsProvider) FetchUsers(projectKey string, query string) []jira.User { | ||
us, err := r.api.FindUsersWithQuery(projectKey, query) | ||
if err != nil { | ||
app.Error(err.Error()) | ||
} | ||
return us | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package users | ||
|
||
import ( | ||
"github.com/mk-5/fjira/internal/app" | ||
"github.com/mk-5/fjira/internal/jira" | ||
"github.com/mk-5/fjira/internal/ui" | ||
) | ||
|
||
const ( | ||
typeaheadSearchThreshold = 100 | ||
) | ||
|
||
func NewFuzzyFind(projectKey string, api jira.Api) (*app.FuzzyFind, *[]jira.User) { | ||
var us []jira.User | ||
provider := NewApiRecordsProvider(api) | ||
return app.NewFuzzyFindWithProvider(ui.MessageSelectUser, func(query string) []string { | ||
// it searches up to {typeaheadThreshold} records using typeahead - then it do regular fuzzy-find | ||
if len(us) > 0 && len(us) < typeaheadSearchThreshold { | ||
return FormatJiraUsers(us) | ||
} | ||
us = provider.FetchUsers(projectKey, query) | ||
us = append(us, jira.User{DisplayName: ui.MessageAll}) | ||
usersStrings := FormatJiraUsers(us) | ||
return usersStrings | ||
}), &us | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package users | ||
|
||
import ( | ||
"github.com/gdamore/tcell/v2" | ||
"github.com/mk-5/fjira/internal/app" | ||
"github.com/mk-5/fjira/internal/jira" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestNewFuzzyFind(t *testing.T) { | ||
screen := tcell.NewSimulationScreen("utf-8") | ||
_ = screen.Init() //nolint:errcheck | ||
defer screen.Fini() | ||
app.InitTestApp(screen) | ||
|
||
tests := []struct { | ||
name string | ||
}{ | ||
{"should use api find up to typeaheadThreshold, then fuzzy-find"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// given | ||
apiCall := false | ||
sb2User := strings.Builder{} | ||
sb2User.WriteString(`[{"id": "U1", "displayName": "Bob"}, {"id": "U2", "displayName": "John"}]`) | ||
sb1000Users := strings.Builder{} | ||
sb1000Users.WriteString("[") | ||
for i := 0; i < 1000; i++ { | ||
sb1000Users.WriteString(`{"id": "U1", "displayName": "Bob"}`) | ||
if i != 999 { | ||
sb1000Users.WriteString(",") | ||
} | ||
} | ||
sb1000Users.WriteString("]") | ||
var apiResult *strings.Builder | ||
api := jira.NewJiraApiMock(func(w http.ResponseWriter, r *http.Request) { | ||
apiCall = true | ||
w.WriteHeader(200) | ||
_, _ = w.Write([]byte(apiResult.String())) | ||
}) | ||
fuzzyFind, us := NewFuzzyFind("ABC", api) | ||
fuzzyFind.SetDebounceDisabled(true) | ||
|
||
// when | ||
apiCall = false | ||
apiResult = &sb1000Users | ||
fuzzyFind.SetQuery("") | ||
fuzzyFind.Update() | ||
|
||
// then | ||
assert.True(t, apiCall) | ||
assert.Equal(t, 1001, len(*us)) | ||
|
||
// when | ||
apiCall = false | ||
apiResult = &sb1000Users | ||
fuzzyFind.SetQuery("b") | ||
fuzzyFind.Update() | ||
|
||
// then | ||
assert.True(t, apiCall) | ||
assert.Equal(t, 1001, len(*us)) | ||
|
||
// when | ||
apiCall = false | ||
apiResult = &sb2User | ||
fuzzyFind.SetQuery("bo") | ||
fuzzyFind.Update() | ||
|
||
// then | ||
assert.True(t, apiCall) | ||
assert.Equal(t, 3, len(*us)) | ||
|
||
// when | ||
apiCall = false | ||
apiResult = &sb2User | ||
fuzzyFind.SetQuery("bo") | ||
fuzzyFind.Update() | ||
|
||
// then | ||
assert.False(t, apiCall, "api shouldn't be called because previous call returned 2 records") | ||
assert.Equal(t, 3, len(*us)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters