This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and test the reminders list (#215)
* refactor: improve tests of reminders list Test the actual reminders composition and order. * refactor: name variables in channels list
- Loading branch information
Showing
2 changed files
with
102 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
) | ||
|
||
func TestListReminders(t *testing.T) { | ||
p := &Plugin{} | ||
|
||
user := &model.User{ | ||
Email: "[email protected]", | ||
|
@@ -22,53 +23,109 @@ func TestListReminders(t *testing.T) { | |
Locale: "en", | ||
} | ||
|
||
occurrences := []Occurrence{ | ||
{ | ||
Id: model.NewId(), | ||
ReminderId: model.NewId(), | ||
Occurrence: time.Now(), | ||
originChannel := &model.Channel{ | ||
Id: model.NewId(), | ||
} | ||
|
||
publicChannel := &model.Channel{ | ||
Id: model.NewId(), | ||
Name: "public-channel", | ||
} | ||
|
||
T, _ := p.translation(user) | ||
|
||
pastOccurrenceDate := time.Now().Add(-1*time.Minute) | ||
futureOccurrenceDate := time.Now().Add(1*time.Minute) | ||
|
||
pastReminder := Reminder{ | ||
Id: model.NewId(), | ||
Username: user.Username, | ||
Message: "This reminder was triggered a single time", | ||
When: "in 1 minute", | ||
Occurrences: []Occurrence{ | ||
{ | ||
Id: model.NewId(), | ||
ReminderId: model.NewId(), | ||
Occurrence: pastOccurrenceDate, | ||
}, | ||
}, | ||
Completed: p.emptyTime, | ||
} | ||
|
||
reminders := []Reminder{ | ||
{ | ||
Id: model.NewId(), | ||
Username: user.Username, | ||
Message: "Message", | ||
When: "in 1 second", | ||
Occurrences: occurrences, | ||
Completed: time.Time{}.AddDate(1, 1, 1), | ||
upcomingReminder := Reminder{ | ||
Id: model.NewId(), | ||
Username: user.Username, | ||
Message: "This reminder triggers a single time", | ||
When: "in 1 minute", | ||
Occurrences: []Occurrence{ | ||
{ | ||
Id: model.NewId(), | ||
ReminderId: model.NewId(), | ||
Occurrence: futureOccurrenceDate, | ||
}, | ||
}, | ||
Completed: p.emptyTime, | ||
} | ||
|
||
stringReminders, _ := json.Marshal(reminders) | ||
channelReminder := Reminder{ | ||
Id: model.NewId(), | ||
Username: user.Username, | ||
Target: "~" + publicChannel.Name, | ||
Message: "This reminder posts in a channel a single time", | ||
When: "in 1 minute", | ||
Occurrences: []Occurrence{ | ||
{ | ||
Id: model.NewId(), | ||
ReminderId: model.NewId(), | ||
Occurrence: futureOccurrenceDate, | ||
}, | ||
}, | ||
Completed: p.emptyTime, | ||
} | ||
|
||
channel := &model.Channel{ | ||
Id: model.NewId(), | ||
recurringReminder := Reminder{ | ||
Id: model.NewId(), | ||
Username: user.Username, | ||
Message: "This reminder triggers several times", | ||
When: "every Monday", | ||
Occurrences: []Occurrence{ | ||
{ | ||
Id: model.NewId(), | ||
ReminderId: model.NewId(), | ||
Repeat: "every Monday at 9:00AM", | ||
Occurrence: futureOccurrenceDate, | ||
}, | ||
}, | ||
Completed: p.emptyTime, | ||
} | ||
|
||
setupAPI := func() *plugintest.API { | ||
setupAPI := func(reminders []Reminder) *plugintest.API { | ||
serializedReminders, _ := json.Marshal(reminders) | ||
api := &plugintest.API{} | ||
api.On("LogDebug", mock.Anything, mock.Anything, mock.Anything).Maybe() | ||
api.On("LogError", mock.Anything, mock.Anything, mock.Anything).Maybe() | ||
api.On("LogInfo", mock.Anything).Maybe() | ||
api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) | ||
api.On("KVGet", mock.Anything).Return(stringReminders, nil) | ||
api.On("KVGet", "testuser").Return(serializedReminders, nil) | ||
return api | ||
} | ||
|
||
t.Run("if list happens", func(t *testing.T) { | ||
|
||
api := setupAPI() | ||
defer api.AssertExpectations(t) | ||
|
||
p := &Plugin{} | ||
p.API = api | ||
|
||
assert.NotNil(t, p.ListReminders(user, channel.Id)) | ||
|
||
t.Run("the list categorizes reminders by type", func(t *testing.T) { | ||
reminders := []Reminder { pastReminder, upcomingReminder, channelReminder, recurringReminder } | ||
p.API = setupAPI(reminders) | ||
|
||
post := p.ListReminders(user, originChannel.Id) | ||
|
||
attachments := post.Attachments() | ||
assert.NotNil(t, post, "A post must be returned") | ||
assert.Equal(t, post.ChannelId, originChannel.Id, "The list must be posted to the channel it was requested") | ||
assert.Equal(t, len(reminders) + 1, len(attachments), "The list must have one attachment per active reminder, plus an attachment for control") | ||
assert.Contains(t, attachments[0].Text, T("list.upcoming"), "The first displayed reminders must be upcoming reminders") | ||
assert.Contains(t, attachments[1].Text, T("list.recurring"), "The next displayed reminders must be recurring reminders") | ||
assert.Contains(t, attachments[2].Text, T("list.past.and.incomplete"), "The next displayed reminders must be past and incomplete reminders") | ||
assert.Contains(t, attachments[3].Text, T("list.channel"), "The next displayed reminders must be channel reminders") | ||
assert.Contains(t, attachments[len(attachments)-1].Text, T("reminders.page.numbers"), "The last attachment must be list pagination and controls") | ||
}) | ||
|
||
} | ||
|
||
func TestUpdateListReminders(t *testing.T) { | ||
|