Skip to content

Commit

Permalink
- Added Afk command and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
PureKrome committed Dec 18, 2011
1 parent 31fd53e commit ada7852
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 6 deletions.
58 changes: 58 additions & 0 deletions JabbR.Test/CommandManagerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,64 @@ public void CanSetNoteWithNoTextClearsTheNoteProperty()
}
}

public class AfkCommand
{
[Fact]
public void CanSetAfkWithTextSetsTheNoteProperty()
{
// Arrange.
const string note = "I'll be back later!";
var repository = new InMemoryRepository();
var user = new ChatUser
{
Name = "asshat",
Id = "1"
};
repository.Add(user);
var service = new ChatService(repository, new Mock<ICryptoService>().Object);
var notificationService = new Mock<INotificationService>();
var commandManager = new CommandManager("clientid",
"1",
null,
service,
repository,
notificationService.Object);
// Act.
bool result = commandManager.TryHandleCommand("/afk " + note);

Assert.True(result);
Assert.Equal("Afk " + note, user.Note);
notificationService.Verify(x => x.ChangeNote(user, user.Note), Times.Once());
}

[Fact]
public void CanSetAfkWithNoTextSetTheNoteProperty()
{
// Arrange.
var repository = new InMemoryRepository();
var user = new ChatUser
{
Name = "asshat",
Id = "1"
};
repository.Add(user);
var service = new ChatService(repository, new Mock<ICryptoService>().Object);
var notificationService = new Mock<INotificationService>();
var commandManager = new CommandManager("clientid",
"1",
null,
service,
repository,
notificationService.Object);
// Act.
bool result = commandManager.TryHandleCommand("/afk ");

Assert.True(result);
Assert.Equal("Afk", user.Note);
notificationService.Verify(x => x.ChangeNote(user, user.Note), Times.Once());
}
}

public class HelpCommand
{
[Fact]
Expand Down
10 changes: 4 additions & 6 deletions JabbR/Chat.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,13 +1042,11 @@
},
changeNote: function (user, roomName, note) {
var room = getRoomElements(roomName),
$user = room.getUserReferences(user.Name),
src = 'http://jabbr.net/Content/images/toast-on.png';
$user = room.getUserReferences(user.Name),
src = 'http://jabbr.net/Content/images/toast-on.png';

var element = $user.find('.note');
if (note == null && element != null) {
element.empty();
} else {
var element = $user.find('.note').empty();
if (note != null) {
element.append('<img src="' + src + '" alt="' + note + '" title="' + note +'" />');
}
}
Expand Down
25 changes: 25 additions & 0 deletions JabbR/Commands/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,12 @@ private bool TryHandleUserCommand(string commandName, string[] parts)

return true;
}
else if (commandName.Equals("afk", StringComparison.OrdinalIgnoreCase))
{
HandleAfk(user, parts);

return true;
}

return false;
}
Expand Down Expand Up @@ -794,5 +799,25 @@ private void HandleNote(ChatUser user, ICollection<string> parts)

_repository.CommitChanges();
}

private void HandleAfk(ChatUser user, IEnumerable<string> parts)
{
if (user == null)
{
throw new ArgumentNullException("user");
}

if (parts == null)
{
throw new ArgumentNullException("parts");
}

// First part is the command.
// Second is the AFK initial text.
var newParts = new List<string> {"afk", "Afk"};
newParts.AddRange(parts.Skip(1));

HandleNote(user, newParts);
}
}
}

0 comments on commit ada7852

Please sign in to comment.